Resources.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. namespace BansheeEngine
  4. {
  5. /// <summary>
  6. /// Handles dynamic loading of resources during runtime.
  7. /// </summary>
  8. public static class Resources
  9. {
  10. /// <summary>
  11. /// Loads a resource at the specified path. If running outside of the editor you must make sure to mark that
  12. /// the resource gets included in the build. If running inside the editor this has similar functionality as
  13. /// if loading using the project library. If resource is already loaded an existing instance is returned.
  14. /// </summary>
  15. /// <typeparam name="T">Type of the resource.</typeparam>
  16. /// <param name="path">Path of the resource, relative to game directory. If running from editor this will be
  17. /// the same location as resource location in the project library.</param>
  18. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  19. public static T Load<T>(string path) where T : Resource
  20. {
  21. return (T)Internal_Load(path);
  22. }
  23. /// <summary>
  24. /// Loads a resource referenced by the provided reference. If running outside of the editor you must make sure
  25. /// to mark that the resource gets included in the build. If running inside the editor this has similar functionality
  26. /// as if loading using the project library. If resource is already loaded an existing instance is returned.
  27. /// </summary>
  28. /// <typeparam name="T">Type of the resource.</typeparam>
  29. /// <param name="reference">Reference to the resource to load.</param>
  30. /// <returns>Loaded resource, or null if resource cannot be found.</returns>
  31. public static T Load<T>(ResourceRefBase reference) where T : Resource
  32. {
  33. return (T)Internal_LoadRef(reference);
  34. }
  35. /// <summary>
  36. /// Unloads all resources that are no longer referenced. Usually the system keeps resources in memory even if
  37. /// they are no longer referenced to avoid constant loading/unloading if resource is often passed around.
  38. /// </summary>
  39. public static void UnloadUnused()
  40. {
  41. Internal_UnloadUnused();
  42. }
  43. [MethodImpl(MethodImplOptions.InternalCall)]
  44. private static extern Resource Internal_Load(string path);
  45. [MethodImpl(MethodImplOptions.InternalCall)]
  46. private static extern Resource Internal_LoadRef(ResourceRefBase reference);
  47. [MethodImpl(MethodImplOptions.InternalCall)]
  48. private static extern void Internal_UnloadUnused();
  49. }
  50. }