facebook twitter hatena line email

「Unity/Editor/inspector値更新」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行10: 行10:
 
public class SampleScene : MonoBehaviour
 
public class SampleScene : MonoBehaviour
 
{
 
{
    [SerializeField] GameObject testObject;
 
 
     [SerializeField] int num;
 
     [SerializeField] int num;
 +
    [SerializeField] GameObject testObject;
 +
    [SerializeField] List<GameObject> users;
 
     // Start is called before the first frame update
 
     // Start is called before the first frame update
 
     void Start()
 
     void Start()
行38: 行39:
 
         serializedObject.Update();
 
         serializedObject.Update();
  
        var testObject = serializedObject.FindProperty("testObject");
 
 
         var num = serializedObject.FindProperty("num");
 
         var num = serializedObject.FindProperty("num");
 +
        var testObject = serializedObject.FindProperty("testObject");
 +
        var users = serializedObject.FindProperty("users");
 +
        var user = users.GetArrayElementAtIndex(0);
  
 
         num.intValue = 3;
 
         num.intValue = 3;
 
         testObject.objectReferenceValue = GameObject.Find("TestObject");
 
         testObject.objectReferenceValue = GameObject.Find("TestObject");
 +
        user.objectReferenceValue = GameObject.Find("User");
 
          
 
          
        EditorGUILayout.PropertyField(testObject);
 
 
         EditorGUILayout.PropertyField(num);
 
         EditorGUILayout.PropertyField(num);
 +
        EditorGUILayout.PropertyField(testObject);
 +
        EditorGUILayout.PropertyField(users);
  
 
         serializedObject.ApplyModifiedProperties();
 
         serializedObject.ApplyModifiedProperties();
行52: 行57:
 
</pre>
 
</pre>
 
参考:https://www.hanachiru-blog.com/entry/2020/10/19/120000#SerializedObject%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E3%82%84%E3%82%8A%E6%96%B9
 
参考:https://www.hanachiru-blog.com/entry/2020/10/19/120000#SerializedObject%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E3%82%84%E3%82%8A%E6%96%B9
 +
 +
参考:https://qiita.com/albatrus/items/b9799f7f2a805faf3da8

2022年2月23日 (水) 00:25時点における版

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");
        var users = serializedObject.FindProperty("users");
        var user = users.GetArrayElementAtIndex(0);

        num.intValue = 3;
        testObject.objectReferenceValue = GameObject.Find("TestObject");
        user.objectReferenceValue = GameObject.Find("User");
        
        EditorGUILayout.PropertyField(num);
        EditorGUILayout.PropertyField(testObject);
        EditorGUILayout.PropertyField(users);

        serializedObject.ApplyModifiedProperties();
    }
}

参考:https://www.hanachiru-blog.com/entry/2020/10/19/120000#SerializedObject%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E3%82%84%E3%82%8A%E6%96%B9

参考:https://qiita.com/albatrus/items/b9799f7f2a805faf3da8