Snippets

Jones Cropper Multi Object Pool Picker

Created by Jones Cropper
#region Author
/*
     
     Jones St. Lewis Cropper (caLLow)
     
     Another caLLowCreation
     
     Visit us on Google+ and other social media outlets @caLLowCreation
     
     Thanks for using our product.
     
     Send questions/comments/concerns/requests to 
      e-mail: caLLowCreation@gmail.com
      subject: PoolEverything
     
*/
#endregion

#region Author
/*
     
     Jones St. Lewis Cropper (caLLow)
     
     Another caLLowCreation
     
     Visit us on Google+ and other social media outlets @caLLowCreation
     
     Thanks for using our product.
     
     Send questions/comments/concerns/requests to 
      e-mail: caLLowCreation@gmail.com
      subject: PoolEverything
     
*/
#endregion

using UnityEngine;
using System.Collections.Generic;

namespace PoolEverything.Helpers
{
    public class MultiObjectPool : MonoBehaviour
    {
        [SerializeField, PoolManagerPicker]
        PoolManager m_PoolManager = null;

        [SerializeField, MultiObjectPoolPicker("m_PoolManager", "Object Id")]
        List<int> m_ObjectIds = null;

        [SerializeField, MultiObjectPoolPicker("m_PoolManager", "Id")]
        int m_ObjectId = -1;
    }
}
#region Author
/*
     
     Jones St. Lewis Cropper (caLLow)
     
     Another caLLowCreation
     
     Visit us on Google+ and other social media outlets @caLLowCreation
     
     Thanks for using our product.
     
     Send questions/comments/concerns/requests to 
      e-mail: caLLowCreation@gmail.com
      subject: PoolEverything
     
*/
#endregion

using UnityEngine;

namespace PoolEverything.Helpers
{
    /// <summary>
    /// Attribute to draw a Pool Manager and index popup to replace the integer field allows for multiple object selections
    /// </summary>
    public class MultiObjectPoolPickerAttribute : PropertyAttribute
    {
        /// <summary>
        /// Name of PoolManager field
        /// </summary>
        public readonly string poolManagerName;
        /// <summary>
        /// Label to replace the default field name
        /// </summary>
        public readonly string label;
        /// <summary>
        /// Constructor
        /// </summary>
        public MultiObjectPoolPickerAttribute(string poolManagerName = "m_PoolManager", string label = "Pool Object")
        {
            this.poolManagerName = poolManagerName;
            this.label = label;
        }
    }
}
#region Author
/*
     
     Jones St. Lewis Cropper (caLLow)
     
     Another caLLowCreation
     
     Visit us on Google+ and other social media outlets @caLLowCreation
     
     Thanks for using our product.
     
     Send questions/comments/concerns/requests to 
      e-mail: caLLowCreation@gmail.com
      subject: PoolEverything
     
*/
#endregion

using UnityEditor;
using UnityEngine;
using System.Linq;
using PoolEverything.Helpers;
using PoolEverything;

namespace PoolEverythingEditor
{
    /// <summary>
    /// Custom editor to draw popup to pick a pool manager and pooled object
    /// </summary>
    [CustomPropertyDrawer(typeof(MultiObjectPoolPickerAttribute), true)]
    [CanEditMultipleObjects]
    public class MultiObjectPoolPickerDrawer : PropertyDrawer
    {
        int m_PoolKey = -1;

        /// <summary>
        /// Called to draw the proeprty
        /// </summary>
        /// <param name="position">Rectangle position to draw property</param>
        /// <param name="property">Peoperty passed in by the calss field</param>
        /// <param name="label">Name and toolip of field property</param>
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            MultiObjectPoolPickerAttribute attr = attribute as MultiObjectPoolPickerAttribute;
            SerializedProperty poolManagerProperty = property.serializedObject.FindProperty(attr.poolManagerName);
            PoolManager poolManager = poolManagerProperty.objectReferenceValue as PoolManager;
            int poolIndex = property.intValue;
            for (int i = 0; poolManager && poolManager.poolReferences != null && i < poolManager.poolReferences.Length; i++)
            {
                if (poolIndex == i)
                {
                    m_PoolKey = i;
                    break;
                }
            }
            if (poolManager)
            {
                var prefabs = poolManager.poolReferences;
                if (prefabs == null || poolManager.poolReferences.Where(x => x.reference).Count() == 0)
                {
                    var helpRect = position;
                    helpRect.x = 100;
                    helpRect.width -= 86;
                    EditorGUI.HelpBox(helpRect, "No pools in manager.", MessageType.Info);
                    var btnRect = position;
                    btnRect.x += 10;
                    btnRect.width = 74;
                    if (GUI.Button(btnRect, "Add Now"))
                    {
                        Selection.activeObject = poolManager;
                    }
                }
                else
                {
                    var guiContents = prefabs.Select(x => new GUIContent(x != null && x.reference ? x.reference.name : "")).ToArray();
                    m_PoolKey = EditorGUI.Popup(position, new GUIContent(attr.label), m_PoolKey, guiContents);
                }
                if (GUI.changed)
                {
                    property.intValue = m_PoolKey;
                }
            }
            else
            {
                EditorGUI.HelpBox(position, "Select a Pool Manager", MessageType.Info);
                m_PoolKey = -1;
            }
        }
    }

}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.