3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Unity】ScrollRectを継承したクラスのpublic変数をInspectorに表示するEditor拡張

Posted at

確認環境

Unity 2020.1.6fa

内容

ScrollRectを継承して無限スクロール、多重スクロールなどを実装しようとしたときに継承先のpublicやSerializeFieldの変数がInspectorに表示されなかったのでEditor拡張で表示させてみました。

CustomScrollRect.cs

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Custom.UI
{

    public class CustomScrollRect : ScrollRect
    {
        public Transform test;
        public GameObject hoge01;
        [SerializeField]private GameObject hoge02;
        public GameObject[] hoge03;
        public List<GameObject> hoge04;
    }

}

testやhoge01などが表示されていない。
image.png


ScrollRectなどは独自のEditor拡張されているので表示されないみたい。
ということで考えたらScrollRectEditor.csを継承したEditor拡張を作ればうまくいくと思いやってみました。

CustomScrollRectEditor.cs
using UnityEngine;
using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.UI;
using Custom.UI;

namespace Custom
{

    [CanEditMultipleObjects]
    [CustomEditor(typeof(CustomScrollRect), true)]
    public class CustomScrollRectEditor : ScrollRectEditor
    {
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            if (serializedObject.targetObject is CustomScrollRect customScrollRect)
            {
                Type classType = customScrollRect.GetType();
                FieldInfo[] fieldInfos = classType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                for (int i = 0; i < fieldInfos.Length; i++)
                {
                    FieldInfo fieldInfo = fieldInfos[i];
                    object[] attributes = fieldInfo.GetCustomAttributes(true);
                    bool isSerializeField = false;
                    foreach (var attribute in attributes)
                    {
                        if (attribute.GetType() == typeof(SerializeField))
                        {
                            isSerializeField = true;
                            break;
                        }
                    }
                    if (fieldInfo.Attributes != FieldAttributes.Public && !isSerializeField) continue;
                    SerializedProperty serializedProperty = serializedObject.FindProperty(fieldInfo.Name);
                    EditorGUILayout.PropertyField(serializedProperty);
                }
            }

            serializedObject.ApplyModifiedProperties();

            // 区切り線
            GUILayout.Box("", GUILayout.ExpandWidth(true), GUILayout.Height(2));

            base.OnInspectorGUI();


        }
    }
}

image.png

しっかり元のScrollRectの表示も維持してうまく表示されました。

最後に

ほかにもすでにEditor拡張されているクラスを継承したときにはこの方法を使えばうまく表示されると思います。

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?