public GameObject createModel(string id, Vector3 position) { GameObject obj = null; if (waiting.Count > 0) { obj = waiting[0]; waiting.Remove(obj); obj.transform.position = position; obj.GetComponent <GOFactoryTracker>().resetObject(); } else { if (prefab != null) { obj = GameObject.Instantiate(prefab); } else { GameObject model = Resources.Load(machineName) as GameObject; if (model != null) { obj = GameObject.Instantiate(model); } else { Debug.LogError("[GOF]Couldn't find any prefab named " + machineName + ". Returning null."); return(null); } } obj.transform.position = position; if (machineName != "") { obj.name = machineName; } obj.transform.SetParent(factory.transform); GOFactoryTracker tracker = obj.AddComponent <GOFactoryTracker>(); tracker.Id = id; tracker.Machine = this; tracker.recycle(); } inUse.Add(id, obj); if (lifeSpan > 0) { factory.startChildCoroutine(activeLifeSpanCoroutine(obj.GetComponent <GOFactoryTracker>(), lifeSpan)); } return(obj); }
IEnumerator inactiveLifeSpanCoroutine(GOFactoryTracker obj, float time) { float coroutineStartTime = Time.time; while (obj != null && !obj.gameObject.activeSelf) { if (Time.time - coroutineStartTime > time) { remove(obj); break; } yield return(new WaitForEndOfFrame()); } }
public void remove(GOFactoryTracker obj) { if (obj != null) { if (waiting.Contains(obj.gameObject)) { waiting.Remove(obj.gameObject); } if (inUse.ContainsKey(obj.Id)) { inUse.Remove(obj.Id); } GameObject.Destroy(obj.gameObject); } }
public void putAway(GOFactoryTracker obj) { if (obj != null) { if (inUse.ContainsKey(obj.Id)) { var creepInList = inUse[obj.Id]; inUse.Remove(obj.Id); waiting.Add(creepInList); if (inactiveLifeSpan > 0) { factory.startChildCoroutine(inactiveLifeSpanCoroutine(obj, inactiveLifeSpan)); } } } }
IEnumerator activeLifeSpanCoroutine(GOFactoryTracker obj, float time) { yield return(new WaitForSeconds(time)); remove(obj); }