LoginSignup
0
1

More than 3 years have passed since last update.

[Editor拡張] InspectorにUI取得ボタンを表示して押したらUIを取得

Last updated at Posted at 2021-01-06

使用すると下画像のようにInspectorにボタンを追加します。

スクリーンショット 2021-01-07 0.19.35.png

以下 サンプルコード

Sample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Test
{
    public class Sample : MonoBehaviour
    {
#if UNITY_EDITOR
        public void SetInspectorUI()
        {
                // ここに処理を記載
                Debug.Log("On Click");
        }
#endif
    }

#if UNITY_EDITOR
    [CustomEditor(typeof(Sample))]
    public class SampleEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (GUILayout.Button("Set UI"))
            {
                Sample t = target as Sample;
                t.SetInspectorUI();
            }
        }
    }
#endif
}

UIを取得するならこういう書き方かな?というサンプル

Sample.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

namespace Test
{
    public class Sample : MonoBehaviour
    {
        public Text A;
        public Text B;
        public Text C;

#if UNITY_EDITOR
        public void SetInspectorUI()
        {
            var texts = GetComponentsInChildren<Text>();

            foreach (var t in texts)
            {
                if (t.name == "A")
                {
                    A = t;
                }
                else if (t.name == "B")
                {
                    B = t;
                }
                else if (t.name == "C")
                {
                    C = t;
                }
            }
        }
#endif
    }

#if UNITY_EDITOR
    [CustomEditor(typeof(Sample))]
    public class SampleEditor : Editor
    {
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            if (GUILayout.Button("Set UI"))
            {
                Sample t = target as Sample;
                t.SetInspectorUI();
            }
        }
    }
#endif
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1