版本:Unity 6 (6000.0)
语言:英语
创建拖放式 UI 以在编辑器窗口之间拖动
创建循环过渡

创建过渡事件

版本: 2021.3+

此示例演示了过渡事件的生命周期。

示例概述

该示例创建一个自定义编辑器窗口,其中包含一个按钮和一个调色板。如果单击按钮,则会显示以下内容

  • 过渡开始将调色板从蓝色更改为绿色。
  • 过渡期间发送的过渡事件。
  • 事件的持续时间。

您可以在此 GitHub 存储库 中找到此示例创建的完整文件。

先决条件

本指南适用于熟悉 Unity 编辑器、UI(用户界面) 允许用户与您的应用程序交互。Unity 目前支持三种 UI 系统。 更多信息
参见 术语表
工具包和 C# 脚本的开发人员。在开始之前,请熟悉以下内容

创建示例

  1. 在 Unity 中使用任何模板创建一个项目。

  2. 从菜单中选择 **资产** > **创建** > **UI 工具包** > **编辑器窗口**。

  3. 在 **UI 工具包编辑器窗口创建器** 窗口中,输入 TransitionExample

  4. 保存您的更改。这将创建三个文件,分别是 TransitionExample.csTransitionExample.ussTransitionExample.uxml

  5. TransitionExample.cs 替换为以下内容

    using System;
    using UnityEditor;
    using UnityEngine;
    using UnityEngine.UIElements;
    
    public class TransitionExample : EditorWindow
    {
        [SerializeField] private VisualTreeAsset m_VisualTreeAsset = default;
    
        private Button clickMeButton;
        private VisualElement colorChanger;
        private Label eventLabel;
        private Label timeLabel;
    
        private DateTime lastEvent;
        private static readonly TimeSpan NearlyInstantaneousThreshold = TimeSpan.FromMilliseconds(10);
    
        private static readonly string ClickMeButtonClass = "click-me";
        private static readonly string ColorChangerClass = "color-changer";
        private static readonly string ColorChangerTransitionClass = "color-transition";
        private static readonly string EventLabelName = "eventLabel";
        private static readonly string TimeLabelName = "timeLabel";
        private static readonly string TimeBelowThresholdText = "Almost instantaneous.";
    
        [MenuItem("Window/UI Toolkit/Transition Example")]
        public static void ShowExample()
        {
            TransitionExample wnd = GetWindow<TransitionExample>();
            wnd.titleContent = new GUIContent("TransitionExample");
            wnd.minSize = new Vector2(500f, 400f);
        }
    
        public void CreateGUI()
        {
            lastEvent = DateTime.Now;
    
            // Each editor window contains a root VisualElement object
            VisualElement root = rootVisualElement;
    
            // Instantiate UXML
            VisualElement uxmlAsset = m_VisualTreeAsset.Instantiate();
            root.Add(uxmlAsset);
    
            // Get the relevant elements by querying the root element
            clickMeButton = root.Q<Button>(className: ClickMeButtonClass);
    
            colorChanger = root.Q<VisualElement>(className: ColorChangerClass);
    
            eventLabel = root.Q<Label>(name: EventLabelName);
    
            timeLabel = root.Q<Label>(name: TimeLabelName);
    
            // Add callbacks for clicking on the button and monitoring the color changing element.
            clickMeButton.RegisterCallback<ClickEvent>(OnClickEvent);
    
            colorChanger.RegisterCallback<TransitionRunEvent>(OnTransitionRun);
            colorChanger.RegisterCallback<TransitionStartEvent>(OnTransitionStart);
            colorChanger.RegisterCallback<TransitionEndEvent>(OnTransitionEnd);
            colorChanger.RegisterCallback<TransitionCancelEvent>(OnTransitionCancel);
        }
    
        private void OnDisable()
        {
            clickMeButton.UnregisterCallback<ClickEvent>(OnClickEvent);
    
            colorChanger.UnregisterCallback<TransitionRunEvent>(OnTransitionRun);
            colorChanger.UnregisterCallback<TransitionStartEvent>(OnTransitionStart);
            colorChanger.UnregisterCallback<TransitionEndEvent>(OnTransitionEnd);
            colorChanger.UnregisterCallback<TransitionCancelEvent>(OnTransitionCancel);
        }
    
        private void OnClickEvent(ClickEvent evt)
        {
            colorChanger.ToggleInClassList(ColorChangerTransitionClass);
        }
    
        private void OnTransitionRun(TransitionRunEvent evt)
        {
            DisplayLatestEvent("TransitionRunEvent", DateTime.Now);
        }
    
        private void OnTransitionStart(TransitionStartEvent evt)
        {
            DisplayLatestEvent("TransitionStartEvent", DateTime.Now);
        }
    
        private void OnTransitionEnd(TransitionEndEvent evt)
        {
            DisplayLatestEvent("TransitionEndEvent", DateTime.Now);
        }
    
        private void OnTransitionCancel(TransitionCancelEvent evt)
        {
            DisplayLatestEvent("TransitionCancelEvent", DateTime.Now);
        }
    
        private void DisplayLatestEvent(string eventType, DateTime timestamp)
        {
            // If two events are sent too close together, add both to the Latest event line.
            // This happens if the delay is set to 0 and the TransitionRun and TransitionStart
            // are sent at the same time, or if the button was pressed before the transition
            // was over, thus sending TransitionCancel and TransitionRun (and potentially
            // TransitionStart) events close together.
            var elapsed = timestamp - lastEvent;
            if (elapsed <= NearlyInstantaneousThreshold)
            {
                timeLabel.text = TimeBelowThresholdText;
                eventLabel.text += eventType;
            }
            else
            {
                timeLabel.text = $"{elapsed:s\\.ff} s";
                eventLabel.text = eventType;
            }
    
            lastEvent = timestamp;
        }
    }
    
  6. TransitionExample.uxml 替换为以下内容

    <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements"
            xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements"
            noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
        <Style src="TransitionExample.uss"/>
        <ui:VisualElement class="main-container">
            <ui:Button text="Click Me!" class="click-me"/>
            <ui:VisualElement class="color-changer"/>
            <ui:VisualElement class="label-section">
                <ui:VisualElement class="label-line">
                    <ui:Label text="Latest event(s) :  "/>
                    <ui:Label name="eventLabel"/>
                </ui:VisualElement>
                <ui:VisualElement class="label-line">
                    <ui:Label text="Time since last event :  "/>
                    <ui:Label name="timeLabel"/>
                </ui:VisualElement>
            </ui:VisualElement>
        </ui:VisualElement>
    </ui:UXML>
    
  7. TransitionExample.uss 替换为以下内容

    .click-me {
        width: 250px;
        height: 50px;
        font-size: 40px;
        -unity-font-style: bold-and-italic;
        margin: 30px;
    }
    
    .color-changer {
        margin: 10px;
        width: 150px;
        height: 150px;
        border-width: 10px;
        border-radius: 75px;
        background-color: rgb(0, 31, 138);
        transition: background-color 3s ease-in-out 1s;
    }
    
    .main-container {
        align-items: center;
        justify-content: space-between;
        flex-grow: 1;
        background-color: rgb(60, 60, 60);
    }
    
    .label-section {
        margin: 10px;
        border-width: 2px;
        width: 95%;
        align-items: center;
    }
    
    .label-line {
        flex-direction: row;
        margin: 5px;
        flex-grow: 1;
        align-items: center;
        width: 90%;
        height: 25px;
        font-size: 14px;
        padding: 0;
    }
    
    .color-transition {
        background-color: rgb(177, 221, 111);
    }
    
  8. 选择 TransitionExample.cs 并将 TransitionExample.uxml 拖到 **视觉树由轻量级节点组成的对象图,它包含窗口或面板中的所有元素。它定义了您使用 UI 工具包构建的每个 UI。
    参见 术语表
    资产** 字段,如果尚未存在,请将其拖到 检查器一个 Unity 窗口,显示有关当前选定游戏对象、资产或项目设置的信息,允许您检查和编辑这些值。 更多信息
    参见 术语表
    中。

  9. 要试用该示例,从菜单中选择 **窗口** > **UI 工具包** > **过渡示例**。

  10. 单击按钮以查看颜色过渡和发送事件的描述。TransitionRunEventTransitionStartEvent 之间存在很长的延迟。在长时间内,您可以再次单击按钮以中断过渡。

其他资源

创建拖放式 UI 以在编辑器窗口之间拖动
创建循环过渡