Version: Unity 6 Beta (6000.0)
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;
        }
    }
}
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961