Resources.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 running outside of the editor you must make sure to mark that
  17. /// the resource gets included in the build. If running inside the editor this has similar functionality as
  18. /// if loading using the project library. If resource is already loaded an existing instance is returned.
  19. ///
  20. /// All resources are automatically unloaded when they go out of scope unless <see cref="keepLoaded"/> parameter
  21. /// is specified.
  22. /// </summary>
  23. /// <typeparam name="T">Type of the resource.</typeparam>
  24. /// <param name="path">Path of the resource, relative to game directory. If running from editor this will be
  25. /// the same location as resource location in the project library. If a sub-resource within
  26. /// a file is needed, append the name of the subresource to the path (for example
  27. /// mymesh.fbx/my_animation).</param>
  28. /// <param name="keepLoaded">If true the system will keep the resource loaded even when it goes out of scope.
  29. /// You must call <see cref="Release(Resource)"/> in order to allow the resource to be
  30. /// unloaded (it must be called once for each corresponding load). </param>
  31. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  32. public static T Load<T>(string path, bool keepLoaded = true) where T : Resource
  33. {
  34. return (T)Internal_Load(path, keepLoaded);
  35. }
  36. /// <summary>
  37. /// Loads a resource referenced by the provided reference. If running outside of the editor you must make sure
  38. /// to mark that the resource gets included in the build. If running inside the editor this has similar functionality
  39. /// as if loading using the project library. If resource is already loaded an existing instance is returned.
  40. ///
  41. /// All resources are automatically unloaded when they go out of scope unless <see cref="keepLoaded"/> parameter
  42. /// is specified.
  43. /// </summary>
  44. /// <typeparam name="T">Type of the resource.</typeparam>
  45. /// <param name="reference">Reference to the resource to load.</param>
  46. /// <param name="keepLoaded">If true the system will keep the resource loaded even when it goes out of scope.
  47. /// You must call <see cref="Release(ResourceRef)"/> in order to allow the resource to be
  48. /// unloaded (it must be called once for each corresponding load). </param>
  49. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  50. public static T Load<T>(ResourceRef reference, bool keepLoaded = true) where T : Resource
  51. {
  52. return (T)Internal_LoadRef(reference, keepLoaded);
  53. }
  54. /// <summary>
  55. /// Releases an internal reference to the resource held by the resources system. This allows the resource
  56. /// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter.
  57. ///
  58. /// Alternatively you can also skip manually calling <see cref="Release(ResourceRef)"/> and call <see cref="UnloadUnused"/>
  59. /// which will unload all resources that do not have any external references, but you lose the fine grained control
  60. /// of what will be unloaded.
  61. /// </summary>
  62. /// <param name="resource">Resource to release</param>
  63. public static void Release(ResourceRef resource)
  64. {
  65. if (resource == null)
  66. return;
  67. Internal_ReleaseRef(resource.GetCachedPtr());
  68. }
  69. /// <summary>
  70. /// Releases an internal reference to the resource held by the resources system. This allows the resource
  71. /// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter.
  72. ///
  73. /// Alternatively you can also skip manually calling <see cref="Release(Resource)"/> and call <see cref="UnloadUnused"/>
  74. /// which will unload all resources that do not have any external references, but you lose the fine grained control
  75. /// of what will be unloaded.
  76. /// </summary>
  77. /// <param name="resource">Resource to release</param>
  78. public static void Release(Resource resource)
  79. {
  80. if(resource == null)
  81. return;
  82. Internal_Release(resource.GetCachedPtr());
  83. }
  84. /// <summary>
  85. /// Unloads all resources that are no longer referenced. This only applies to resources loaded with "keepLoaded"
  86. /// parameter, as all other resources will be unloaded when they go out of scope.
  87. /// </summary>
  88. public static void UnloadUnused()
  89. {
  90. Internal_UnloadUnused();
  91. }
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. private static extern Resource Internal_Load(string path, bool keepLoaded);
  94. [MethodImpl(MethodImplOptions.InternalCall)]
  95. private static extern Resource Internal_LoadRef(ResourceRef reference, bool keepLoaded);
  96. [MethodImpl(MethodImplOptions.InternalCall)]
  97. private static extern void Internal_Release(IntPtr resource);
  98. [MethodImpl(MethodImplOptions.InternalCall)]
  99. private static extern void Internal_ReleaseRef(IntPtr resource);
  100. [MethodImpl(MethodImplOptions.InternalCall)]
  101. private static extern void Internal_UnloadUnused();
  102. }
  103. /** @} */
  104. }