Resources.cs 5.9 KB

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