Unity/Editor/inspector値更新
提供: 初心者エンジニアの簡易メモ
Inspector更新サンプル
以下メンバを更新するサンプル
- testObjectに、TestObjectをいれる。
- numに、3をいれる。
- usersにGameObjectをいれる。
SampleScene.cs
using UnityEngine; public class SampleScene : MonoBehaviour { [SerializeField] int num; [SerializeField] GameObject testObject; [SerializeField] List<GameObject> users; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } }
SampleSceneUpdateEditor.cs
using UnityEngine; using UnityEditor; [CustomEditor(typeof(SampleScene))] public class SampleSceneUpdateEditor : Editor { public override void OnInspectorGUI() { serializedObject.Update(); // num var num = serializedObject.FindProperty("num"); num.intValue = 3; EditorGUILayout.PropertyField(num); // testObject var testObject = serializedObject.FindProperty("testObject"); testObject.objectReferenceValue = GameObject.Find("TestObject"); EditorGUILayout.PropertyField(testObject); // users配列 var users = serializedObject.FindProperty("users"); if (users.arraySize == 0) { users.InsertArrayElementAtIndex(0); } var user = users.GetArrayElementAtIndex(0); user.objectReferenceValue = GameObject.Find("User"); EditorGUILayout.PropertyField(users); serializedObject.ApplyModifiedProperties(); } }