Search Unity

Multi-Object Editing

Discussion in 'Scripting' started by allebrandt, Nov 12, 2012.

  1. allebrandt

    allebrandt

    Joined:
    Oct 29, 2012
    Posts:
    18
    Hey guys!

    I'm trying to create a editor script that uses the multi-object editing from Unity, like here http://docs.unity3d.com/Documentation/Manual/Multi-ObjectEditing.html. When I've many objects selected with the same component applied to them and with differente values, I'll have a dash on the field to representate this.

    I've been using SerializeObject and SerializeField in my script, but looks to be not enough.

    Here is a short sample of code of a script with the editor extension that shows how I use SerializeObject and SerializeField.

    MonoBehaviour Script:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour
    5. {
    6.     public float Speed = 10.0f;
    7. }

    Editor Script:
    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(Test)), CanEditMultipleObjects]
    6. public class TestInspector : Editor
    7. {
    8.     private SerializedObject m_Object;
    9.     private SerializedProperty m_Speed;
    10.  
    11.     private void OnEnable()
    12.     {
    13.        m_Object = new SerializedObject(target as Test);
    14.        m_Speed = m_Object.FindProperty("Speed");
    15.     }
    16.  
    17.     public override void OnInspectorGUI()
    18.     {
    19.        m_Object.Update();
    20.        EditorGUILayout.PropertyField(m_Speed);
    21.        m_Object.ApplyModifiedProperties();
    22.     }
    23. }
     
    SamFernGamer4k likes this.
  2. Democre

    Democre

    Joined:
    Mar 31, 2010
    Posts:
    345
    try this instead:
    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(Test)), CanEditMultipleObjects]
    6. public class TestInspector : Editor
    7. {
    8.     private SerializedProperty m_Speed;
    9.  
    10.     private void OnEnable ()
    11.     {
    12.         m_Speed = serializedObject.FindProperty ("Speed");
    13.     }
    14.  
    15.     public override void OnInspectorGUI ()
    16.     {
    17.         serializedObject.Update ();
    18.         EditorGUILayout.PropertyField (m_Speed);
    19.         serializedObject.ApplyModifiedProperties ();
    20.     }
    21. }
    I believe using the "target" object only picks the last selected object as the value to use/display. In other words, don't create a new SerializedObject based on target, but rather use the builtin serializedObject member.

    I guess you could also create a SerializedObject based on "targets" instead, but I don't see the point when you already have serializedObject as a member.
     
  3. allebrandt

    allebrandt

    Joined:
    Oct 29, 2012
    Posts:
    18
    Both ways worked to me...
    Using "targets" instead of "target" and using the builtin serializedObject member...

    Thanks Democre!
     
    flashframe likes this.
  4. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    This works:

    Code (CSharp):
    1.  
    2. [CustomEditor(typeof(MyMonoScript)), CanEditMultipleObjects]
    3. public class MyMonoScript_Editor : Editor {
    4.  
    5.     private MyMonoScript[] myMonoScript;
    6.  
    7.     private Vector3 startingPoint;
    8.  
    9.     private void OnEnable() {
    10.         Object[] monoObjects = targets;
    11.         myMonoScript = new MyMonoScript[monoObjects.Length];
    12.         for (int i = 0; i < monoObjects.Length; i++) {
    13.             myMonoScript[i] = monoObjects[i] as MyMonoScript;
    14.         }
    15.     }
    16.  
    17.     private void OnSceneGUI() {
    18.         for (int i = 0; i < myMonoScript.Length; i++) {
    19.             Debug.Log(myMonoScript[i].transform.position.x);
    20.         }
    21.     }
    22. }
     
    r00f likes this.