Ejemplo n.º 1
0
    /// <summary>
    /// Spawns an object from specified pool
    /// </summary>
    /// <param name="prefab">Pool name</param>
    /// <param name="pos">Target position</param>
    /// <param name="rot">Target rotation</param>
    /// <returns></returns>
    public static GameObject Spawn(GameObject prefab, Vector3 pos, Quaternion rot)
    {
        QuickPool targetPool = ObjectsPool.Instance.pools.Where(pool => pool.Prefab == prefab).FirstOrDefault();

        if (targetPool == null)
        {
            return(null);
        }

        GameObject obj = targetPool.GetItem();

        if (obj == null)
        {
            return(null);
        }

        obj.transform.position = pos;
        obj.transform.rotation = rot;

        if (ObjectsPool.Instance.spawnDespawnMessages)
        {
            obj.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
        }
        return(obj);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Spawns an object from specified pool
    /// </summary>
    /// <param name="name">Pool name</param>
    /// <param name="pos">Target position</param>
    /// <param name="rot">Target rotation</param>
    /// <returns></returns>
    public static GameObject Spawn(string name, Vector3 pos, Quaternion rot)
    {
        QuickPool targetPool = ObjectsPool.Instance.pools.Where(pool => pool.poolName == name).FirstOrDefault();

        if (targetPool == null)
        {
            return(null);
        }

        GameObject obj = targetPool.GetItem();

        if (obj == null)
        {
            if (ObjectsPool.Instance.debugMessages)
            {
                Debug.Log("No such object left");
            }
            return(null);
        }

        obj.SetActive(true);
        obj.transform.position = pos;
        obj.transform.rotation = rot;

        if (ObjectsPool.Instance.spawnDespawnMessages)
        {
            obj.SendMessage("OnSpawn", SendMessageOptions.DontRequireReceiver);
        }

        return(obj);
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Hides object
    /// </summary>
    /// <param name="target">Target</param>
    public static void Despawn(GameObject target)
    {
        if (ObjectsPool.Instance.spawnDespawnMessages)
        {
            target.SendMessage("OnDespawn", SendMessageOptions.DontRequireReceiver);
        }

        QuickPool targetPool = ObjectsPool.Instance.pools.Where(pool => pool.spawned.Contains(target)).FirstOrDefault();

        targetPool.PushItem(target);
    }
Ejemplo n.º 4
0
    private void PoolArea(QuickPool pool)
    {
        GUILayout.BeginVertical(poolBackground);
        GUILayout.BeginHorizontal(EditorStyles.toolbar);
        GUILayout.Space(10f);

        pool.foldout = EditorGUILayout.Foldout(pool.foldout, pool.poolName);
        GUILayout.FlexibleSpace();

        if (Application.isPlaying)
        {
            GUILayout.Label("Spawned: " + pool.spawnedCount + "/" + pool.totalCount);
        }

        if (GUILayout.Button("Clear", EditorStyles.toolbarButton, GUILayout.Width(80)))
        {
            pool.ClearAndDestroy();
        }

        if (GUILayout.Button("Preinstantiate", EditorStyles.toolbarButton, GUILayout.Width(80)))
        {
            pool.PreInstantiate();
        }

        GUI.color = Color.red;
        if (GUILayout.Button("-", EditorStyles.toolbarButton, GUILayout.Width(15)))
        {
            pool.ClearAndDestroy();
            if (pool.m_Root != null)
            {
                GameObject.DestroyImmediate(pool.m_Root.gameObject);
            }
            poolManager.pools.Remove(pool);
        }

        GUI.color = Color.white;

        GUILayout.EndHorizontal();

        if (pool.foldout)
        {
            pool.Prefab        = EditorGUILayout.ObjectField("Prefab: ", pool.Prefab, typeof(GameObject), false) as GameObject;
            pool.size          = EditorGUILayout.IntField("Pool size: ", pool.size);
            pool.allowMore     = EditorGUILayout.Toggle("Allow more: ", pool.allowMore);
            pool.debugMessages = EditorGUILayout.Toggle("Debug messages: ", pool.debugMessages);
        }

        GUILayout.EndVertical();
    }
Ejemplo n.º 5
0
    private void Toolbar()
    {
        GUILayout.Space(10f);
        DropArea();
        GUILayout.BeginHorizontal(GUI.skin.FindStyle("Toolbar"), GUILayout.ExpandWidth(true));
        GUILayout.Label("Pools (" + poolManager.pools.Count + ")");
        SearchField();

        if (GUILayout.Button("Expand All", EditorStyles.toolbarButton, GUILayout.Width(65)))
        {
            poolManager.pools.ForEach(pool => pool.foldout = true);
        }

        if (GUILayout.Button("Collapse All", EditorStyles.toolbarButton, GUILayout.Width(71)))
        {
            poolManager.pools.ForEach(pool => pool.foldout = false);
        }

        GUILayout.EndHorizontal();
        GUILayout.BeginVertical(background);

        var results = searchStr == "" ? poolManager.pools : poolManager.pools.Where(pool => pool.Prefab.name.Contains(searchStr)).ToList();

        for (int i = 0; i < results.Count; i++)
        {
            QuickPool pool = results[i];

            GUILayout.BeginHorizontal();
            GUILayout.Space(10f);
            GUILayout.BeginVertical();
            PoolArea(pool);
            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
        }

        GUILayout.EndVertical();
    }