ResourceRef.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// Common class for all resource references. <see cref="ResourceRef{T}"/>
  7. /// </summary>
  8. public class ResourceRefBase : ScriptObject
  9. {
  10. /// <summary>
  11. /// Constructor for internal use only.
  12. /// </summary>
  13. protected ResourceRefBase()
  14. { }
  15. /// <summary>
  16. /// Checks is the referenced resource loaded
  17. /// </summary>
  18. public bool IsLoaded
  19. {
  20. get { return Internal_IsLoaded(mCachedPtr); }
  21. }
  22. /// <inheritdoc/>
  23. public override bool Equals(object other)
  24. {
  25. if (!(other is ResourceRefBase))
  26. return false;
  27. ResourceRefBase otherRef = (ResourceRefBase)other;
  28. return Internal_GetUUID(mCachedPtr).Equals(Internal_GetUUID(otherRef.mCachedPtr));
  29. }
  30. /// <inheritdoc/>
  31. public override int GetHashCode()
  32. {
  33. return Internal_GetUUID(mCachedPtr).GetHashCode();
  34. }
  35. [MethodImpl(MethodImplOptions.InternalCall)]
  36. private static extern bool Internal_IsLoaded(IntPtr thisPtr);
  37. [MethodImpl(MethodImplOptions.InternalCall)]
  38. protected static extern Resource Internal_GetResource(IntPtr thisPtr);
  39. [MethodImpl(MethodImplOptions.InternalCall)]
  40. private static extern string Internal_GetUUID(IntPtr thisPtr);
  41. }
  42. /// <summary>
  43. /// Allows you to store a reference to a resource without needing to have that resource loaded.
  44. /// </summary>
  45. public class ResourceRef<T> : ResourceRefBase where T : Resource
  46. {
  47. /// <summary>
  48. /// Retrieves the referenced resource. This will load the resources if it is not already loaded.
  49. /// </summary>
  50. /// <typeparam name="T">Type of the resource to load.</typeparam>
  51. /// <returns>Loaded resource object.</returns>
  52. public T Get()
  53. {
  54. return (T)Internal_GetResource(mCachedPtr);
  55. }
  56. }
  57. }