Summary
A wrapper which simplifies the process of lazy loading and caching resources and other assets, i.e. only
loading them when they are first needed instead of on startup.
- Assembly
- Weaver
.dll - Namespace
- Weaver
- Base Types
-
- Object
graph BT
Type-->Base0["Object"]
Type["Asset<T>"]
class Type type-node
Syntax
public sealed class Asset<T> where T : Object
Examples
This class can essentially replace the following code:
private static GameObject _Warrior;
public static GameObject Warrior
{
get
{
if (_Warrior == null)
_Warrior = Resources.Load<GameObject>("Creatures/Goblins/Warrior");
return _Warrior;
}
}
With a single field:
public static readonly Asset<GameObject> Warrior = "Creatures/Goblins/Warrior";
-
You can either use an implicit conversion from string as shown above, or you can use a regular constructor
like so:
new Asset<GameObject>("Creatures/Goblins/Warrior"). -
You can access the actual goblin warrior prefab using
Warrior.Target . This will load the prefab the first time it is actually used and cache the value for better performance when you need the asset again in the future, just like the property in the above example. -
You can also implicitly cast the
Warrior field to aGameObject . Unfortunately, you can’t use the basicObject.Instantiate(Warrior) due to ambiguity, but you can passWarrior.Target into any of the other overloads or useWarrior.Instantiate() . -
If you specify a path that begins with "Assets/" and includes the file extension (such as
"Assets/Art/Creatures/Goblins/Warrior.fbx"), it will use
AssetDatabase.LoadAssetAtPath instead ofResources.Load . This allows you to target any asset in your project, though it won’t be able to load it at runtime once your project is built.
Type Parameters
Name | Description |
---|---|
T | The type of asset being wrapped. Must derive from UnityEngine.Object . |
Constructors
Name | Summary |
---|---|
Asset |
Creates an Weaver.Asset`1 targeting the specified asset or resource path. |
Fields
Name | Constant Value | Summary |
---|---|---|
Path |
The resource path or asset path of the asset.
|
Properties
Methods
Name | Value | Summary |
---|---|---|
ForceReload |
void |
[Editor-Only] Reloads the asset, even if it was already loaded.
|
Instantiate |
T |
Clones the specified asset and returns the clone.
Uses
UnityEngine.Object.Instantiate(UnityEngine.Object,UnityEngine.Transform) .
In the Unity Editor, it uses PrefabUtility.InstantiatePrefab instead.
|
Instantiate |
T |
Clones the specified asset and returns the clone.
Uses
UnityEngine.Object.Instantiate(UnityEngine.Object,UnityEngine.Transform,System.Boolean) .
In the Unity Editor, it uses PrefabUtility.InstantiatePrefab instead.
|
Instantiate |
T |
Clones the specified asset and returns the clone.
Uses
UnityEngine.Object.Instantiate(UnityEngine.Object,UnityEngine.Vector3,UnityEngine.Quaternion) .
In the Unity Editor, it uses PrefabUtility.InstantiatePrefab instead.
|
Instantiate |
T |
Clones the specified asset and returns the clone.
Uses
UnityEngine.Object.Instantiate(UnityEngine.Object,UnityEngine.Vector3,UnityEngine.Quaternion,UnityEngine.Transform) .
In the Unity Editor, it uses PrefabUtility.InstantiatePrefab instead.
|
Load |
void |
Loads the asset into memory if it wasn't already loaded.
|
Unload |
void |
Unloads the asset from memory using
UnityEngine.Resources.UnloadAsset(UnityEngine.Object) . |