Asset<T> Class

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 a GameObject. Unfortunately, you can’t use the basic Object.Instantiate(Warrior) due to ambiguity, but you can pass Warrior.Target into any of the other overloads or use Warrior.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 of Resources.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(string) 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

Name Value Summary
IsLoaded bool
Checks if the asset is currently loaded.
Name string
The file name of the asset (without the file extension).
Target T
Loads, caches, and returns the asset.

Methods

Name Value Summary
ForceReload() void
[Editor-Only] Reloads the asset, even if it was already loaded.
Instantiate(Transform) 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(Transform, bool) 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(Vector3, Quaternion) 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(Vector3, Quaternion, Transform) 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).

Operators

Name Value Summary
implicit operator Asset<T>(string) Asset<T>
implicit operator T(Asset<T>) T