using Boo.Lang; using System; using System.IO; using UnityEditor; using UnityEngine; public class UIData { public string Name; public string des; } public class CreateUIClass : EditorWindow { [MenuItem("Tools/Class/Create UI Class")] public static void CreateUIClassWindow() { var window = (CreateUIClass)EditorWindow.GetWindow(typeof(CreateUIClass), true, "Create UI Class"); window.Show(); } public bool CloseAttrube; public string DataName; public List Data = new List(); public int DataCount = 2; private void OnGUI() { GUILayout.BeginVertical("HelpBox"); GUILayout.BeginVertical("Box"); DataName = EditorGUILayout.TextField("DataName", DataName); GUILayout.EndVertical(); GUILayout.BeginHorizontal("Box"); DataCount = EditorGUILayout.IntSlider("数据量:", DataCount, 0, 20); if (GUILayout.Button("创建")) { Data.Clear(); for (int i = 0; i < DataCount; i++) { UIData data = new UIData(); Data.Add(data); } } GUILayout.EndHorizontal(); for (int i = 0; i < Data.Count; i++) { GUILayout.BeginHorizontal("Box"); Data[i].Name = EditorGUILayout.TextField("名称:", Data[i].Name); Data[i].des = EditorGUILayout.TextField("描述:", Data[i].des); GUILayout.EndHorizontal(); } GUILayout.BeginVertical("Box"); CloseAttrube = EditorGUILayout.Toggle("关闭界面选项", CloseAttrube); GUILayout.EndVertical(); GUILayout.BeginVertical("Box"); if (GUILayout.Button("Create")) { CreateClass(); } GUILayout.EndVertical(); GUILayout.EndVertical(); } private void CreateClass() { GameObject selected = Selection.activeObject as GameObject; if (selected == null || selected.name.Length == 0) { Debug.Log("Selected object not Valid"); return; } string name = selected.name.Replace(" ", "_"); name = name.Replace("-", "_"); string copyPath = "Assets/Scripts/UIManager/View/" + name + ".cs"; if (File.Exists(copyPath)) { File.Delete(copyPath); } using(var outfile = new StreamWriter(copyPath)) { outfile.WriteLine("using UnityEngine;"); outfile.WriteLine("using UnityEngine.UI;"); outfile.WriteLine("using UniRx;"); outfile.WriteLine("using AX.ImageViewer;"); outfile.WriteLine(""); if (string.IsNullOrEmpty(DataName)) { outfile.WriteLine("public class " + name + " : UIView"); } else { outfile.WriteLine("public class " + name + " : UIView<"+DataName+","+DataName+ "Reactive>"); } outfile.WriteLine("{"); foreach(var tempdata in Data) { outfile.WriteLine($" //{tempdata.des}"); outfile.WriteLine($" public Text {tempdata.Name}Text;"); outfile.WriteLine($" public InputField {tempdata.Name}Input;"); } outfile.WriteLine(" //图片类型"); outfile.WriteLine(" public OriginalImageType ImageType;"); outfile.WriteLine(" //图片面板"); outfile.WriteLine(" public Image ImagePanel;"); outfile.WriteLine(" //全景开关"); outfile.WriteLine(" public Toggle ImageTypeToggle;"); outfile.WriteLine(" //查看原图"); outfile.WriteLine(" public Button ViewButton;"); outfile.WriteLine(" public OpenImage OpenImageFile;"); outfile.WriteLine(" //保存按钮"); outfile.WriteLine(" public Button SaveButton;"); outfile.WriteLine(" public override void Awake()"); outfile.WriteLine(" {"); outfile.WriteLine(" base.Awake();"); outfile.WriteLine(" #region DataSource Bind"); foreach (var tempdata in Data) { outfile.WriteLine($" //{tempdata.des}"); outfile.WriteLine($" DataSource.{tempdata.Name}.SubscribeToText({tempdata.Name}Text).AddTo(gameObject);"); outfile.WriteLine($" DataSource.{tempdata.Name}.SubscribeToText({tempdata.Name}Input).AddTo(gameObject);"); outfile.WriteLine($" {tempdata.Name}Input.OnValueChangedAsObservable().Subscribe(s => DataSource.{tempdata.Name}.Value = s);"); } outfile.WriteLine(" //图片类型"); outfile.WriteLine(" DataSource.ImageType.Subscribe(data =>"); outfile.WriteLine(" {"); outfile.WriteLine(" switch (data)"); outfile.WriteLine(" {"); outfile.WriteLine(" case OriginalImageType.Normal:"); outfile.WriteLine(" ImageTypeToggle.isOn = false;"); outfile.WriteLine(" break;"); outfile.WriteLine(" case OriginalImageType.Panorama:"); outfile.WriteLine(" ImageTypeToggle.isOn = true;"); outfile.WriteLine(" break;"); outfile.WriteLine(" }"); outfile.WriteLine(" }).AddTo(gameObject);"); outfile.WriteLine(" ImageTypeToggle.OnValueChangedAsObservable().Subscribe(b =>"); outfile.WriteLine(" {"); outfile.WriteLine(" DataSource.ImageType.Value = b ? OriginalImageType.Panorama : OriginalImageType.Normal;"); outfile.WriteLine(" }).AddTo(gameObject);"); outfile.WriteLine(" #endregion"); outfile.WriteLine(" #region Button Click"); outfile.WriteLine(" //保存"); outfile.WriteLine(" SaveButton.onClick.AsObservable().Subscribe(onClick =>"); outfile.WriteLine(" {"); outfile.WriteLine(" SaveData();"); outfile.WriteLine(" }).AddTo(gameObject);"); outfile.WriteLine(" //查看原图"); outfile.WriteLine(" ViewButton.onClick.AsObservable().Subscribe(onClick =>"); outfile.WriteLine(" {"); outfile.WriteLine(" ViewImage();"); outfile.WriteLine(" }).AddTo(gameObject);"); outfile.WriteLine(" //上传图片"); outfile.WriteLine(" OpenImageFile.OnLoadTextureFinished = texture =>"); outfile.WriteLine(" {"); outfile.WriteLine(" UploadImage(texture);"); outfile.WriteLine(" };"); if (CloseAttrube) { outfile.WriteLine(""); outfile.WriteLine(" transform.Find(\"TitleBar/CloseButton\").GetComponent