Snippets

Johannes P. Demo Code - https://bitbucket.org/sirenix/odin-inspector/issues/141

Created by Johannes P. last modified
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Sirenix.OdinInspector;
using Sirenix.Utilities.Editor;


public class Demo : SerializedMonoBehaviour
{
    [ListDrawerSettings(HideAddButton = true, OnTitleBarGUI = "RenderAddButton")]
    public List<DemoElement> DemoList;

    [System.NonSerialized] private int _controlID;
    [System.NonSerialized] private bool _pickerActive = false;

    // Custom List add button that opens object picker
    void RenderAddButton()
    {
        if (SirenixEditorGUI.ToolbarButton(EditorIcons.Checkmark))
        {
            _controlID = GUIUtility.GetControlID(FocusType.Passive);
            _pickerActive = true;
            EditorGUIUtility.ShowObjectPicker<Material>(null, false, "", _controlID);
        }
    }

    // Handle if we have the picked object
    [OnInspectorGUI, PropertyOrder(int.MaxValue)]
    void HandlePickedObject()
    {
        if (HavePickedObject())
        {
            Material mat = EditorGUIUtility.GetObjectPickerObject() as Material;
            if (mat == null)
                Debug.Log("INVALID OBJECT");
            else
            {
                Debug.Log("VALID OBJECT!");
                DemoList.Add(new DemoElement(mat));
            }
        }
    }

    // Check if we have a valid item from the object picker
    bool HavePickedObject()
    {
        if (Event.current.commandName != "ObjectSelectorClosed") return false;
        if (EditorGUIUtility.GetObjectPickerControlID() != _controlID) return false;
        if (!_pickerActive) return false;
        _pickerActive = false;
        return true;
    }

    [System.Serializable]
    public class DemoElement
    {
        public DemoElement(Material mat) { Mat = mat; }
        [HorizontalGroup, HideLabel, DisplayAsString] public string Data = "Some Other Data";
        [HorizontalGroup, HideLabel] public Material Mat;
    }
}

Comments (0)

HTTPS SSH

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