起因
最近和小伙伴交流的时候突然注意到, Addressable
在异步实例化物体时,Unity
生命周期也不太相同
下面为各种情况下的测试数据
await 加载
加载脚本
public class TestAddressable : MonoBehaviour {
private async Task Awake() {
try{
Log.Debug("Start Load Prefab");
await Addressables.InstantiateAsync("TestPrefab").Task;
Log.Debug("Finish Load Prefab");
}
catch(Exception e){
Log.Error(e);
}
}
}
生命周期脚本
public class TestUpdate : MonoBehaviour {
private void Awake() { Log.Debug("Awake"); }
private void Start() { Log.Debug("Start"); }
private void OnEnable() { Log.Debug("OnEnable"); }
private void Update() { Log.Debug("Update"); }
}
这个时候我们会得到如下的顺序,在
await
结束后,Start
和Update
都会早于结束加载的生命周期
await 加载 + 手动 active
我们默认将 Prefab
设置为 inactive
状态,加载脚本中对齐进行手动 active
public class TestAddressable : MonoBehaviour {
private async Task Awake() {
try{
Log.Debug("Start Load Prefab");
GameObject obj = await Addressables.InstantiateAsync("TestPrefab").Task;
Log.Debug("Finish Load Prefab");
obj.SetActive(true);
}
catch(Exception e){
Log.Error(e);
}
}
}
结果和预想的差不多,如果用 await 这种方式加载,担心生命周期的顺序和代码有所冲突,可以采用手动激活的方式
回调方式加载
官方给的例子也是这种加载方式
public class TestAddressable : MonoBehaviour {
private async Task Awake() {
try{
Log.Debug("Start Load Prefab");
Addressables.InstantiateAsync("TestPrefab").Completed += obj => {
Log.Debug("Finish Load Prefab");
};
}
catch(Exception e){
Log.Error(e);
}
}
}
这样和正常实例化物体时生命周期是一致的