/*
--- 创建人: zjy
--- 描 述: 导出UI控制脚本. UI控件需要安装标准命名
*/
using System;
using System.Collections.Generic;
//using System.Globalization;
//using System.Text.RegularExpressions;
using System.IO;
using System.Text;
using TMPro;
using Unity.Plastic.Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class GoInfo
{
public string AllPath;
public string TypeName; //类型名称
public string FullName; //全名
public GameObject go;
}
public class UIExportTool : Editor
{
public static string ScriptName;
public static string ScriptDir;
public static string UIScriptRootPath = UnityEngine.Application.dataPath + "/Scripts/UI"; // UI 脚本生成根目录
public static readonly string UIPrefabRootPath = "Assets/GameAssets/Prefabs/UI"; // UI Prefab根目录
public static readonly string UIPathConfigFile = "Assets/GameAssets/TextAssets/UIMgrPath.json"; // UI Prefab资源路径文件名
///
/// 在Project下右键创建
///
[MenuItem("Assets/创建UI脚本", false, -99)]
public static void ProCreateLua()
{
string allPath = AssetDatabase.GetAssetPath(Selection.activeGameObject);
//先替换成lua后缀
allPath = allPath.Replace(".prefab", ".cs");
string[] patharr = allPath.Split('/');
if (patharr.Length > 2)
{
//固定路径
ScriptDir = UIScriptRootPath + "/" + patharr[patharr.Length - 2];
ScriptName = patharr[patharr.Length - 1];
}
Debug.Log("开始生成:" + ScriptDir + "/" + ScriptName);
OnCreateBehaviour(Selection.activeGameObject.transform);
}
public static void OnCreateBehaviour(Transform ptf)
{
if (string.IsNullOrEmpty(ScriptName))
{
Debug.LogWarning("OnCreateBehaviour. string.IsNullOrEmpty(ScriptName). 先把设置文件名");
return;
}
List objs = new List();
foreach (var tf in ptf.GetComponentsInChildren(true))
{
if (tf == ptf)
{
continue;
}
string goname = tf.gameObject.name;
// Debug.Log(allPath);
GoInfo goinfo = new GoInfo();
goinfo.go = tf.gameObject;
goinfo.AllPath = GetAllPath(tf, ptf);
if (ComponentCanGrae(goname, tf, goinfo))
{
objs.Add(goinfo);
}
}
if (objs.Count == 0)
{
Debug.LogError("UI命名不规范|未找到需要导出的控件. 请自行创建");
return;
}
if (!Directory.Exists(ScriptDir))
{
Directory.CreateDirectory(ScriptDir);
}
string filePath = ScriptDir + "/" + ScriptName;
Debug.Log("导出目录 filePath:" + filePath);
if (!File.Exists(filePath))
{
CreateFile(objs);
Debug.Log("导出成功 filePath:" + filePath);
ExportUIPath();
return;
}
//文件存在替换文件
ReplaceFile(objs);
}
///
/// 文件不存在的时候创建文件
///
///
private static void CreateFile(List objs)
{
// Debug.Log("创建脚本");
UIExportTextBuilder bs = new UIExportTextBuilder(objs, Selection.activeGameObject.name);
bs.BuildFileAllStr();
byte[] buf = Encoding.UTF8.GetBytes(bs.AllStr.ToString());
File.WriteAllBytes(bs.filePath, buf);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("创建脚本完成. 路径:" + bs.filePath);
}
///
/// 文件存在的时候替换文件
///
///
private static void ReplaceFile(List objs)
{
//Debug.Log("已有文本");
UIExportTextBuilder bs = new UIExportTextBuilder(objs, ScriptName);
bs.ReplaceFile();
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
Debug.Log("已替换旧文件标签内内容. filePath:" + bs.filePath);
}
public static string GetAllPath(Transform tf, Transform ptf)
{
string name = tf.gameObject.name;
Transform temp = tf.parent;
while (temp != null && temp != ptf)
{
name = temp.name + "/" + name;
temp = temp.parent;
}
return name;
}
// TestUI // 界面预制体
// TestUI.lua.txt // 界面脚本
// TestUnit // 界面单元格
// TestUnit.lua.txt // 界面单元格脚本
// TestCtrl // 预制体
// TestCtrl.lua.txt // 预制体控制脚本
//
// UI控件简写
// Text: txtTest // 文本
// Text: txtTest // 文本
// Image: imgTest // 精灵图片
// RawImage: rimgTest // 大图
// Button: btnTest // 按钮
// InputField: iptTest // 输入框
// Transform: tfTest // 坐标变幻
// RectTransform: rtfTest // 坐标变幻
// GameObject: goTest // 对象
// Panel: plTest // 面板,容器
// ScrollView|ScrollRect: svTest // 滑动列表
// SupperScrollView: ssvTest // 超级滑动列表
// Toggle: tglTest // 选择框
// ToggleGroup: tglgTest // 选择组
// Slider: sldTest // 滑动条
// Dropdown: ddTest // 下拉选择框
// UIInputNumChange: numc //数字变化组件
// TextMeshProUGUI: 例如 tmpTest //TMP文本 tmp
// TMP_InputField: 例如 tiptTest //TMP文本输入框 tipt
// TMP_Dropdown: 例如 tddTest //TMP下拉框
// UICurBgLeft: uibglTest //标题居左大
// UICurBgMiddle: uibgmTest //标题居中大
// UICurBgFullScreen: uibgfTest //全屏
// UICurBgSmall: uibgsTest //标题居中小
// UICurBgMiddle: uibgmtTest //弹窗
// UIRoleRawImage: rriTest // 角色模型
// UIItemIconList: iilTest // 道具列表
public static bool ComponentCanGrae(string goname, Transform tf, GoInfo goinfo)
{
if (goname.StartsWith("tf"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("rtf"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("go"))
{
TipHasComponent(goname, tf, goinfo);
goinfo.TypeName = "GameObject";
return true;
}
if (goname.StartsWith("pl"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("sv"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("rimg"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("img"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("tglg"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("tgl"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("sld"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("sclb"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("txt"))
{
TipHasComponent(goname, tf, goinfo);
return true;
}
if (goname.StartsWith("btn"))
{
TipHasComponent