示例#1
0
        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);
        }
示例#2
0
        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());
            }
        }
示例#3
0
 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);
     }
 }
示例#4
0
 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));
             }
         }
     }
 }
示例#5
0
        IEnumerator activeLifeSpanCoroutine(GOFactoryTracker obj, float time)
        {
            yield return(new WaitForSeconds(time));

            remove(obj);
        }