Version: 2021.3
LanguageEnglish
  • C#

EditorWindow.OnInspectorUpdate()

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

OnInspectorUpdate is called at 10 frames per second to give the inspector a chance to update.

// Simple script that aligns the position of several selected GameObjects
// with the first selected one.

using UnityEditor;
using UnityEngine;
using System.Collections;
using UnityEngine.UIElements;

public class OnInspectorUpdateExample : EditorWindow
{
    bool alignToX = true;
    bool alignToY = true;
    bool alignToZ = true;
    string selected = "";
    string alignTo = "";

    [MenuItem("Examples/OnInspectorUpdate example")]
    static void Init()
    {
        OnInspectorUpdateExample window = (OnInspectorUpdateExample)GetWindow(typeof(OnInspectorUpdateExample));
        window.Show();
    }

    void  OnInspectorUpdate()
    {        
        selected = Selection.activeTransform ? Selection.activeTransform.name : "";
    }

    void CreateGUI()
    {
        var label = new Label("Select various Objects in the Hierarchy view to align them with the first selected one.");
        rootVisualElement.Add(label);

        selected = Selection.activeTransform ? Selection.activeTransform.name : "";

        var labelSelected = new Label();
        rootVisualElement.Add(labelSelected);

        labelSelected.schedule.Execute(() =>
        {
            labelSelected.text = $"Selected object: {selected}";
        }).Every(10);

        var toggleX = new Toggle();
        toggleX.text = "X";
        toggleX.value = alignToX;
        toggleX.RegisterValueChangedCallback(evt => alignToX = evt.newValue);
        rootVisualElement.Add(toggleX);

        var toggleY = new Toggle();
        toggleY.text = "Y";
        toggleY.value = alignToY;
        toggleY.RegisterValueChangedCallback(evt => alignToY = evt.newValue);
        rootVisualElement.Add(toggleY);

        var toggleZ = new Toggle();
        toggleZ.text = "Z";
        toggleZ.value = alignToZ;
        toggleZ.RegisterValueChangedCallback(evt => alignToZ = evt.newValue);
        rootVisualElement.Add(toggleZ);

        var buttonAlign = new Button();
        buttonAlign.text = "Align";
        buttonAlign.clicked += () => Align();
        rootVisualElement.Add(buttonAlign);
    }

    void Align()
    {
        if (selected == "")
        {
            Debug.LogError("No objects selected to align");
        }

        foreach (Transform t in Selection.transforms)
        {
            Vector3 alignementPosition = Selection.activeTransform.position;
            Vector3 newPosition;

            newPosition.x = alignToX ? alignementPosition.x : t.position.x;
            newPosition.y = alignToY ? alignementPosition.y : t.position.y;
            newPosition.z = alignToZ ? alignementPosition.z : t.position.z;

            t.position = newPosition;
        }
    }
}