如果提示用户保存,则显示的消息。
在派生类中设置 saveChangesMessage 以防止用户意外丢失未保存的工作。为了使 saveChangesMessage 起作用,您必须将其与 Editor.hasUnsavedChanges 一起使用并覆盖 Editor.SaveChanges 方法。此消息按您编写的方式显示。此消息呈现给具有未保存更改的用户,如果他们尝试关闭 Inspector、更改选择或进入 Playmode。保存更改消息可能会与来自其他编辑器的其他消息合并。如果有多个编辑器具有未保存的更改,就会发生这种情况。
using UnityEngine; using UnityEditor;
[CreateAssetMenu] public class UnsavedChangesExampleSO : ScriptableObject {}
[CustomEditor(typeof(UnsavedChangesExampleSO))] public class UnsavedChangesExampleEditor : UnityEditor.Editor { void OnEnable() { saveChangesMessage = "This editor has unsaved changes. Would you like to save?"; }
void OnInspectorGUI() { saveChangesMessage = EditorGUILayout.TextField(saveChangesMessage);
EditorGUILayout.LabelField(hasUnsavedChanges ? "I have changes!" : "No changes.", EditorStyles.wordWrappedLabel); EditorGUILayout.LabelField("Try to change selection.");
using (new EditorGUI.DisabledScope(hasUnsavedChanges)) { if (GUILayout.Button("Create unsaved changes")) hasUnsavedChanges = true; }
using (new EditorGUI.DisabledScope(!hasUnsavedChanges)) { if (GUILayout.Button("Save")) SaveChanges();
if (GUILayout.Button("Discard")) DiscardChanges(); } }
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(); } }