Ejemplo n.º 1
0
		public static void CreateSimplePoolManager(SimplePool firstPool)
		{
			GameObject go = new GameObject();
			SimplePoolManager manager = go.AddComponent<SimplePoolManager>();
			SimplePoolManager.Instance = manager;
			SimplePoolManager.Add(firstPool);
			go.name = "SimplePoolManager";
		}
Ejemplo n.º 2
0
        public static void CreateSimplePoolManager(SimplePool firstPool)
        {
            GameObject        go      = new GameObject();
            SimplePoolManager manager = go.AddComponent <SimplePoolManager>();

            SimplePoolManager.Instance = manager;
            SimplePoolManager.Add(firstPool);
            go.name = "SimplePoolManager";
        }
Ejemplo n.º 3
0
        public static SimplePool CreatePool(GameObject prefab)
        {
            GameObject go   = new GameObject();
            SimplePool pool = go.AddComponent <SimplePool>();

            pool.ThisPrefab = prefab;
            go.name         = "SimplePool";
            pool.PoolName   = go.name;

            SimplePoolManager.Add(pool);
            if (go.transform.parent == null)
            {
                go.transform.SetParent(SimplePoolManager.Instance.transform);
            }

            return(pool);
        }
Ejemplo n.º 4
0
        //Overloads with different parameters
        public static GameObject Spawn(GameObject prefab, Vector3 position)
        {
            GameObject go = null;

            foreach (var pool in Instance.Pools)
            {
                if (pool.Uses(prefab))
                {
                    go = pool.Spawn(position);
                    pool.AfterSpawn(go);
                    return(go);
                }
            }
            SimplePool newPool = CreatePool(prefab);

            go = newPool.Spawn(position);
            newPool.AfterSpawn(go);
            return(go);
        }
Ejemplo n.º 5
0
        public static void MovePool(int from, int to)
        {
            if (from < 0 || from >= Instance.Pools.Count)
            {
                return;
            }
            if (to < 0 || to >= Instance.Pools.Count)
            {
                return;
            }

            SimplePool tmp = Instance.Pools[to];

            Instance.Pools[to]   = Instance.Pools[from];
            Instance.Pools[from] = tmp;
            IUpdatePoolNames();

            if (Instance.NameIndexChanged != null)
            {
                Instance.NameIndexChanged(from, to);
            }
        }
Ejemplo n.º 6
0
 public static bool Contains(SimplePool pool)
 {
     return(Instance.Pools.Contains(pool));
 }
Ejemplo n.º 7
0
		public static void Remove(SimplePool pool)
		{
			if (Contains(pool))
				Instance.Pools.Remove(pool);
			Instance.UpdatePoolNames();
		}
Ejemplo n.º 8
0
		public static void Add(SimplePool pool)
		{
			if (!Contains(pool))
				Instance.Pools.Add(pool);
			Instance.UpdatePoolNames();
		}
Ejemplo n.º 9
0
		public static bool Contains(SimplePool pool)
		{
			return Instance.Pools.Contains(pool);
		}
Ejemplo n.º 10
0
		private void DoInstantiate(SimplePool.InstantiateInfo info)
		{
			info.pool.InstantiateNewObject();
		}
Ejemplo n.º 11
0
		private void DoDestroy(SimplePool.DestroyInfo info)
		{
			info.pool.DestroyOneObject();
		}
Ejemplo n.º 12
0
		public void AddInstantiateInfo(SimplePool.InstantiateInfo info)
		{
			if (SplitWorkload)
				instantiateQueue.Enqueue(info);
			else
				DoInstantiate(info);
		}
Ejemplo n.º 13
0
		public void AddDestroyInfo(SimplePool.DestroyInfo info)
		{
			if (SplitWorkload)
				destroyQueue.Enqueue(info);
			else
				DoDestroy(info);
		}
Ejemplo n.º 14
0
        public override void OnInspectorGUI()
        {
            SimplePool pool = (SimplePool)target;

            if (pool == null)
            {
                return;
            }

            if (SimplePoolManager.Instance != null)
            {
                if (!SimplePoolManager.Contains(pool))
                {
                    Undo.RecordObject(SimplePoolManager.Instance, "Add Pool");
                    pool.PoolName = pool.gameObject.name;
                    SimplePoolManager.Add(pool);
                }
            }

            Color tmpColor;

            serializedObject.Update();

            //Editor
            SerializedProperty TabState = serializedObject.FindProperty("TabState");

            //SimplePool
            SerializedProperty PoolName                 = serializedObject.FindProperty("PoolName");
            SerializedProperty EnablePooling            = serializedObject.FindProperty("EnablePooling");
            SerializedProperty ThisPrefab               = serializedObject.FindProperty("ThisPrefab");
            SerializedProperty InstantiateAmountOnStart = serializedObject.FindProperty("InstantiateAmountOnStart");
            SerializedProperty DoNotDestroyOnLoad       = serializedObject.FindProperty("DoNotDestroyOnLoad");
            SerializedProperty UseThisAsParent          = serializedObject.FindProperty("UseThisAsParent");
            SerializedProperty Parent            = serializedObject.FindProperty("Parent");
            SerializedProperty ReparentOnDespawn = serializedObject.FindProperty("ReparentOnDespawn");

            SerializedProperty UseOnSpawnMessage   = serializedObject.FindProperty("UseOnSpawnMessage");
            SerializedProperty UseOnDespawnMessage = serializedObject.FindProperty("UseOnDespawnMessage");

            SerializedProperty ActivateOnSpawn     = serializedObject.FindProperty("ActivateOnSpawn");
            SerializedProperty DeactivateOnDespawn = serializedObject.FindProperty("DeactivateOnDespawn");

            SerializedProperty MoveOnDespawn           = serializedObject.FindProperty("MoveOnDespawn");
            SerializedProperty UseTransformPosition    = serializedObject.FindProperty("UseTransformPosition");
            SerializedProperty DeactivatePosition      = serializedObject.FindProperty("DeactivatePosition");
            SerializedProperty TargetPositionTransform = serializedObject.FindProperty("TargetPositionTransform");

            SerializedProperty DestroyUnusedObjects = serializedObject.FindProperty("DestroyUnusedObjects");
            SerializedProperty Intelligence         = serializedObject.FindProperty("Intelligence");
            SerializedProperty WantedFreeObjects    = serializedObject.FindProperty("WantedFreeObjects");

            //Debug
            SerializedProperty RunningDestroyWorker     = serializedObject.FindProperty("RunningDestroyWorker");
            SerializedProperty RunningInstantiateWorker = serializedObject.FindProperty("RunningInstantiateWorker");

            SerializedProperty ObjectCount      = serializedObject.FindProperty("ObjectCount");
            SerializedProperty AvailableObjects = serializedObject.FindProperty("AvailableObjects");
            SerializedProperty SpawnedObjects   = serializedObject.FindProperty("SpawnedObjects");

            SerializedProperty EnableProfiler = serializedObject.FindProperty("EnableProfiler");

            SerializedProperty UsedTimesCount  = serializedObject.FindProperty("UsedTimesCount");
            SerializedProperty CurrentUsedTime = serializedObject.FindProperty("CurrentUsedTime");
            SerializedProperty MinUsedTime     = serializedObject.FindProperty("MinUsedTime");
            SerializedProperty MaxUsedTime     = serializedObject.FindProperty("MaxUsedTime");
            SerializedProperty AverageUsedTime = serializedObject.FindProperty("AverageUsedTime");



            //Tabs
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("General Settings", GUILayout.Height(TabButtonHeight)))
            {
                TabState.intValue = 0;
            }
            if (GUILayout.Button("Object Control", GUILayout.Height(TabButtonHeight)))
            {
                TabState.intValue = 1;
            }
            if (GUILayout.Button("Informations", GUILayout.Height(TabButtonHeight)))
            {
                TabState.intValue = 2;
            }
            EditorGUILayout.EndHorizontal();

            SimpleEditor.SplitBox();

            if (TabState.intValue == 0)
            {
                #region General Settings

                tmpColor            = GUI.backgroundColor;
                GUI.backgroundColor = EnablePooling.boolValue ? Color.red : Color.green;
                if (GUILayout.Button(EnablePooling.boolValue ? "Disable Pooling" : "Enable Pooling"))
                {
                    EnablePooling.boolValue = !EnablePooling.boolValue;
                }
                GUI.backgroundColor = tmpColor;

                if (!EnablePooling.boolValue)
                {
                    EditorGUILayout.LabelField("SimplePool.Spawn works like Object.Instantiate");
                    EditorGUILayout.LabelField("SimplePool.Despawn works like Object.Destroy");
                    SimpleEditor.SplitBox();
                }

                EditorGUILayout.LabelField("Use \"/\" to create categories");
                string tmp = EditorGUILayout.TextField("PoolName:", PoolName.stringValue);
                if (tmp != PoolName.stringValue)
                {
                    PoolName.stringValue = tmp;
                    ((SimplePool)target).gameObject.name = PoolName.stringValue;

                    serializedObject.ApplyModifiedProperties();
                    SimplePoolManager.Instance.UpdatePoolNames();
                }


                EditorGUILayout.PropertyField(ThisPrefab, new GUIContent("GameObject Prefab:"));

                InstantiateAmountOnStart.intValue = EditorGUILayout.IntField("Start Instances:", InstantiateAmountOnStart.intValue);

                DoNotDestroyOnLoad.boolValue = EditorGUILayout.Toggle("Keep this on LoadLevel:", DoNotDestroyOnLoad.boolValue);

                UseThisAsParent.boolValue = EditorGUILayout.Toggle("Use this as parent:", UseThisAsParent.boolValue);

                if (!UseThisAsParent.boolValue)
                {
                    EditorGUILayout.PropertyField(Parent, new GUIContent("Custom Parent:"));
                }
                ReparentOnDespawn.boolValue = EditorGUILayout.Toggle("Reparent on Despawn:", ReparentOnDespawn.boolValue);

                #endregion
            }
            else if (TabState.intValue == 1)
            {
                #region Object Control

                EditorGUILayout.LabelField("Sends \"OnSpawn\" to any class on the spawned Object");
                UseOnSpawnMessage.boolValue = EditorGUILayout.Toggle("Use Spawn Message", UseOnSpawnMessage.boolValue);
                EditorGUILayout.LabelField("Sends \"OnDespawn\" to any class on the spawned Object");
                UseOnDespawnMessage.boolValue = EditorGUILayout.Toggle("Use Despawn Message", UseOnDespawnMessage.boolValue);

                EditorGUILayout.LabelField("Activates the Object when it gets spawned");
                ActivateOnSpawn.boolValue = EditorGUILayout.Toggle("Activate on spawn", ActivateOnSpawn.boolValue);

                EditorGUILayout.LabelField("Deactivates the Object when it gets despawned");
                DeactivateOnDespawn.boolValue = EditorGUILayout.Toggle("Deactivate on despawn", DeactivateOnDespawn.boolValue);

                EditorGUILayout.LabelField("Moves the Object to a given position on despawn");
                MoveOnDespawn.boolValue = EditorGUILayout.Toggle("Move on despawn", MoveOnDespawn.boolValue);

                if (MoveOnDespawn.boolValue)
                {
                    UseTransformPosition.boolValue = EditorGUILayout.Toggle("Use Transform position", UseTransformPosition.boolValue);

                    if (UseTransformPosition.boolValue)
                    {
                        EditorGUILayout.PropertyField(TargetPositionTransform, new GUIContent("Position Transform:"));
                    }
                    else
                    {
                        EditorGUILayout.Vector3Field("World Position", DeactivatePosition.vector3Value);
                    }
                }

                EditorGUILayout.LabelField("Allows SimplePool to destroy unused Objects");
                DestroyUnusedObjects.boolValue = EditorGUILayout.Toggle("Destroy unused Objects", DestroyUnusedObjects.boolValue);
                EditorGUILayout.LabelField("Caches last actions to calculate if it needs to destroy or create Objects");
                Intelligence.boolValue = EditorGUILayout.Toggle("Use Intelligence", Intelligence.boolValue);

                EditorGUILayout.LabelField("SimplePool tries to always have this amount of available Objects");
                WantedFreeObjects.intValue = EditorGUILayout.IntField("Free Object Count:", WantedFreeObjects.intValue);


                #endregion
            }
            else if (TabState.intValue == 2)
            {
                #region Informations

                EditorGUILayout.IntField("Object Count:", ObjectCount.intValue);
                EditorGUILayout.IntField("Free Objects:", AvailableObjects.intValue);
                EditorGUILayout.IntField("Spawned Objects:", SpawnedObjects.intValue);

                EditorGUILayout.IntField("Instantiate Worker Count:", RunningDestroyWorker.intValue);
                EditorGUILayout.IntField("Destroy Worker Count:", RunningInstantiateWorker.intValue);

                SimpleEditor.SplitBox();
                EditorGUILayout.LabelField("Running Information:");
                UsedTimesCount.intValue  = EditorGUILayout.IntField("Cached values amount:", UsedTimesCount.intValue);
                EnableProfiler.boolValue = EditorGUILayout.Toggle("Enable Profiler:", EnableProfiler.boolValue);

                if (EnableProfiler.boolValue)
                {
                    EditorGUILayout.LabelField(string.Format("Current work: {0:0.000000}s", CurrentUsedTime.floatValue));
                    EditorGUILayout.LabelField(string.Format("Min work: {0:0.000000}s", MinUsedTime.floatValue));
                    EditorGUILayout.LabelField(string.Format("Max work: {0:0.000000}s", MaxUsedTime.floatValue));
                    EditorGUILayout.LabelField(string.Format("Average work: {0:0.000000}s", AverageUsedTime.floatValue));
                }

                #endregion
            }

            serializedObject.ApplyModifiedProperties();
        }
Ejemplo n.º 15
0
        public static void CreateSimplePool()
        {
            SimplePool pool = CreatePool(null);

            Selection.activeGameObject = pool.gameObject;
        }
Ejemplo n.º 16
0
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            SimplePoolManager manager = (SimplePoolManager)target;
            //SimplePoolManager.Instance = manager;


            SerializedProperty Pools_FoldOut = serializedObject.FindProperty("Pools_FoldOut");

            SerializedProperty SplitWorkload            = serializedObject.FindProperty("SplitWorkload");
            SerializedProperty UpdateEveryXFrames       = serializedObject.FindProperty("UpdateEveryXFrames");
            SerializedProperty MaxUsedDeltaTimePerFrame = serializedObject.FindProperty("MaxUsedDeltaTimePerFrame");


            SerializedProperty EnableProfiler = serializedObject.FindProperty("EnableProfiler");

            SerializedProperty UsedTimesCount  = serializedObject.FindProperty("UsedTimesCount");
            SerializedProperty CurrentUsedTime = serializedObject.FindProperty("CurrentUsedTime");
            SerializedProperty MinUsedTime     = serializedObject.FindProperty("MinUsedTime");
            SerializedProperty MaxUsedTime     = serializedObject.FindProperty("MaxUsedTime");
            SerializedProperty AverageUsedTime = serializedObject.FindProperty("AverageUsedTime");

            if (GUILayout.Button("Add Pool", GUILayout.Height(TabButtonHeight)))
            {
                SimplePool pool;
                Undo.RegisterFullObjectHierarchyUndo(manager, "Find Pools");
                GameObject go = new GameObject();
                go.name = "NewPool";
                go.transform.SetParent(manager.transform);
                pool          = go.AddComponent <SimplePool>();
                pool.PoolName = go.name;
                SimplePoolManager.Add(pool);
                //manager.FindAllPools();
                EditorUtility.SetDirty(manager);
            }

            //EditorGUILayout.IntField("Pool Count:", manager.Pools.Count);

            if (manager.Pools.Count > 0)
            {
                Pools_FoldOut.boolValue = EditorGUILayout.Foldout(Pools_FoldOut.boolValue, string.Format("Pool-List: {0}", manager.Pools.Count));
                if (Pools_FoldOut.boolValue)
                {
                    SimplePool pool       = null;
                    int        moveFrom   = 0;
                    int        moveTo     = 0;
                    int        removePool = -1;
                    EditorGUI.indentLevel++;
                    for (int poolIndex = 0; poolIndex < manager.Pools.Count; poolIndex++)
                    {
                        pool = manager.Pools[poolIndex];
                        if (pool == null)
                        {
                            removePool = poolIndex;
                            break;
                        }
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.ObjectField(pool, typeof(SimplePool), false);

                        if (GUILayout.Button("Edit", EditorStyles.miniButton, GUILayout.Width(30f)))
                        {
                            Selection.activeGameObject = pool.gameObject;
                        }
                        GUI.enabled = poolIndex != 0;
                        if (GUILayout.Button("⤴", EditorStyles.miniButton, GUILayout.Width(20f)))
                        {
                            moveFrom = poolIndex;
                            moveTo   = poolIndex - 1;
                        }
                        GUI.enabled = poolIndex + 1 < manager.Pools.Count;
                        if (GUILayout.Button("⤵", EditorStyles.miniButton, GUILayout.Width(20f)))
                        {
                            moveFrom = poolIndex;
                            moveTo   = poolIndex + 1;
                        }
                        GUI.enabled = true;
                        if (GUILayout.Button("-", EditorStyles.miniButton, GUILayout.Width(20f)))
                        {
                            removePool = poolIndex;
                        }
                        EditorGUILayout.EndHorizontal();
                    }

                    if (removePool >= 0)
                    {
                        Undo.RegisterFullObjectHierarchyUndo(manager, "Remove Pool");
                        GameObject go = null;
                        if (manager.Pools[removePool])
                        {
                            go = manager.Pools[removePool].gameObject;
                        }
                        //Undo.RegisterFullObjectHierarchyUndo(manager, "Remove Pool");
                        //if (go) Undo.RecordObject(go, "Remove Pool");
                        SimplePoolManager.RemoveAt(removePool);
                        if (go)
                        {
                            Undo.DestroyObjectImmediate(go);
                        }
                        EditorUtility.SetDirty(manager);
                    }
                    if (moveFrom >= 0 && moveTo >= 0)
                    {
                        Undo.RecordObject(manager, "Move Pool");
                        SimplePoolManager.MovePool(moveFrom, moveTo);
                        EditorUtility.SetDirty(manager);
                    }
                    EditorGUI.indentLevel--;
                }
            }

            EditorGUILayout.LabelField("Uses a worker queue to split operations to different frames");
            SplitWorkload.boolValue = EditorGUILayout.Toggle("Split Workload:", SplitWorkload.boolValue);

            UpdateEveryXFrames.intValue = EditorGUILayout.IntField("Work every x frame:", UpdateEveryXFrames.intValue);

            MaxUsedDeltaTimePerFrame.floatValue = EditorGUILayout.FloatField("Max work (seconds):", MaxUsedDeltaTimePerFrame.floatValue);


            SimpleEditor.SplitBox();
            EditorGUILayout.LabelField("Running Information:");
            UsedTimesCount.intValue  = EditorGUILayout.IntField("Cached values amount:", UsedTimesCount.intValue);
            EnableProfiler.boolValue = EditorGUILayout.Toggle("Enable Profiler:", EnableProfiler.boolValue);

            if (EnableProfiler.boolValue)
            {
                EditorGUILayout.LabelField(string.Format("Current work: {0:0.000000}s", CurrentUsedTime.floatValue));
                EditorGUILayout.LabelField(string.Format("Min work: {0:0.000000}s", MinUsedTime.floatValue));
                EditorGUILayout.LabelField(string.Format("Max work: {0:0.000000}s", MaxUsedTime.floatValue));
                EditorGUILayout.LabelField(string.Format("Average work: {0:0.000000}s", AverageUsedTime.floatValue));
            }

            serializedObject.ApplyModifiedProperties();
        }