using System; using System.Collections; using System.Collections.Generic; using System.Security.Cryptography; using TyphoonGUIStyle; using UnityEditor; using UnityEngine; namespace TyphoonUI { [CustomEditor(typeof(RectTransformLayoutSelector))] public class RectTransformLayoutSelectorEditor : Editor { public RectTransformLayoutSelector Self => target as RectTransformLayoutSelector; private int _layoutIndex = -1; private InputType _inputType = InputType.None; private enum InputType { None, UpdateLayout, RemoveLayout, PreviewLayout, } private void OnEnable() { } public override void OnInspectorGUI() { if (Self.Root == null) { GUILayout.Label(new GUIContent("缺少 Root", Styles.IconError), Styles.HelpBox); } base.OnInspectorGUI(); var maskString = Self.GetCullMaskString(); GUILayout.BeginHorizontal(); GUILayout.Label("CullingMask"); if (GUILayout.Button(maskString, "PopUp")) { var self = Self; var menu = new GenericMenu(); menu.AddItem(new GUIContent("Nothing"), false, () => { self.Nothing(); EditorUtility.SetDirty(self); }); menu.AddItem(new GUIContent("Everything"), false, () => { self.Everything(); EditorUtility.SetDirty(self); }); var propertyTypes = RectTransformLayoutSelector.PropertyTypes; foreach (var type in propertyTypes) { var isOn = self.IsContain(type); menu.AddItem(new GUIContent($"{type}"), isOn, () => { self.SetMask(type, !isOn); EditorUtility.SetDirty(self); }); } menu.ShowAsContext(); } GUILayout.EndHorizontal(); //绘制布局 if (GUILayout.Button("+记录布局", Styles.BtnGreen, GUILayout.Width(100))) { Self.AddLayout(); EditorUtility.SetDirty(Self); } GUILayout.Space(5); //预览布局 GUILayout.Label("快捷预览", Styles.BoldLabel); var layouts = Self.Layouts; var index = 0; foreach (var layout in layouts) { GUILayout.BeginHorizontal(); if (GUILayout.Button($"[{index + 1}]{layout.Name}", Styles.BtnGreenLeft)) { PreviewLayout(index); } if (GUILayout.Button("更新布局", Styles.BtnBlue, GUILayout.Width(80))) { UpdateLayout(index); } if (GUILayout.Button("移除", Styles.BtnRed, GUILayout.Width(60))) { RemoveLayout(index); } GUILayout.EndHorizontal(); index += 1; } HandleInput(); Repaint(); } private void ShowMessageBox(string content, Action ok) { var click = EditorUtility.DisplayDialog("提示", content, "是"); if (click) { ok?.Invoke(); } } //更新layout private void UpdateLayout(int index) { _inputType = InputType.UpdateLayout; _layoutIndex = index; } //移除layout private void RemoveLayout(int index) { _inputType = InputType.RemoveLayout; _layoutIndex = index; } //预览布局 private void PreviewLayout(int index) { _inputType = InputType.PreviewLayout; _layoutIndex = index; } private void HandleInput() { var type = _inputType; _inputType = InputType.None; switch (type) { case InputType.RemoveLayout: { var layout = Self.Layouts[_layoutIndex]; ShowMessageBox($"移除布局:{layout.Name}?", () => { Self.RemoveLayout(_layoutIndex); EditorUtility.SetDirty(Self); }); } break; case InputType.UpdateLayout: { var layout = Self.Layouts[_layoutIndex]; ShowMessageBox($"更新布局到:{layout.Name}?", () => { Self.UpdateLayout(_layoutIndex); EditorUtility.SetDirty(Self); }); } break; case InputType.PreviewLayout: { var layout = Self.Layouts[_layoutIndex]; Self.SetLayout(_layoutIndex); EditorUtility.SetDirty(Self); } break; } } } }