예제 #1
0
        public override void OnInspectorGUI()
        {
            PoolManager t = (PoolManager)target;

            EditorGUILayout.Space();

            t.dontDestroyOnLoad = EditorTools.Toggle("Don't destroy on load", t.dontDestroyOnLoad, true);
            EditorGUILayout.Space();

            t.isGarbageCollector = EditorTools.Toggle("Enable garbage collector", t.isGarbageCollector);
            if (t.isGarbageCollector)
            {
                t.garbageCycletime = EditorGUILayout.FloatField("Garbage cycle time", t.garbageCycletime);
            }


            EditorGUILayout.Space();

            EditorGUILayout.BeginHorizontal();

            EditorTools.Title("Per-Object Pool options", false, Screen.width - 55);
            if (EditorTools.ButtonTitle("+", false, 22))
            {
                PoolProperty po = new PoolProperty();
                t.poolObjectsProperties.Add(po);
            }
            EditorGUILayout.EndHorizontal();


            if (t.poolObjectsProperties.Count > 0)
            {
                EditorTools.BeginGroup(); {
                    EditorGUILayout.Space();
                    for (int i = 0; i < t.poolObjectsProperties.Count; i++)
                    {
                        EditorGUILayout.BeginHorizontal();
                        if (EditorTools.Button("X", Color.red, 19))
                        {
                            PoolManager.DestroyPool(t.poolObjectsProperties[i].obj, true);
                            t.poolObjectsProperties.RemoveAt(i);
                            i--;
                        }
                        if (i >= 0)
                        {
                            string label = "Empty";
                            if (t.poolObjectsProperties[i].obj)
                            {
                                label = t.poolObjectsProperties[i].obj.name;
                            }
                            t.poolObjectsProperties[i].showInpsector = EditorTools.ChildFoldOut(t.poolObjectsProperties[i].showInpsector, label, Color.white, Screen.width - 60);
                        }
                        EditorGUILayout.EndHorizontal();

                        if (i >= 0)
                        {
                            if (t.poolObjectsProperties[i].showInpsector)
                            {
                                EditorTools.BeginGroup(25); {
                                    EditorGUI.indentLevel++;

                                    // Prefab
                                    GameObject tmpObj = (GameObject)EditorGUILayout.ObjectField("Prefab", t.poolObjectsProperties[i].obj, typeof(GameObject), true);
                                    if (tmpObj != t.poolObjectsProperties[i].obj)
                                    {
                                        PoolManager.DestroyPool(t.poolObjectsProperties[i].obj, true);

                                        t.poolObjectsProperties[i].obj = tmpObj;

                                        //if (!t.poolObjectsProperties[i].preLoadAtStart){
                                        PoolManager.CreatePool(t.poolObjectsProperties[i].obj, t.poolObjectsProperties[i].poolAmount);
                                        //}
                                    }

                                    EditorGUILayout.Space();

                                    // Preload
                                    bool tmpBool = EditorTools.Toggle("Pre-Load at start", t.poolObjectsProperties[i].preLoadAtStart);
                                    if (tmpBool != t.poolObjectsProperties[i].preLoadAtStart)
                                    {
                                        t.poolObjectsProperties[i].preLoadAtStart = tmpBool;
                                        if (t.poolObjectsProperties[i].preLoadAtStart)
                                        {
                                            PoolManager.DestroyPool(t.poolObjectsProperties[i].obj, true);
                                        }
                                        else
                                        {
                                            PoolManager.DestroyPool(t.poolObjectsProperties[i].obj, true);
                                            PoolManager.CreatePool(t.poolObjectsProperties[i].obj, t.poolObjectsProperties[i].poolAmount);
                                        }
                                    }

                                    int amount = EditorGUILayout.IntField("Preload Amount", t.poolObjectsProperties[i].poolAmount);
                                    if (amount != t.poolObjectsProperties[i].poolAmount)
                                    {
                                        t.poolObjectsProperties[i].poolAmount = amount;
                                        PoolManager.DestroyPool(t.poolObjectsProperties[i].obj, true);
                                        if (!t.poolObjectsProperties[i].preLoadAtStart)
                                        {
                                            PoolManager.CreatePool(t.poolObjectsProperties[i].obj, t.poolObjectsProperties[i].poolAmount);
                                        }
                                    }

                                    EditorGUILayout.Space();
                                    // WillGrow
                                    t.poolObjectsProperties[i].allowGrowth = EditorTools.Toggle("Allow growth", t.poolObjectsProperties[i].allowGrowth);

                                    EditorGUILayout.BeginHorizontal();
                                    t.poolObjectsProperties[i].limitGrowth = EditorTools.Toggle("Limit growth", t.poolObjectsProperties[i].limitGrowth);
                                    if (t.poolObjectsProperties[i].limitGrowth)
                                    {
                                        t.poolObjectsProperties[i].limit = EditorGUILayout.IntField("", t.poolObjectsProperties[i].limit, GUILayout.Width(50));
                                        if (t.poolObjectsProperties[i].limit < t.poolObjectsProperties[i].poolAmount)
                                        {
                                            t.poolObjectsProperties[i].limit = t.poolObjectsProperties[i].poolAmount;
                                        }
                                    }
                                    EditorGUILayout.EndHorizontal();

                                    EditorGUI.indentLevel--;
                                } EditorTools.EndGroup();
                            }
                        }
                    }
                } EditorTools.EndGroup();
            }

            if (GUI.changed && !EditorApplication.isPlaying)
            {
                EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
            }
        }
예제 #2
0
        private GameObject _Spawn(GameObject obj, int amount = 1, bool allowGrowth = true, int limit = -1)
        {
            // Automatic pool creation at spawn
            if (!_keys.Contains(obj))
            {
                int indexPooled = FindPoolObjectIndex(obj);
                if (indexPooled == -1)
                {
                    PoolProperty pooledObj = new PoolProperty();
                    pooledObj.allowGrowth = allowGrowth;
                    pooledObj.obj         = obj;
                    if (limit > -1)
                    {
                        pooledObj.limitGrowth = true;
                        pooledObj.limit       = limit;
                    }
                    else
                    {
                        pooledObj.limitGrowth = false;
                    }
                    poolObjectsProperties.Add(pooledObj);
                }
                _CreatePool(obj, amount);
            }


            // Get Pool
            int index = FindPoolObjectIndex(obj);

            if (index > -1)
            {
                tmpPool = _values[index];

                // Get first available obj
                for (int i = 0; i < tmpPool.pooledObjects.Count; i++)
                {
                    if (!tmpPool.pooledObjects[i].activeInHierarchy && ((!tmpPool.haveDespawn[i]) || (tmpPool.haveDespawn[i] && !tmpPool.pooledObjects[i].GetComponent <DespawnObject>().HaveActiveChild())))
                    {
                        tmpPool.objectsLastUse[i] = Time.realtimeSinceStartup;
                        return(tmpPool.pooledObjects[i]);
                    }
                }



                // No object available, but allows growth
                int propertyIndex = FindPooledObjectOptionIndex(obj);
                if (propertyIndex > -1)
                {
                    if ((poolObjectsProperties[propertyIndex].allowGrowth && !poolObjectsProperties[propertyIndex].limitGrowth) ||
                        (poolObjectsProperties[propertyIndex].allowGrowth && poolObjectsProperties[propertyIndex].limitGrowth && _values[index].pooledObjects.Count <= poolObjectsProperties[propertyIndex].limit))
                    {
                        GameObject newObj = Instantiate(obj.gameObject);
                        newObj.transform.parent = transform;
                        newObj.SetActive(false);

                        _values[index].pooledObjects.Add(newObj);
                        _values[index].haveDespawn.Add(newObj.GetComponent <DespawnObject>());
                        _values[index].objectsLastUse.Add(Time.realtimeSinceStartup);
                        return(newObj);
                    }
                }
            }

            return(null);
        }