Version: 2022.3

EditorGUI.BeginChangeCheck

切换到手册
public static void BeginChangeCheck ();

描述

启动一个新代码块来检查 GUI 更改。

将它与 EditorGUI.EndChangeCheck 结合使用来创建一个代码块,检查是否只有该代码块中包含的控件的 GUI 状态发生了更改。
此方法不同于 GUI.changed,后者对于 GUI 状态的“任何”更改都返回 true。BeginChangeCheck() 将检查限制为一组特定的控件。

using UnityEditor;

public class ExampleWindow : EditorWindow { float sliderValue = 0; string labelText = "-";

[MenuItem("Window/Example Window")] static void Init() { var example = (ExampleWindow)EditorWindow.GetWindow(typeof(ExampleWindow)); example.Show(); }

void OnGUI() { EditorGUILayout.LabelField("New value", labelText);

// Start a code block to check for GUI changes EditorGUI.BeginChangeCheck();

sliderValue = EditorGUILayout.Slider(sliderValue, 0, 1);

// End the code block and update the label if a change occurred if (EditorGUI.EndChangeCheck()) { labelText = sliderValue.ToString(); } } }