资源文件中包含的对 UnityEngine.Object 的可序列化延迟引用。
允许一个资源引用另一个资源,但会将被引用资源的加载延迟到使用之时,而不是在反序列化引用对象时加载它。
典型用例:
- 对于需要引用资源但从磁盘读取设置的导入器设置,无法加载被引用的资源,因为它们可能没有导入且尚无法访问。
- 为缩短打开场景所需的时间,仅在编辑模式下加载初始设置或显示所需的资源。
注意:
与直接引用相比,延迟引用具有轻微的性能开销。
在独立平台播放器中,当加载播放器或资源包时,将加载所有资源。
using UnityEditor.AssetImporters; using UnityEngine;
[ScriptedImporter(1, "foo")] public class FooImporter : ScriptedImporter { public LazyLoadReference<Material> m_DefaultMaterial;
public override void OnImportAsset(AssetImportContext ctx) { // At this point, 'm_DefaultMaterial' may refer to a material that has yet to be loaded into memory
Material mat; if (!m_DefaultMaterial.isSet) // 'isSet' Does not load the referenced material even if not in memory. { mat = new Material(Shader.Find("Transparent/Diffuse")); ctx.AddObjectToAsset("mat", mat); } else { mat = m_DefaultMaterial.asset; // Will load referenced material if it is not already in memory. }
var obj = GameObject.CreatePrimitive(PrimitiveType.Cube); obj.transform.GetComponent<MeshRenderer>().material = mat;
ctx.AddObjectToAsset("main", obj); ctx.SetMainObject(obj); } }
asset | 对被引用资源的访问器。 |
instanceID | Returns the instance id to the referenced asset. |
isBroken | 用于检查引用是否失效的便捷属性:引用不可用或不可加载的对象。 |
isSet | 确定某个资源是不是目标,无论该资源是否可加载。 |
LazyLoadReference_1 | Construct a new LazyLoadReference. |
LazyLoadReference<T> | Implicit conversion from asset reference to LazyLoadReference. |
LazyLoadReference<T> | Implicit conversion from instance ID to LazyLoadReference. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.