355 lines
12 KiB
C#
355 lines
12 KiB
C#
using System.Collections.Generic;
|
||
using System.IO;
|
||
using PhxhSDK;
|
||
using TMPro;
|
||
using UnityEditor;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using Application = UnityEngine.Application;
|
||
using Button = UnityEngine.UI.Button;
|
||
using MenuItem = UnityEditor.MenuItem;
|
||
using Image = UnityEngine.UI.Image;
|
||
using Slider = UnityEngine.UI.Slider;
|
||
using Toggle = UnityEngine.UI.Toggle;
|
||
using ScrollView = PhxhSDK.PhxhScrollView;
|
||
|
||
/*
|
||
用于获取组件,写界面业务逻辑时直接拿到UGUI节点对应的对象
|
||
命名规范如下:
|
||
Button: Button_Xxx;
|
||
Text: Text_Xxx;
|
||
Image: Image_Xxx;
|
||
*/
|
||
|
||
//UI脚本自动生成工具
|
||
public class GetUIPath :Editor
|
||
{
|
||
|
||
public const string TEMP_TXT_PATH = "Assets/Editor/Tools/UIComponentTool/UICodeGenerateTemp.txt";
|
||
public const string BIND_TEMP_TXT_PATH = "Assets/Editor/Tools/UIComponentTool/UIBinderTemp.txt";
|
||
|
||
private static bool _isSuc = true;
|
||
|
||
private static bool _isAdmitRepeat = false;
|
||
//类型字典,可获取的对象包含以下类型
|
||
public static Dictionary<string, string> typMap = new Dictionary<string, string>()
|
||
{
|
||
{"Button_",typeof(Button).Name },
|
||
{"Text_",typeof(TMP_Text).Name },
|
||
{"TMP_",typeof(TMP_Text).Name },
|
||
{"Image_",typeof(Image).Name },
|
||
{"RawImage_",typeof(RawImage).Name },
|
||
{"Slider_", typeof(Slider).Name },
|
||
{"Scrollbar_", typeof(Scrollbar).Name },
|
||
{"ScrollView_", typeof(RecycleView).Name },
|
||
{"ScrollRect_", typeof(ScrollRect).Name },
|
||
{"Toggle_", typeof(Toggle).Name },
|
||
{"Dropdown_", typeof(TMP_Dropdown).Name },
|
||
{"InputField_", typeof(TMP_InputField).Name },
|
||
{"Canvas_", typeof(Canvas).Name }
|
||
};
|
||
private static List<UIInfo> m_UIInfoList = new List<UIInfo>();
|
||
private static List<UIBinderInfo> _uiBInderInfos = new List<UIBinderInfo>();
|
||
private static Dictionary<string, UIInfo> _uiCmpNameDic = new Dictionary<string, UIInfo>();
|
||
private static Dictionary<string, UIBinderInfo> _uiBinderInfoDic = new Dictionary<string, UIBinderInfo>();
|
||
|
||
//存储路径:Assets\Code\Scripts\Gameplay\UI
|
||
public static string FILE_PATH = Application.dataPath + "/" + "Code" + "/" +
|
||
"Scripts" + "/" + "Gameplay" + "/" + "UI" + "/";
|
||
|
||
public static string filePath = FILE_PATH;
|
||
|
||
private static string _genFilePath = "Assets/Code/Scripts/Gameplay/UI/UIBinder/";
|
||
|
||
private static string m_scriptStr = ""; //写入脚本的初始代码
|
||
private static string _scriptDataStr = "";
|
||
private static string m_file1 = ""; //定义对应的变量,每个符合命名规范的节点都有一个对应变量
|
||
private static string m_body1 = ""; //脚本内对象获取
|
||
|
||
private static string _bindBody1 = "";
|
||
private static string _bindBody2 = "";
|
||
private static string _bindFile1 = "";
|
||
|
||
private static string _uiBinderName = "";
|
||
|
||
private static GameObject _selectGo;
|
||
|
||
// 生成脚本的基础内容写入
|
||
static void WriteScript()
|
||
{
|
||
//_uiBinderName = Selection.activeGameObject.name + "Binder";
|
||
m_scriptStr = ""; //写入脚本的初始代码
|
||
_scriptDataStr = "";
|
||
m_file1 = "";
|
||
_bindBody1 = "";
|
||
_bindBody2 = "";
|
||
_bindFile1 = "";
|
||
|
||
|
||
for (int i = 0; i < m_UIInfoList.Count; i++)
|
||
{
|
||
m_file1 += m_UIInfoList[i].File1 + ";\r\n\t";
|
||
if (i == m_UIInfoList.Count - 1)
|
||
{
|
||
m_body1 += m_UIInfoList[i].Body1 + ";\r\n\t";
|
||
}
|
||
else
|
||
{
|
||
m_body1 += m_UIInfoList[i].Body1 + ";\r\n\t\t";
|
||
}
|
||
//Debug.Log(uinfo[i].File1 + "_____________" + uinfo[i].Body1 + "______________" + uinfo[i].Body2);
|
||
}
|
||
|
||
for (int i = 0; i < _uiBInderInfos.Count; i++)
|
||
{
|
||
_bindFile1 += _uiBInderInfos[i].File1 + ";\r\n\t";
|
||
if (i == _uiBInderInfos.Count - 1)
|
||
{
|
||
_bindBody1 += _uiBInderInfos[i].Body1 + ";\r\n\t";
|
||
_bindBody2 += _uiBInderInfos[i].Body2 + ";\r\n\t";
|
||
}
|
||
else
|
||
{
|
||
_bindBody1 += _uiBInderInfos[i].Body1 + ";\r\n\t\t";
|
||
_bindBody2 += _uiBInderInfos[i].Body2 + ";\r\n\t\t";
|
||
}
|
||
}
|
||
|
||
_uiBInderInfos.Clear();
|
||
//加载模板内的基本代码
|
||
TextAsset txt = AssetDatabase.LoadAssetAtPath<TextAsset>(TEMP_TXT_PATH);
|
||
TextAsset bindTxt = AssetDatabase.LoadAssetAtPath<TextAsset>(BIND_TEMP_TXT_PATH);
|
||
if (!txt || !bindTxt)
|
||
{
|
||
m_scriptStr = "";
|
||
_scriptDataStr = "";
|
||
Debug.LogError("==== 请检查UI脚本模板文件的路径!!!====");
|
||
_isSuc = false;
|
||
}
|
||
else
|
||
{
|
||
m_scriptStr = txt.text;
|
||
_scriptDataStr = bindTxt.text;
|
||
}
|
||
|
||
m_scriptStr = m_scriptStr.Replace("@File1", m_file1);
|
||
m_scriptStr = m_scriptStr.Replace("@BinderName", _uiBinderName);
|
||
|
||
//_scriptDataStr = _scriptDataStr.Replace("@Body1", _bindBody1);
|
||
_scriptDataStr = _scriptDataStr.Replace("@Body2", _bindBody2);
|
||
_scriptDataStr = _scriptDataStr.Replace("@File1", _bindFile1);
|
||
|
||
string fileName;
|
||
if (filePath.EndsWith(_selectGo.name+ "Controller.cs"))
|
||
{
|
||
fileName = filePath;
|
||
}
|
||
else
|
||
{
|
||
fileName = filePath + _selectGo.name+ "Controller.cs";
|
||
}
|
||
string dataFileName = _genFilePath + _uiBinderName + ".cs";
|
||
|
||
|
||
//写入绑定文件
|
||
if (File.Exists(dataFileName))
|
||
{
|
||
// var bindName = _genFilePath + _selectGo.name + "Binder.cs";
|
||
// VersionControlSystem.Checkout(bindName);
|
||
File.Delete(dataFileName);
|
||
}
|
||
|
||
using (StreamWriter bindWriter = File.CreateText(dataFileName))
|
||
{
|
||
string[] name = fileName.Split('/');
|
||
if (name.Length <= 0)
|
||
{
|
||
Debug.LogError("==== 请检查UI脚本文件生成的路径!!!====");
|
||
_isSuc = false;
|
||
return;
|
||
}
|
||
|
||
string nameStr = name[name.Length - 1].Replace(".cs", "");
|
||
_scriptDataStr = _scriptDataStr.Replace("@Name", _uiBinderName);
|
||
_scriptDataStr = _scriptDataStr.Replace("@contoller", nameStr);
|
||
bindWriter.Write(_scriptDataStr);
|
||
}
|
||
|
||
//写入代码文件
|
||
if (!File.Exists(fileName))
|
||
{
|
||
using (StreamWriter writer = File.CreateText(fileName))
|
||
{
|
||
string[] name = fileName.Split('/');
|
||
if (name.Length <= 0)
|
||
{
|
||
Debug.LogError("==== 请检查UI脚本文件生成的路径!!!====");
|
||
_isSuc = false;
|
||
return;
|
||
}
|
||
string nameStr = name[name.Length - 1].Replace(".cs", "");
|
||
m_scriptStr = m_scriptStr.Replace("@Name", nameStr);
|
||
writer.Write(m_scriptStr);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
var filePathName = fileName.Replace('\\', '/');
|
||
//VersionControlSystem.Checkout(filePathName);
|
||
UIReGenTool.ReWriteDefine(_bindFile1, _selectGo.name, filePath);
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取每个GameObj的路径
|
||
/// </summary>
|
||
/// <param name="go"></param>
|
||
/// <returns></returns>
|
||
static string GetGameObjectPath(Transform go)
|
||
{
|
||
string path = "";
|
||
|
||
if (go != _selectGo)
|
||
{
|
||
SetPath(go, ref path);
|
||
go = go.parent;
|
||
}
|
||
return path;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置单个obj的获取路径
|
||
/// </summary>
|
||
/// <param name="pa">路径上的节点
|
||
static void SetPath(Transform pa, ref string path)
|
||
{
|
||
if (path.Length <= 0)
|
||
{
|
||
path = path.Insert(0, pa.name);
|
||
}
|
||
else
|
||
{
|
||
path = path.Insert(0, pa.name + "/");
|
||
}
|
||
|
||
if (pa.parent != pa.root)
|
||
{
|
||
SetPath(pa.parent, ref path);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取子节点信息
|
||
/// </summary>
|
||
/// <param name="tf"></param>
|
||
static void GetChildinfo(Transform tf)
|
||
{
|
||
foreach (Transform tfChild in tf)
|
||
{
|
||
string contrastKey = null;
|
||
foreach (var item in typMap)
|
||
{
|
||
if (tfChild.name.Contains(item.Key))
|
||
contrastKey = item.Key;
|
||
}
|
||
if (contrastKey != null && typMap.ContainsKey(contrastKey) )
|
||
{
|
||
UIInfo uinf = new UIInfo(tfChild.name, contrastKey, GetGameObjectPath(tfChild));
|
||
m_UIInfoList.Add(uinf);
|
||
if (!_isAdmitRepeat)
|
||
{
|
||
if (_uiCmpNameDic.ContainsKey(tfChild.name))
|
||
{
|
||
|
||
DebugUtil.LogError("有重名节点!!!节点名:" + tfChild.name);
|
||
_isSuc = false;
|
||
return;
|
||
}
|
||
_uiCmpNameDic.Add(tfChild.name, uinf);
|
||
}
|
||
}
|
||
if (tfChild.childCount >= 0)
|
||
{
|
||
//获取子节点
|
||
GetChildinfo(tfChild);
|
||
}
|
||
}
|
||
}
|
||
|
||
static void GetBinderChildInfo(Transform tf)
|
||
{
|
||
foreach (Transform tfChild in tf)
|
||
{
|
||
string contrastKey = null;
|
||
foreach (var item in typMap)
|
||
{
|
||
if (tfChild.name.Contains(item.Key))
|
||
contrastKey = item.Key;
|
||
}
|
||
if (contrastKey != null && typMap.ContainsKey(contrastKey) )
|
||
{
|
||
UIBinderInfo uinf = new UIBinderInfo(tfChild.name, contrastKey, GetGameObjectPath(tfChild));
|
||
_uiBInderInfos.Add(uinf);
|
||
if (!_isAdmitRepeat)
|
||
{
|
||
if (_uiBinderInfoDic.ContainsKey(tfChild.name))
|
||
{
|
||
DebugUtil.LogError("有重名节点!!!节点名:" + tfChild.name);
|
||
_isSuc = false;
|
||
return;
|
||
}
|
||
_uiBinderInfoDic.Add(tfChild.name, uinf);
|
||
}
|
||
}
|
||
if (tfChild.childCount >= 0)
|
||
{
|
||
//获取子节点
|
||
GetBinderChildInfo(tfChild);
|
||
}
|
||
}
|
||
}
|
||
|
||
[MenuItem("Assets/*Custom/UI/GetUIController _%M")]
|
||
public static void GetUIController()
|
||
{
|
||
GameObject[] select = Selection.gameObjects;
|
||
if (select.Length == 1)
|
||
{
|
||
// _selectGo = select[0];
|
||
// Transform selectGo = select[0].transform;
|
||
// _uiBinderName = selectGo.name + "Binder";
|
||
// GetBinderChildInfo(selectGo);
|
||
// GetChildinfo(selectGo);
|
||
// WriteScript();
|
||
// AssetDatabase.Refresh();
|
||
UICtrlGenerateWindow.ShowGenerateWindow();
|
||
}
|
||
else
|
||
{
|
||
EditorUtility.DisplayDialog("无法创建", "选中对象数量有误", "确定");
|
||
}
|
||
}
|
||
|
||
public static void CreateScript(GameObject go, string path, bool isAdmitRepeat = false)
|
||
{
|
||
_isSuc = true;
|
||
_selectGo = go;
|
||
_isAdmitRepeat = isAdmitRepeat;
|
||
_uiBinderName = go.name + "Binder";
|
||
_uiCmpNameDic.Clear();
|
||
_uiBinderInfoDic.Clear();
|
||
m_UIInfoList.Clear();
|
||
_uiBInderInfos.Clear();
|
||
GetBinderChildInfo(go.transform);
|
||
GetChildinfo(go.transform);
|
||
filePath = path;
|
||
if (!_isSuc)
|
||
{
|
||
return;
|
||
}
|
||
WriteScript();
|
||
AssetDatabase.Refresh();
|
||
filePath = FILE_PATH;
|
||
}
|
||
} |