Beispiel #1
0
    private static GameObject GetNewObject(bool isAlwaysNew, string objName, GameObject prefab, GameObject parent = null, bool isSetActive = true)
    {
        GameObject go   = null;
        string     name = objName;

        if (string.IsNullOrEmpty(name))
        {
            name = prefab.name;
        }

        if (!isAlwaysNew && IsExist(name))
        {
            go = recyclePools[name][0];
            recyclePools[name].RemoveAt(0);

            AssetsPoolManager.MarkeFlag(name, typeof(GameObject));
        }
        else
        {
            if (prefab == null && !string.IsNullOrEmpty(objName))
            {
                go = NewGameObject(name, parent);
                if (createPools.ContainsKey(name))
                {
                    createPools[name].Add(go);
                }
                else
                {
                    createPools.Add(name, new List <GameObject>()
                    {
                        go
                    });
                }
            }
            else if (prefab != null && string.IsNullOrEmpty(objName))
            {
                go = ObjectInstantiate(prefab, parent);
            }
        }

        PoolObject po = go.GetComponent <PoolObject>();

        if (po)
        {
            try
            {
                po.OnFetch();
            }
            catch (Exception e)
            {
                Debug.LogError("GetNewObject Error: " + e.ToString());
            }
        }

        if (isSetActive)
        {
            go.SetActive(true);
        }

        if (parent == null)
        {
            go.transform.SetParent(null);
        }
        else
        {
            go.transform.SetParent(parent.transform);
        }
        return(go);
    }
Beispiel #2
0
    private static GameObject GetByPrefabe(string objName, GameObject prefab, GameObject parent = null, bool isSetActive = true)
    {
        GameObject go   = null;
        string     name = objName;

        if (string.IsNullOrEmpty(name))
        {
            name = prefab.name;
        }
        if (IsExist(name))
        {
            go = s_objectPool[name][0];

            s_objectPool[name].RemoveAt(0);
            PoolObject po = go.GetComponent <PoolObject>();
            if (po)
            {
                po.OnFetch();
            }
            AssetsPoolManager.MarkeFlag(name, true);
        }
        else
        {
            if (prefab == null && !string.IsNullOrEmpty(objName))
            {
                go = CreateGameObject(name, parent);
            }
            else if (prefab != null && string.IsNullOrEmpty(objName))
            {
                go = ObjectInstantiate(prefab, parent);
            }
            PoolObject po = go.GetComponent <PoolObject>();
            if (po)
            {
                po.OnCreate();
            }
            if (createPools.ContainsKey(name))
            {
                createPools[name].Add(go);
            }
            else
            {
                createPools.Add(name, new List <GameObject>()
                {
                    go
                });
            }
        }
        if (isSetActive)
        {
            go.SetActive(true);
        }

        if (parent == null)
        {
            go.transform.SetParent(null);
        }
        else
        {
            go.transform.SetParent(parent.transform);
        }

        //go.transform.localScale = Vector3.one;

        return(go);
    }