而不是使用
Find
更确切地说,使用变量时,存储实例化对象时获得的引用,并在以后重用它们。
// Here you store the reference from
// Assetbundle.LoadAsset
private object loadedAsset;
// Here you store the reference to the assets instance itself
private GameObject assetInstance;
public void LoadAsstBundles(int choice)
{
// Is _assetBundle available?
if (!_assetsBundle)
{
Debug.Log("Could Not load AssetBundles", this);
return;
}
// Was the bundle loaded before?
if (!loadedAsset)
{
loadedAsset = _assetsBundle.LoadAsset(_assetname);
if(!loadedAsset)
{
Debug.LogError("unable to load asset", this);
return;
}
Debug.Log("Asset Bundles Loaded", this);
}
// Is the object Instantiated in the scene?
if(!assetInstance)
{
assetInstance = (GameObject)Instantiate(loadedAsset, ParentTransform);
Debug.LogFormat(this, "Instantiated {0}", assetInstance.name);
}
// no need to go by names
// simply get the correct child by index
var selected = assetInstance.transform.GetChild(choice);
// Enable or disable the childs
// simply run through all childs no need to get their names etc
foreach(Transform chair in assetInstance.transform)
{
chair.gameObject.SetActive(chair == selected);
}
}