Resources.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.CompilerServices;
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup Resources
  8. * @{
  9. */
  10. /// <summary>
  11. /// Handles dynamic loading of resources during runtime.
  12. /// </summary>
  13. public static class Resources
  14. {
  15. /// <summary>
  16. /// Loads a resource at the specified path. If resource is already loaded an existing instance is returned.
  17. /// </summary>
  18. /// <remarks>
  19. /// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
  20. /// running inside the editor this has similar functionality as if loading using the project library.
  21. /// </remarks>
  22. /// <typeparam name="T">Type of the resource.</typeparam>
  23. /// <param name="path">Path of the resource, relative to game directory. If running from editor this will be
  24. /// relative to the project library. If a sub-resource within a file is needed, append the name
  25. /// of the subresource to the path (for example mymesh.fbx/my_animation).</param>
  26. /// <param name="flags">Flags used to control the load process.</param>
  27. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  28. public static T Load<T>(string path, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
  29. {
  30. return (T)Internal_Load(path, flags);
  31. }
  32. /// <summary>
  33. /// Loads a resource with the specified UUID. If resource is already loaded an existing instance is returned.
  34. /// </summary>
  35. /// <remarks>
  36. /// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
  37. /// running inside the editor this has similar functionality as if loading using the project library.
  38. /// </remarks>
  39. /// <typeparam name="T">Type of the resource.</typeparam>
  40. /// <param name="uuid">Unique identifier of the resource to load.</param>
  41. /// <param name="flags">Flags used to control the load process.</param>
  42. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  43. public static T Load<T>(UUID uuid, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
  44. {
  45. return (T)Internal_LoadFromUUID(ref uuid, flags);
  46. }
  47. /// <summary>
  48. /// Loads a resource at the specified path asynchonously (on a separate thread). If resource is already loaded
  49. /// an existing instance is returned. Use <see cref="RRefBase.IsLoaded"/> to confirm the resource has been loaded
  50. /// before using it. Use <see cref="GetLoadProgress"/> to track the loading progress of the resource.
  51. /// </summary>
  52. /// <remarks>
  53. /// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
  54. /// running inside the editor this has similar functionality as if loading using the project library.
  55. /// </remarks>
  56. /// <typeparam name="T">Type of the resource.</typeparam>
  57. /// <param name="path">Path of the resource, relative to game directory. If running from editor this will be
  58. /// relative to the project library. If a sub-resource within a file is needed, append the name
  59. /// of the subresource to the path (for example mymesh.fbx/my_animation).</param>
  60. /// <param name="flags">Flags used to control the load process.</param>
  61. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  62. public static RRef<T> LoadAsync<T>(string path, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
  63. {
  64. return Internal_LoadAsync(path, flags).As<T>();
  65. }
  66. /// <summary>
  67. /// Loads a resource with the specified UUID asynchonously (on a separate thread). If resource is already loaded
  68. /// an existing instance is returned. Use <see cref="RRefBase.IsLoaded"/> to confirm the resource has been loaded
  69. /// before using it. Use <see cref="GetLoadProgress"/> to track the loading progress of the resource.
  70. /// </summary>
  71. /// <remarks>
  72. /// If running outside of the editor you must make sure to mark that the resource gets included in the build. If
  73. /// running inside the editor this has similar functionality as if loading using the project library.
  74. /// </remarks>
  75. /// <typeparam name="T">Type of the resource.</typeparam>
  76. /// <param name="uuid">Unique identifier of the resource to load.</param>
  77. /// <param name="flags">Flags used to control the load process.</param>
  78. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  79. public static RRef<T> LoadAsync<T>(UUID uuid, ResourceLoadFlag flags = ResourceLoadFlag.Default) where T : Resource
  80. {
  81. return Internal_LoadAsyncFromUUID(ref uuid, flags).As<T>();
  82. }
  83. /// <summary>
  84. /// Releases an internal reference to the resource held by the resources system. This allows the resource
  85. /// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter.
  86. ///
  87. /// Alternatively you can also skip manually calling <see cref="Release(RRefBase)"/> and call <see cref="UnloadUnused"/>
  88. /// which will unload all resources that do not have any external references, but you lose the fine grained control
  89. /// of what will be unloaded.
  90. /// </summary>
  91. /// <param name="resource">Resource to release</param>
  92. public static void Release(RRefBase resource)
  93. {
  94. if (resource == null)
  95. return;
  96. Internal_ReleaseRef(resource.GetCachedPtr());
  97. }
  98. /// <summary>
  99. /// Releases an internal reference to the resource held by the resources system. This allows the resource
  100. /// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter.
  101. ///
  102. /// Alternatively you can also skip manually calling <see cref="Release(Resource)"/> and call <see cref="UnloadUnused"/>
  103. /// which will unload all resources that do not have any external references, but you lose the fine grained control
  104. /// of what will be unloaded.
  105. /// </summary>
  106. /// <param name="resource">Resource to release</param>
  107. public static void Release(Resource resource)
  108. {
  109. if(resource == null)
  110. return;
  111. Internal_Release(resource.GetCachedPtr());
  112. }
  113. /// <summary>
  114. /// Unloads all resources that are no longer referenced. This only applies to resources loaded with "keepLoaded"
  115. /// parameter, as all other resources will be unloaded when they go out of scope.
  116. /// </summary>
  117. public static void UnloadUnused()
  118. {
  119. Internal_UnloadUnused();
  120. }
  121. /// <summary>
  122. /// Returns the loading progress of a resource that's being asynchronously loaded.
  123. /// </summary>
  124. /// <param name="resource">Resource whose load progress to check.</param>
  125. /// <param name="includeDependencies">If false the progress will reflect the load progress only for this individual
  126. /// resource. If true the progress will reflect load progress of this resource
  127. /// and all of its dependencies.</param>
  128. /// <returns>Load progress in range [0, 1].</returns>
  129. public static float GetLoadProgress(RRefBase resource, bool includeDependencies = true)
  130. {
  131. if(resource == null)
  132. return 0.0f;
  133. return Internal_GetLoadProgress(resource.GetCachedPtr(), includeDependencies);
  134. }
  135. [MethodImpl(MethodImplOptions.InternalCall)]
  136. private static extern Resource Internal_Load(string path, ResourceLoadFlag flags);
  137. [MethodImpl(MethodImplOptions.InternalCall)]
  138. private static extern Resource Internal_LoadFromUUID(ref UUID uuid, ResourceLoadFlag flags);
  139. [MethodImpl(MethodImplOptions.InternalCall)]
  140. private static extern RRefBase Internal_LoadAsync(string path, ResourceLoadFlag flags);
  141. [MethodImpl(MethodImplOptions.InternalCall)]
  142. private static extern RRefBase Internal_LoadAsyncFromUUID(ref UUID uuid, ResourceLoadFlag flags);
  143. [MethodImpl(MethodImplOptions.InternalCall)]
  144. private static extern float Internal_GetLoadProgress(IntPtr resource, bool loadDependencies);
  145. [MethodImpl(MethodImplOptions.InternalCall)]
  146. private static extern void Internal_Release(IntPtr resource);
  147. [MethodImpl(MethodImplOptions.InternalCall)]
  148. private static extern void Internal_ReleaseRef(IntPtr resource);
  149. [MethodImpl(MethodImplOptions.InternalCall)]
  150. private static extern void Internal_UnloadUnused();
  151. }
  152. /** @} */
  153. }