Class ModResource
This class provides access to the resources listed in the Resources section of the mod manifest vis static members and is the base class for the objects representing a resource.
Namespace: Modding
Assembly: Assembly-CSharp.dll
Syntax
public abstract class ModResource
Constructors
ModResource()
Declaration
protected ModResource()
Properties
AllResourcesLoaded
Determines whether all the resources of the calling mod have finished loading.
Declaration
public static bool AllResourcesLoaded { get; }
Property Value
Type | Description |
---|---|
Boolean |
Available
Whether the resource is ready for use, that is, it has finished loading without errors.
Declaration
public virtual bool Available { get; }
Property Value
Type | Description |
---|---|
Boolean |
Error
A description of the error that occured while loading this resource.
An empty string if there was no error.
Declaration
public abstract string Error { get; }
Property Value
Type | Description |
---|---|
String |
HasError
Whether there was an error loading the resource.
Declaration
public abstract bool HasError { get; }
Property Value
Type | Description |
---|---|
Boolean |
See Also
Loaded
Whether this resource has finished loading. Is also true if there was an error loading the resource.
Declaration
public abstract bool Loaded { get; }
Property Value
Type | Description |
---|---|
Boolean |
Name
The name of this resource.
Declaration
public string Name { get; }
Property Value
Type | Description |
---|---|
String |
Type
The type of this resource.
Declaration
public ModResource.ResourceType Type { get; protected set; }
Property Value
Type | Description |
---|---|
ModResource.ResourceType |
Methods
CreateAssetBundleResource(String, String, Boolean)
Creates a new AssetBundle resource at runtime.
The resource will then work just like one that was declared in the Mod.xml file.
The path is either relative to the mod's data directory, or the mod's root directory. It is not relative to the Resources/ directory, unlike the paths in the Mod.xml file.
Declaration
public static ModAssetBundle CreateAssetBundleResource(string name, string path, bool data = false)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
String | path | Relative path to the resource. |
Boolean | data | Whether the path is relative to the data directory. |
Returns
Type | Description |
---|---|
ModAssetBundle | Newly created ModAssetBundle resource. |
CreateAudioClipResource(String, String, Boolean)
Creates a new AudioClip resource at runtime.
The resource will then work just like one that was declared in the Mod.xml file.
The path is either relative to the mod's data directory, or the mod's root directory. It is not relative to the Resources/ directory, unlike the paths in the Mod.xml file.
Declaration
public static ModAudioClip CreateAudioClipResource(string name, string path, bool data = false)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
String | path | Relative path to the resource. |
Boolean | data | Whether the path is relative to the data directory. |
Returns
Type | Description |
---|---|
ModAudioClip | Newly created ModAudioClip resource. |
CreateMeshResource(String, String, Boolean)
Creates a new Mesh resource at runtime.
The resource will then work just like one that was declared in the Mod.xml file.
The path is either relative to the mod's data directory, or the mod's root directory. It is not relative to the Resources/ directory, unlike the paths in the Mod.xml file.
Declaration
public static ModMesh CreateMeshResource(string name, string path, bool data = false)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
String | path | Relative path to the resource. |
Boolean | data | Whether the path is relative to the data directory. |
Returns
Type | Description |
---|---|
ModMesh | Newly created ModMesh resource. |
CreateTextureResource(String, String, Boolean, Boolean)
Creates a new Texture resource at runtime.
The resource will then work just like one that was declared in the Mod.xml file.
The path is either relative to the mod's data directory, or the mod's root directory. It is not relative to the Resources/ directory, unlike the paths in the Mod.xml file.
Declaration
public static ModTexture CreateTextureResource(string name, string path, bool data = false, bool readable = false)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
String | path | Relative path to the resource. |
Boolean | data | Whether the path is relative to the data directory. |
Boolean | readable | Whether the texture should be loaded as readable. |
Returns
Type | Description |
---|---|
ModTexture | Newly created ModTexture resource. |
Get(ResourceReference)
Returns the resource specified by the given reference.
Declaration
public static ModResource Get(ResourceReference reference)
Parameters
Type | Name | Description |
---|---|---|
ResourceReference | reference | Reference to the resource to fetch. |
Returns
Type | Description |
---|---|
ModResource | The resource, or null if no resource with the given name could be found. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | If called from an assembly not listed in the mod manifest. |
GetAssetBundle(String)
Returns the asset bundle resource with the given name.
The resource must be declared in the mod manifest's Resources section.
Declaration
public static ModAssetBundle GetAssetBundle(string name)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
Returns
Type | Description |
---|---|
ModAssetBundle | The resource. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | If called from an assembly not listed in the mod manifest. |
ArgumentException | If the resource with the given name is not an asset bundle. |
ArgumentException | If no resource with the given name exists. |
GetAudioClip(String)
Returns the audio clip resource with the given name.
The resource must be declared in the mod manifest's Resources section.
Declaration
public static ModAudioClip GetAudioClip(string name)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
Returns
Type | Description |
---|---|
ModAudioClip | The resource. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | If called from an assembly not listed in the mod manifest. |
ArgumentException | If the resource with the given name is not an audio clip. |
ArgumentException | If no resource with the given name exists. |
GetMesh(String)
Returns the mesh resource with the given name.
The resource must be declared in the mod manifest's Resources section.
Declaration
public static ModMesh GetMesh(string name)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
Returns
Type | Description |
---|---|
ModMesh | The resource. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | If called from an assembly not listed in the mod manifest. |
ArgumentException | If the resource with the given name is not a mesh. |
ArgumentException | If no resource with the given name exists. |
GetTexture(String)
Returns the texture resource with the given name.
The resource must be declared in the mod manifest's Resources section.
Declaration
public static ModTexture GetTexture(string name)
Parameters
Type | Name | Description |
---|---|---|
String | name | Name of the resource. |
Returns
Type | Description |
---|---|
ModTexture | The resource. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | If called from an assembly not listed in the mod manifest. |
ArgumentException | If the resource with the given name is not a texture. |
ArgumentException | If no resource with the given name exists. |
SetOnObject(GameObject, Action<GameObject>, Action)
SetOnObject is a utility method that easily allows handling longer resource loading times, as well as objects that should receive a resource that is not loaded yet being instantiated.
Declaration
public void SetOnObject(GameObject go, Action<GameObject> postSetAction = null, Action prefabPostSetAction = null)
Parameters
Type | Name | Description |
---|---|---|
UnityEngine.GameObject | go | Object on which to set the resource. |
Action<UnityEngine.GameObject> | postSetAction | Callback for every object that is set. |
Action | prefabPostSetAction | Callback for the original object. |
Remarks
When SetOnObject is called on an object, an OnLoad handler for the resource is installed that will automatically set the appropriate value (renderer.material.mainTexture for a ModTexture, meshFilter.mesh for a ModMesh, and audioSource.clip for a ModAudioClip) when the resource is done loading.
However, the major benefit of using SetOnObject is that it will also automatically handle the given GameObject being instantiated/copied. All instances of the object will also automatically receive the resource.
The two optional callbacks can be used to perform additional actions that are necessary to correctly set the resource. The first one is called for every object on which the resource is set (including the original object), and the second one only for the object originally passed into the function.
Asset bundle resources can not be set on objects.
Exceptions
Type | Condition |
---|---|
InvalidOperationException | If this resource is an asset bundle. |
TriggerOnLoad()
Declaration
protected void TriggerOnLoad()
Events
OnAllResourcesLoaded
Event that fires once all resources of the mod have finished loading.
If all resources have already finished loading when a callback is registered, it is called immediately.
Declaration
public static event Action OnAllResourcesLoaded
Event Type
Type | Description |
---|---|
Action |
OnLoad
Called when the resource has finished loading.
This is also called if there was an error loading the resource.
If a new handler is added to this and the resource has already finished loading, it is immediately called.
Declaration
public virtual event Action OnLoad
Event Type
Type | Description |
---|---|
Action |
OnResourceLoaded
Event that fires whenever a resource of your mod finishes loading.
Note: The event also fires if there was an error loading the resource.
If there are already any resources that have finished loading when you register a callback, the callback will be called once for reach of them.
Declaration
public static event Action<ModResource> OnResourceLoaded
Event Type
Type | Description |
---|---|
Action<ModResource> |