「Unity/Editor/inspector値更新」の版間の差分
提供: 初心者エンジニアの簡易メモ
行41: | 行41: | ||
var num = serializedObject.FindProperty("num"); | var num = serializedObject.FindProperty("num"); | ||
var testObject = serializedObject.FindProperty("testObject"); | var testObject = serializedObject.FindProperty("testObject"); | ||
− | |||
− | |||
num.intValue = 3; | num.intValue = 3; | ||
testObject.objectReferenceValue = GameObject.Find("TestObject"); | testObject.objectReferenceValue = GameObject.Find("TestObject"); | ||
− | |||
+ | // 配列 | ||
+ | var users = serializedObject.FindProperty("users"); | ||
+ | SerializedProperty user; | ||
+ | if (users.arraySize == 0) | ||
+ | { | ||
+ | users.InsertArrayElementAtIndex(0); | ||
+ | user = users.GetArrayElementAtIndex(0); | ||
+ | user.objectReferenceValue = GameObject.Find("User"); | ||
+ | } | ||
+ | |||
EditorGUILayout.PropertyField(num); | EditorGUILayout.PropertyField(num); | ||
EditorGUILayout.PropertyField(testObject); | EditorGUILayout.PropertyField(testObject); |
2022年2月23日 (水) 01:01時点における版
Inspector更新サンプル
以下メンバを更新するサンプル
- testObjectに、TestObjectをいれる。
- numに、3をいれる。
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(); var num = serializedObject.FindProperty("num"); var testObject = serializedObject.FindProperty("testObject"); num.intValue = 3; testObject.objectReferenceValue = GameObject.Find("TestObject"); // 配列 var users = serializedObject.FindProperty("users"); SerializedProperty user; if (users.arraySize == 0) { users.InsertArrayElementAtIndex(0); user = users.GetArrayElementAtIndex(0); user.objectReferenceValue = GameObject.Find("User"); } EditorGUILayout.PropertyField(num); EditorGUILayout.PropertyField(testObject); EditorGUILayout.PropertyField(users); serializedObject.ApplyModifiedProperties(); } }