上海虹口龙之梦项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
12 KiB

8 months ago
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<UIData> Data = new List<UIData>();
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<Button>().OnClickAsObservable()");
outfile.WriteLine(" .Subscribe(_ => Hide());");
}
outfile.WriteLine(" #endregion");
outfile.WriteLine(" }");
outfile.WriteLine(" public override void Show()");
outfile.WriteLine(" {");
outfile.WriteLine(" base.Show();");
outfile.WriteLine(" LoadData();");
outfile.WriteLine(" GetComponent<AttributePanelControl>().ResetPanel();");
outfile.WriteLine(" }");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" /// 加载数据");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" private void LoadData()");
outfile.WriteLine(" {");
outfile.WriteLine(" string url = string.Format(HttpManager.Instance.GetBuildingFeatures);");
outfile.WriteLine(" HttpManager.Instance.Get<"+DataName+ ">(url, d =>");
outfile.WriteLine(" {");
outfile.WriteLine(" DataSource.SetData(d);");
outfile.WriteLine(" GetImage();");
outfile.WriteLine(" });");
outfile.WriteLine(" }");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" /// 保存数据");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" private void SaveData()");
outfile.WriteLine(" {");
outfile.WriteLine(" string url = string.Format(HttpManager.Instance.PostOutdoorFireHydrantsById, DataSource.Id);");
outfile.WriteLine(" HttpManager.Instance.Post(url, DataSource.GetData());");
outfile.WriteLine(" }");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" /// 上传图片");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" private void UploadImage(Texture2D texture)");
outfile.WriteLine(" {");
outfile.WriteLine(" HttpManager.Instance.PostImage($\"{ DataSource.Id}.jpg\", texture, a =>");
outfile.WriteLine(" {");
outfile.WriteLine(" DataSource.ImageUrl.Value = a?.ObjectName;");
outfile.WriteLine(" });");
outfile.WriteLine(" }");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" /// 获取图片");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" private void GetImage()");
outfile.WriteLine(" {");
outfile.WriteLine(" if (!string.IsNullOrEmpty(DataSource.ImageUrl.Value))");
outfile.WriteLine(" {");
outfile.WriteLine(" HttpManager.Instance.GetImage($\"{ DataSource.ImageUrl.Value}?x-oss-process=image/resize,h_200\", texture =>");
outfile.WriteLine(" {");
outfile.WriteLine(" ImagePanel.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);");
outfile.WriteLine(" ImagePanel.preserveAspect = true;");
outfile.WriteLine(" });");
outfile.WriteLine(" }");
outfile.WriteLine(" else");
outfile.WriteLine(" {");
outfile.WriteLine(" ImagePanel.sprite = AssetManager.Instance.DefaultSprite;");
outfile.WriteLine(" }");
outfile.WriteLine(" }");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" /// 查看原图");
outfile.WriteLine(" /// <summary>");
outfile.WriteLine(" private void ViewImage()");
outfile.WriteLine(" {");
outfile.WriteLine(" if (!string.IsNullOrEmpty(DataSource.ImageUrl.Value))");
outfile.WriteLine(" {");
outfile.WriteLine(" HttpManager.Instance.GetImage(DataSource.ImageUrl.Value, texture =>");
outfile.WriteLine(" {");
outfile.WriteLine(" switch (DataSource.ImageType.Value)");
outfile.WriteLine(" {");
outfile.WriteLine(" case OriginalImageType.Normal:");
outfile.WriteLine(" ImageViewer.Load(texture);");
outfile.WriteLine(" break;");
outfile.WriteLine(" case OriginalImageType.Panorama:");
outfile.WriteLine(" PanoramicViewer.Load(texture);");
outfile.WriteLine(" break;");
outfile.WriteLine(" }");
outfile.WriteLine(" });");
outfile.WriteLine(" }");
outfile.WriteLine(" else");
outfile.WriteLine(" {");
outfile.WriteLine(" MessageBox.Show(\"未上传图片!\", Color.white, 3f);");
outfile.WriteLine(" }");
outfile.WriteLine(" }");
outfile.WriteLine("}");
}
//File written
AssetDatabase.Refresh();
}
}