public static bool TryCloneObject(string key, out GameObject clonedObject) { NpcCache.CachingItem cachingItem; if (NpcCache.Enabled && NpcCache.mCachedItems.TryGetValue(key, out cachingItem) && cachingItem.model != null) { cachingItem.refCount++; clonedObject = (UnityEngine.Object.Instantiate(cachingItem.model) as GameObject); NpcCache.RegisterAutoRemover(key, clonedObject); return(true); } clonedObject = null; return(false); }
public static GameObject AddAndClone(string key, IDownloadedItem item) { if (!NpcCache.Enabled) { GameObject original = item.mainAsset as GameObject; return(UnityEngine.Object.Instantiate(original) as GameObject); } if (string.IsNullOrEmpty(key)) { int startIndex = item.assetPath.IndexOf('.'); key = item.assetPath.Remove(startIndex); } GameObject gameObject; if (NpcCache.TryCloneObject(key, out gameObject)) { return(gameObject); } if (NpcCache.mRoot == null) { NpcCache.mRoot = GameObject.Find("@Internal NPC"); if (NpcCache.mRoot == null) { NpcCache.mRoot = new GameObject("@Internal NPC"); NpcCache.mRoot.SetActive(false); UnityEngine.Object.DontDestroyOnLoad(NpcCache.mRoot); } } gameObject = (UnityEngine.Object.Instantiate(item.mainAsset) as GameObject); gameObject.name = key; NpcCache.mCachedItems[key] = new NpcCache.CachingItem(gameObject); gameObject.transform.parent = NpcCache.mRoot.transform; GameObject gameObject2 = UnityEngine.Object.Instantiate(gameObject) as GameObject; NpcCache.RegisterAutoRemover(key, gameObject2); return(gameObject2); }