版本:Unity 6(6000.0)
语言英语
  • C#

EditorAction.OnSceneGUI

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。尽管我们无法接收所有提交内容,但我们确实会阅读用户建议的每项内容,并在适用处进行更新。

关闭

提交失败

由于某些原因,无法提交您的建议更改。请在几分钟后<a>重试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public void OnSceneGUI(SceneView sceneView);

参数

sceneView 调用 Actions.EditorAction.OnSceneGUI 的 Scene 视图。

说明

在 Scene 视图调用 OnGUI 时引发的回调。

使用此方法实现任何手柄或交互式代码。这在功能上等效于 EditorTool.OnToolGUI

using System;
using UnityEditor;
using UnityEditor.Actions;
using UnityEditor.Overlays;
using UnityEngine;
using UnityEngine.UIElements;

public class EditorActionSampleOverlay : Overlay
{
    Vector3Field m_Field;

    public Action<Vector3> positionChanged;

    public void SetPosition(Vector3 position)
    {
        m_Field?.SetValueWithoutNotify(position);
    }

    public override VisualElement CreatePanelContent()
    {
        m_Field = new Vector3Field();
        m_Field.RegisterValueChangedCallback((evt) => positionChanged?.Invoke(evt.newValue));
        return m_Field;
    }
}

public class EditorActionSample : EditorAction
{
    [MenuItem("Test/Start Action Sample")]
    static void StartEditorActionSample()
    {
        Start(new EditorActionSample());
    }

    public Vector3 handlePosition = Vector3.zero;

    EditorActionSampleOverlay m_Overlay;

    public EditorActionSample()
    {
        // Create the overlay when the action is created
        m_Overlay = new EditorActionSampleOverlay();
        m_Overlay.SetPosition(handlePosition);
        m_Overlay.positionChanged += (value) => handlePosition = value;
        SceneView.AddOverlayToActiveView(m_Overlay);
        m_Overlay.displayed = true;
    }

    protected override void OnFinish(EditorActionResult result)
    {
        // Remove the overlay when the action is finished
        SceneView.RemoveOverlayFromActiveView(m_Overlay);

        Debug.Log($"Action Finished [{result}] with position: {handlePosition}");
    }

    public override void OnSceneGUI(SceneView sceneView)
    {
        EditorGUI.BeginChangeCheck();
        handlePosition = Handles.PositionHandle(handlePosition, Quaternion.identity);

        if (EditorGUI.EndChangeCheck())
        {
            m_Overlay.SetPosition(handlePosition);
        }
    }
}