如果提示用户保存,则显示的消息
在派生类中设置 saveChangesMessage 以防止用户意外丢失未保存的工作。为了使 saveChangesMessage 起作用,您必须将其与 EditorWindow.hasUnsavedChanges 一起使用,并覆盖 EditorWindow.SaveChanges 方法。此消息将按您编写的原样显示。此消息将显示给具有未保存更改的用户,如果他们尝试关闭具有此 EditorWindow 的窗口或选项卡。保存更改消息可能会与来自其他窗口的其他消息合并。如果有多个窗口具有未保存的更改,就会发生这种情况。
using UnityEngine; using UnityEditor; using UnityEngine.UIElements; public class UnsavedChangesExampleWindow : EditorWindow { [MenuItem("Examples/Editor Window With Unsaved Changes")] static void Init() { UnsavedChangesExampleWindow window = (UnsavedChangesExampleWindow)EditorWindow.GetWindowWithRect(typeof(UnsavedChangesExampleWindow), new Rect(100, 100, 400, 400)); window.saveChangesMessage = "This window has unsaved changes. Would you like to save?"; window.Show(); } void CreateGUI() { var label = new Label(); label.text = hasUnsavedChanges ? "I have changes!" : "No changes."; rootVisualElement.Add(label); var buttonCreate = new Button(); buttonCreate.text = "Create unsaved changes"; buttonCreate.clicked += () => { hasUnsavedChanges = true; Debug.Log($"{this} has unsaved changes!!!"); }; rootVisualElement.Add(buttonCreate); var buttonSave = new Button(); buttonSave.text = "Save"; buttonSave.clicked += () => SaveChanges(); rootVisualElement.Add(buttonSave); var buttonDiscard = new Button(); buttonDiscard.text = "Discard"; buttonDiscard.clicked += () => DiscardChanges(); rootVisualElement.Add(buttonDiscard); } public override void SaveChanges() { // Your custom save procedures here Debug.Log($"{this} saved successfully!!!"); base.SaveChanges(); } public override void DiscardChanges() { // Your custom procedures to discard changes Debug.Log($"{this} discarded changes!!!"); base.DiscardChanges(); } }