using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; public class FloorButtonEditor : EditorWindow { [MenuItem("Tools/楼层面板编辑工具")] public static void FloorButtonWindow() { var window = (FloorButtonEditor)GetWindow(typeof(FloorButtonEditor), true, "楼层面板编辑工具"); window.Show(); } private Vector2 UIScrollow; public string fullpath; public FloorData Data = new FloorData { Floors = new List() }; public bool ShowInfos; public bool LoadData = true; public bool NewData = false; public string Prefix; public int Highest; public int lowest; public string FileName="Floors"; private int ToolbarInt = 0; private readonly string[] ToolbarStrings = { "新建数据", "修改数据" }; private void OnEnable() { fullpath = $"{Application.streamingAssetsPath}/Data/{FileName}.json"; } private void OnGUI() { ToolbarInt = GUILayout.Toolbar(ToolbarInt, ToolbarStrings); #region NewData if (ToolbarInt == 0) { GUILayout.BeginHorizontal("Box"); FileName = EditorGUILayout.TextField("文件名:", FileName); if (GUILayout.Button("SET")) { fullpath = $"{Application.streamingAssetsPath}/data/{FileName}.json"; } GUILayout.EndHorizontal(); EditorGUILayout.LabelField($"存储路径预览:{fullpath}", new GUIStyle() { normal = new GUIStyleState() { textColor = Color.green } }); GUILayout.BeginVertical("Box"); Prefix = EditorGUILayout.TextField("前缀:", Prefix); Highest = EditorGUILayout.IntField("最高层:", Highest); lowest = EditorGUILayout.IntField("最底层:", lowest); GUILayout.BeginHorizontal("Box"); if (GUILayout.Button("保存")) { SetData(); } GUILayout.EndHorizontal(); GUILayout.EndVertical(); } #endregion #region Load Data if (ToolbarInt==1) { UIScrollow = EditorGUILayout.BeginScrollView(UIScrollow, false, false); GUILayout.BeginHorizontal("Box"); FileName = EditorGUILayout.TextField("文件名:", FileName); if (GUILayout.Button("SET")) { fullpath = $"{Application.streamingAssetsPath}/data/{FileName}.json"; } GUILayout.EndHorizontal(); EditorGUILayout.LabelField($"存储路径预览:{fullpath}", new GUIStyle() { normal = new GUIStyleState() { textColor = Color.green } }); GUILayout.BeginHorizontal("Box"); if (GUILayout.Button("获取数据")) { GetData(); } GUILayout.EndHorizontal(); GUILayout.BeginVertical("Box"); Data.Prefix = EditorGUILayout.TextField("前缀:", Data.Prefix); Data.Highest = EditorGUILayout.IntField("最高层:", Data.Highest); Data.Lowest = EditorGUILayout.IntField("最底层:", Data.Lowest); GUILayout.BeginHorizontal("Box"); ShowInfos = EditorGUILayout.Toggle("显示楼层详细信息:", ShowInfos); GUILayout.EndHorizontal(); if (ShowInfos) { for (int i = 0; i < Data.Floors.Count; i++) { GUILayout.BeginHorizontal("Box"); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("按钮名称:"); Data.Floors[i].ButtonName = EditorGUILayout.TextField(Data.Floors[i].ButtonName); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("楼层名称:"); Data.Floors[i].FloorName = EditorGUILayout.TextField(Data.Floors[i].FloorName); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("避难层:"); Data.Floors[i].BNC = EditorGUILayout.Toggle(Data.Floors[i].BNC); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("防火分区:"); Data.Floors[i].FHFQ = EditorGUILayout.Toggle(Data.Floors[i].FHFQ); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("防火门:"); Data.Floors[i].FM = EditorGUILayout.Toggle(Data.Floors[i].FM); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("防火卷帘:"); Data.Floors[i].FHJL = EditorGUILayout.Toggle(Data.Floors[i].FHJL); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("室内消火栓:"); Data.Floors[i].SNXHS = EditorGUILayout.Toggle(Data.Floors[i].SNXHS); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("消控室:"); Data.Floors[i].XKS = EditorGUILayout.Toggle(Data.Floors[i].XKS); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal("HelpBox"); GUILayout.Label("泵房水箱:"); Data.Floors[i].BFSX = EditorGUILayout.Toggle(Data.Floors[i].BFSX); GUILayout.EndHorizontal(); GUILayout.EndHorizontal(); } } GUILayout.BeginHorizontal("Box"); if (GUILayout.Button("保存")) { SaveData(); } GUILayout.EndHorizontal(); GUILayout.EndVertical(); //滑条结束,所有包含在滑条内的UI在此之上 EditorGUILayout.EndScrollView(); } #endregion } private void SetData() { Data.Prefix = Prefix; Data.Highest = Highest; Data.Lowest = lowest; Data.Floors = new List(); for(int i = lowest; i < Highest; i++) { if (i != 0) { var floor = new FloorButton(); Data.Floors.Add(floor); floor.ButtonName = "Floor" + i; floor.FloorName = i + "F"; } } PostFile(fullpath, Data); } public void SaveData() { PostFile(fullpath, Data); } public void GetData() { GetFile(fullpath, data => Data = data); ShowInfos = true; } //上传数据 public void PostFile(string url, T obj) { var jsonParam = JsonConvert.SerializeObject(obj, Formatting.Indented); File.WriteAllText(url, jsonParam); Debug.Log($"数据保存完成"); } //获取数据 public void GetFile(string url, Action action) { string json = File.ReadAllText(url); T obj = JsonConvert.DeserializeObject(json); action?.Invoke(obj); } }