先修复一下,错误的场景 删除不必要的钓场资产 修复into场景 update:更新meta文件,修复报错 update:修复资源 修复第一章节建造 修复建造第三章 update:删除多余内容 update:更新README.md update:还原图标 update:更新README update:更新配置
377 lines
11 KiB
C#
377 lines
11 KiB
C#
namespace Editor.UITool
|
|
{
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
|
|
|
|
public class UIPrefabToClass
|
|
{
|
|
|
|
private static StreamWriter writer;
|
|
//缩写 Obj_ 开头
|
|
private static Dictionary<string, string> TransformDic = new Dictionary<string, string>();
|
|
//缩写 Img_ 开头
|
|
private static Dictionary<string, string> ImageDic = new Dictionary<string, string>();
|
|
//缩写 Btn_ 开头
|
|
private static Dictionary<string, string> ButtonDic = new Dictionary<string, string>();
|
|
//缩写 Txt_ 开头
|
|
private static Dictionary<string, string> TextDic = new Dictionary<string, string>();
|
|
//缩写 Input_ 开头
|
|
private static Dictionary<string, string> InputFieldDic = new Dictionary<string, string>();
|
|
//缩写 Toggle_ 开头
|
|
private static Dictionary<string, string> ToggleDic = new Dictionary<string, string>();
|
|
//缩写 ScrollRect_ 开头
|
|
private static Dictionary<string, string> ScrollRectDic = new Dictionary<string, string>();
|
|
//缩写 LayoutGroup_ 开头
|
|
private static Dictionary<string, string> LayoutGroupDic = new Dictionary<string, string>();
|
|
//缩写 Ani_ 开头
|
|
private static Dictionary<string, string> AnimatorDic = new Dictionary<string, string>();
|
|
//缩写 Sk_ 开头
|
|
private static Dictionary<string, string> SkeletonGraphicDic = new Dictionary<string, string>();
|
|
|
|
|
|
[MenuItem("Tools/UI/DoPanelClass", priority = 21)]
|
|
public static void DoPanel()
|
|
{
|
|
ChackCode(1);
|
|
}
|
|
|
|
[MenuItem("Tools/UI/DoItemClass", priority = 22)]
|
|
public static void DoItem()
|
|
{
|
|
ChackCode(2);
|
|
}
|
|
|
|
|
|
public static void ChackCode(int type)
|
|
{
|
|
|
|
if (writer != null)
|
|
writer.Close();
|
|
|
|
var preObj = Selection.activeGameObject;
|
|
|
|
TransformDic.Clear();
|
|
ImageDic.Clear();
|
|
ButtonDic.Clear();
|
|
TextDic.Clear();
|
|
InputFieldDic.Clear();
|
|
ToggleDic.Clear();
|
|
ScrollRectDic.Clear();
|
|
LayoutGroupDic.Clear();
|
|
AnimatorDic.Clear();
|
|
SkeletonGraphicDic.Clear();
|
|
|
|
string path = "Assets/Scripts/UIScriptBase/";
|
|
string fileName = preObj.name + "Logic_Auto.cs";
|
|
|
|
writer = new StreamWriter(path + fileName);
|
|
writer.WriteLine("using System.Collections.Generic;");
|
|
writer.WriteLine("using System.Linq;");
|
|
writer.WriteLine("using UnityEngine;");
|
|
writer.WriteLine("using UnityEngine.UI;");
|
|
writer.WriteLine("using Spine.Unity;");
|
|
writer.WriteLine("using UIScriptBase;");
|
|
writer.WriteLine("using TMPro;");
|
|
|
|
writer.WriteLine("");
|
|
if (type == 1)
|
|
{
|
|
writer.WriteLine("public partial class " + preObj.name + "Logic: BasePanel");
|
|
}
|
|
else
|
|
{
|
|
writer.WriteLine("public partial class " + preObj.name + "Logic: MonoBehaviour");
|
|
}
|
|
|
|
|
|
writer.WriteLine("{");
|
|
|
|
DoPrefab(preObj);
|
|
|
|
//变量定义----begin
|
|
|
|
writer.WriteLine(" private RectTransform _offset;");
|
|
writer.WriteLine(" private GameObject _root;");
|
|
|
|
WriterProp();
|
|
|
|
//变量定义----over
|
|
|
|
writer.WriteLine("");
|
|
writer.WriteLine(" private void Awake()");
|
|
writer.WriteLine(" {");
|
|
|
|
//变量实例化----begin
|
|
|
|
if (type == 1)
|
|
{
|
|
writer.WriteLine(" _offset = GetCmp<RectTransform>(" + '"'+"root" +'"'+");");
|
|
writer.WriteLine(" _root = GetObj("+'"'+"root"+'"' +");");
|
|
}
|
|
|
|
WriterInstance();
|
|
|
|
//变量实例化-----over
|
|
|
|
writer.WriteLine(" }");
|
|
|
|
writer.WriteLine(" private T GetCmp<T>(string path) where T : Component");
|
|
writer.WriteLine(" {");
|
|
writer.WriteLine(" return UIUtils.GetCmp<T>(this.transform, path);");
|
|
writer.WriteLine(" }");
|
|
|
|
writer.WriteLine(" private GameObject GetObj(string path)");
|
|
writer.WriteLine(" {");
|
|
writer.WriteLine(" return UIUtils.GetObj(this.transform, path);");
|
|
writer.WriteLine(" }");
|
|
|
|
writer.WriteLine(" private Transform GetTrans(string path)");
|
|
writer.WriteLine(" {");
|
|
writer.WriteLine(" return UIUtils.GetTrans(this.transform, path);");
|
|
writer.WriteLine(" }");
|
|
|
|
|
|
if (type == 1)
|
|
{
|
|
writer.WriteLine(" public Transform Root");
|
|
writer.WriteLine(" {");
|
|
writer.WriteLine(" get");
|
|
writer.WriteLine(" {");
|
|
writer.WriteLine(" return _offset;");
|
|
writer.WriteLine(" }");
|
|
writer.WriteLine(" }");
|
|
}
|
|
|
|
|
|
writer.WriteLine("}");
|
|
writer.Close();
|
|
var fullDirPath = Path.GetFullPath(path + fileName);
|
|
System.Diagnostics.Process.Start("explorer.exe", fullDirPath);
|
|
|
|
string log = "输出:" + fileName + "完毕!!";
|
|
Debug.LogError($"<color=green>{log}</color>");
|
|
}
|
|
|
|
private static void WriterProp()
|
|
{
|
|
foreach (var prop in TransformDic)
|
|
{
|
|
writer.WriteLine(" private RectTransform" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in ImageDic)
|
|
{
|
|
writer.WriteLine(" private Image" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in ButtonDic)
|
|
{
|
|
writer.WriteLine(" private Button" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in TextDic)
|
|
{
|
|
writer.WriteLine(" private TextMeshProUGUI" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in InputFieldDic)
|
|
{
|
|
writer.WriteLine(" private InputField" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in ToggleDic)
|
|
{
|
|
writer.WriteLine(" private Toggle" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in ScrollRectDic)
|
|
{
|
|
writer.WriteLine(" private ScrollRect" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in LayoutGroupDic)
|
|
{
|
|
writer.WriteLine(" private LayoutGroup" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in AnimatorDic)
|
|
{
|
|
writer.WriteLine(" private Animator" + " " + "_" +prop.Key + ";");
|
|
}
|
|
|
|
foreach (var prop in SkeletonGraphicDic)
|
|
{
|
|
writer.WriteLine(" private SkeletonGraphic" + " " + "_" +prop.Key + ";");
|
|
}
|
|
}
|
|
|
|
private static void WriterInstance()
|
|
{
|
|
foreach (var prop in TransformDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<RectTransform>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in ImageDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<Image>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in ButtonDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<Button>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in TextDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<TextMeshProUGUI>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in InputFieldDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<InputField>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in ToggleDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<Toggle>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in ScrollRectDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<ScrollRect>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in LayoutGroupDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<LayoutGroup>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in AnimatorDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<Animator>(" + prop.Value+");");
|
|
}
|
|
|
|
foreach (var prop in SkeletonGraphicDic)
|
|
{
|
|
writer.WriteLine(" _" +prop.Key +"= GetCmp<SkeletonGraphic>(" + prop.Value+");");
|
|
}
|
|
|
|
}
|
|
|
|
private static void DoPrefab(GameObject obj)
|
|
{
|
|
Transform[] allChild = obj.GetComponentsInChildren<Transform>();
|
|
string[] childNameArr = new string[]{};
|
|
TransformDic.Clear();
|
|
ImageDic.Clear();
|
|
ButtonDic.Clear();
|
|
foreach (Transform child in allChild)
|
|
{
|
|
//Debug.LogError($"<color=green>{child.name}</color>");
|
|
|
|
if (child.name.IndexOf("Obj_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
TransformDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("Img_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
ImageDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("Btn_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
ButtonDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("Txt_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
TextDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("Input_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
InputFieldDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("Toggle_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
ToggleDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("ScrollRect_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
ScrollRectDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("LayoutGroup_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
LayoutGroupDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("Ani_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
AnimatorDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
else if (child.name.IndexOf("Sk_") == 0)
|
|
{
|
|
childNameArr = child.name.Split('_');
|
|
SkeletonGraphicDic[childNameArr[1]] = GetWritePath(child);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static string GetWritePath(Transform trans)
|
|
{
|
|
if (null == trans) return string.Empty;
|
|
string path = GetPath(trans);
|
|
path = FormatPath(path);
|
|
string[] arr = path.Split('/');
|
|
|
|
string splitPath = string.Empty;
|
|
for (int i = 2; i < arr.Length; i++)
|
|
{
|
|
if (i == arr.Length - 1)
|
|
{
|
|
splitPath += arr[i];
|
|
}
|
|
else
|
|
{
|
|
splitPath += arr[i] + "/";
|
|
}
|
|
}
|
|
splitPath = "\"" + FormatPath(splitPath) + "\"";
|
|
return splitPath;
|
|
}
|
|
|
|
public static string GetPath(Transform trans)
|
|
{
|
|
if (null == trans) return string.Empty;
|
|
if (null == trans.parent) return trans.name;
|
|
return GetPath(trans.parent) + "/" + trans.name;
|
|
}
|
|
|
|
public static string FormatPath(string path)
|
|
{
|
|
return path.Replace(@"\", "/");
|
|
}
|
|
|
|
private static string ProcessName(string name)
|
|
{
|
|
string result = name;
|
|
result = result.Replace("(", "");
|
|
result = result.Replace(")", "");
|
|
result = result.Replace(" ", "");
|
|
return result;
|
|
}
|
|
|
|
}
|
|
#endif
|
|
|
|
} |