|
先看效果
2.需要资源:
ViewElementModel.txt
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using GersonFrame.UI;
namespace DragonRun
{
public class {0} : ViewElementBase
{
{1}
public override void InitElement(GameObject go)
{
{2}
}
}
}
ViewElementEditor.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;
public class ViewElementEditor : EditorWindow
{
public string CsFilePath = "/HotFix_Dragon~/UI_Hot/ViewElement/";
public string ViewMudelFilePath = "Assets/GersonFrame/UIManager/Editor/ViewElementModel.txt";
private GameObject m_viewGoRoot;
private string GetCompentStr = &#34;GetComponent<{0}>();\n&#34;;
private bool m_click;
private List<string> m_properityList = new List<string>();
private List<string> m_getCompentList = new List<string>();
private SerializedObject m_obj;
private SerializedProperty m_CsFilePath;
[MenuItem(&#34;MyTools/创建ViewElement数据&#34;)]
static void ShowEditor()
{
ViewElementEditor combinewindow = GetWindow<ViewElementEditor>();
combinewindow.minSize = new Vector2(370, 370);
}
private void OnEnable()
{
MyDebuger.InitLogger( LogLevel.All);
m_click = false;
m_obj = new SerializedObject(this);
m_CsFilePath = m_obj.FindProperty(&#34;CsFilePath&#34;);
}
private void OnGUI()
{
BeginWindows();
m_obj.Update();
EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(m_CsFilePath,true);
if (EditorGUI.EndChangeCheck())
{
m_obj.ApplyModifiedProperties();
}
EditorGUILayout.LabelField(&#34;View根节点:&#34;);
m_viewGoRoot = (GameObject)EditorGUILayout.ObjectField(m_viewGoRoot, typeof(GameObject), true);
this.m_click = GUILayout.Button(&#34;创建&#34;);
EndWindows();
if (m_click)
{
this.m_click = false;
if (m_viewGoRoot == null)
{
MyDebuger.LogError(&#34;View根节点不能为空 &#34;);
return;
}
CreateViewElemnet();
}
}
void CreateViewElemnet()
{
m_properityList.Clear();
m_getCompentList.Clear();
TextAsset basestrTxt = AssetDatabase.LoadAssetAtPath<TextAsset>(ViewMudelFilePath);
string basestr = basestrTxt.text;
string allpropertity = &#34;&#34;;
string allgetCompent = &#34;&#34;;
FindGoChild(m_viewGoRoot.transform);
if (m_properityList.Count < 1) {
MyDebuger.LogError(&#34;未找可生成的组件物体&#34;);
return;
}
m_click = true;
for (int i = 0; i < this.m_properityList.Count; i++)
allpropertity += this.m_properityList;
for (int i = 0; i < this.m_getCompentList.Count; i++)
allgetCompent += this.m_getCompentList;
string elementName = m_viewGoRoot.name+ &#34;ViewElement&#34;;
string newbasestr = basestr.Replace(&#34;{0}&#34;, elementName);
newbasestr= newbasestr.Replace(&#34;{1}&#34;, allpropertity);
string newbasestr2 = newbasestr.Replace(&#34;{2}&#34;, allgetCompent);
string filepath = Application.dataPath + CsFilePath + elementName + &#34;.cs&#34;;
if (File.Exists(filepath)) File.Delete(filepath);
using (FileStream fs = new FileStream(filepath,FileMode.OpenOrCreate))
{
using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
{
sw.Write(newbasestr2);
}
}
MyDebuger.Log(&#34;创建 &#34;+elementName +&#34;成功!&#34;);
AssetDatabase.Refresh();
}
void FindGoChild(Transform ts)
{
TsNeedAddInViewElement(ts);
for (int i = 0; i < ts.childCount; i++)
{
FindGoChild(ts.GetChild(i));
}
}
void TsNeedAddInViewElement(Transform childts)
{
string properitystr = &#34;&#34;;
string tempgetCompentstr = &#34;&#34;;
string properityName = &#34;m_&#34; + childts.name;
if (childts.name.Contains(&#34;_Txt&#34;))
{
tempgetCompentstr= GetCompentStr.Replace(&#34;{0}&#34;, &#34;Text&#34;);
properitystr = &#34;public Text &#34;+ properityName+&#34;;\n&#34;;
}
else if (childts.name.Contains(&#34;_Btn&#34;))
{
tempgetCompentstr = GetCompentStr.Replace(&#34;{0}&#34;, &#34;Button&#34;);
properitystr = &#34;public Button &#34; + properityName + &#34;;\n&#34;;
}
else if (childts.name.Contains(&#34;_RawImg&#34;))
{
tempgetCompentstr = GetCompentStr.Replace(&#34;{0}&#34;, &#34;RawImage&#34;);
properitystr = &#34;public RawImage &#34; + properityName + &#34;;\n&#34;;
}
else if (childts.name.Contains(&#34;_Img&#34;))
{
tempgetCompentstr = GetCompentStr.Replace(&#34;{0}&#34;, &#34;Image&#34;);
properitystr = &#34;public Image &#34; + properityName + &#34;;\n&#34;;
}
else if (childts.name.Contains(&#34;_Ts&#34;))
{
tempgetCompentstr = GetCompentStr.Replace(&#34;{0}&#34;, &#34;Transform&#34;);
properitystr = &#34;public Transform &#34; + properityName + &#34;;\n&#34;;
}
else if (childts.name.Contains(&#34;_RectTs&#34;))
{
tempgetCompentstr = GetCompentStr.Replace(&#34;{0}&#34;, &#34;RectTransform&#34;);
properitystr = &#34;public RectTransform &#34; + properityName + &#34;;\n&#34;;
}
if (!string.IsNullOrEmpty(properitystr))
{
m_properityList.Add(properitystr);
string path = childts.GetPath(m_viewGoRoot.transform);
string tempgetCompentNameStr = properityName+&#34;=go.transform.Find(&#34; + &#39;&#34;&#39; + path+ &#39;&#34;&#39; + &#34;).&#34;;
m_getCompentList.Add(tempgetCompentNameStr+tempgetCompentstr);
}
}
}
ViewElementBase.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace GersonFrame.UI
{
/// <summary>
/// 一个物体结尾为_Txt 需要自动生成Text组件
/// 一个物体结尾为_Btn 需要自动生成Button组件
///一个物体结尾为_RawImg 需要自动生成RawImage组件
///一个物体结尾为_Img 需要自动生成Image组件
///一个物体结尾为_Ts 需要自动生成Transform组件
/// 一个物体结尾为_RectTs 需要自动生成RectTransform组件
/// </summary>
public class ViewElementBase
{
public virtual void InitElement(GameObject go)
{
}
}
}
VS工程自动添加生成的文件方法如下(此处要感谢群里的小伙伴的提示):
1.找到vs工程的csproj这个文件,使用文本编辑器打开 找到一堆include的目录地方
2.添加UI生成文件的指定目标 然后加上*.cs
3.删除原有生成的所有在UI生成文件夹下的Include标记
4.点击解决方案的刷新按钮
5.效果如图
具体用法在脚本中有哈 什么问题欢迎留言 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|