Resources.cs 6.0 KB

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