Resources.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.
  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. /// Unloads a resource, freeing its memory.
  25. /// </summary>
  26. /// <param name="resource">Resource to unload.</param>
  27. public static void Unload(Resource resource)
  28. {
  29. if (resource != null)
  30. Internal_Unload(resource.GetCachedPtr());
  31. }
  32. /// <summary>
  33. /// Unloads all resources that are no longer referenced. Usually the system keeps resources in memory even if
  34. /// they are no longer referenced to avoid constant loading/unloading if resource is often passed around.
  35. /// </summary>
  36. public static void UnloadUnused()
  37. {
  38. Internal_UnloadUnused();
  39. }
  40. [MethodImpl(MethodImplOptions.InternalCall)]
  41. private static extern Resource Internal_Load(string path);
  42. [MethodImpl(MethodImplOptions.InternalCall)]
  43. private static extern void Internal_Unload(IntPtr resourcePtr);
  44. [MethodImpl(MethodImplOptions.InternalCall)]
  45. private static extern void Internal_UnloadUnused();
  46. }
  47. }