using System; using System.Runtime.CompilerServices; namespace BansheeEngine { /// /// Handles dynamic loading of resources during runtime. /// public static class Resources { /// /// Loads a resource at the specified path. If running outside of the editor you must make sure to mark that /// the resource gets included in the build. If running inside the editor this has similar functionality as /// if loading using the project library. If resource is already loaded an existing instance is returned. /// /// All resources are automatically unloaded when they go out of scope unless parameter /// is specified. /// /// Type of the resource. /// Path of the resource, relative to game directory. If running from editor this will be /// the same location as resource location in the project library. /// If true the system will keep the resource loaded even when it goes out of scope. /// You must call in order to allow the resource to be /// unloaded (it must be called once for each corresponding load). /// Loaded resource, or null if resource cannot be found. public static T Load(string path, bool keepLoaded = true) where T : Resource { return (T)Internal_Load(path, keepLoaded); } /// /// Loads a resource referenced by the provided reference. If running outside of the editor you must make sure /// to mark that the resource gets included in the build. If running inside the editor this has similar functionality /// as if loading using the project library. If resource is already loaded an existing instance is returned. /// /// All resources are automatically unloaded when they go out of scope unless parameter /// is specified. /// /// Type of the resource. /// Reference to the resource to load. /// If true the system will keep the resource loaded even when it goes out of scope. /// You must call in order to allow the resource to be /// unloaded (it must be called once for each corresponding load). /// Loaded resource, or null if resource cannot be found. public static T Load(ResourceRef reference, bool keepLoaded = true) where T : Resource { return (T)Internal_LoadRef(reference, keepLoaded); } /// /// Releases an internal reference to the resource held by the resources system. This allows the resource /// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter. /// /// Alternatively you can also skip manually calling and call /// which will unload all resources that do not have any external references, but you lose the fine grained control /// of what will be unloaded. /// /// Resource to release public static void Release(ResourceRef resource) { if (resource == null) return; Internal_ReleaseRef(resource.GetCachedPtr()); } /// /// Releases an internal reference to the resource held by the resources system. This allows the resource /// to be unloaded when it goes out of scope, if the resource was loaded with "keepLoaded" parameter. /// /// Alternatively you can also skip manually calling and call /// which will unload all resources that do not have any external references, but you lose the fine grained control /// of what will be unloaded. /// /// Resource to release public static void Release(Resource resource) { if(resource == null) return; Internal_Release(resource.GetCachedPtr()); } /// /// Unloads all resources that are no longer referenced. This only applies to resources loaded with "keepLoaded" /// parameter, as all other resources will be loaded when they go out of scope. /// public static void UnloadUnused() { Internal_UnloadUnused(); } [MethodImpl(MethodImplOptions.InternalCall)] private static extern Resource Internal_Load(string path, bool keepLoaded); [MethodImpl(MethodImplOptions.InternalCall)] private static extern Resource Internal_LoadRef(ResourceRef reference, bool keepLoaded); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_Release(IntPtr resource); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_ReleaseRef(IntPtr resource); [MethodImpl(MethodImplOptions.InternalCall)] private static extern void Internal_UnloadUnused(); } }