EditorWindow.OnInspectorUpdate()

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