mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-14 16:51:28 +00:00
增加支持ComponentAutoBindTool自动绑定UI元素组件。
增加支持ComponentAutoBindTool自动绑定UI元素组件。
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5fefbf9d90fc46eaaa7d12e5cd8cd219
|
||||
timeCreated: 1698301646
|
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 组件自动绑定工具
|
||||
/// </summary>
|
||||
public class ComponentAutoBindTool : MonoBehaviour
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
[Serializable]
|
||||
public class BindData
|
||||
{
|
||||
public BindData()
|
||||
{
|
||||
}
|
||||
|
||||
public BindData(string name, Component bindCom, bool isGameObject = false)
|
||||
{
|
||||
Name = name;
|
||||
BindCom = bindCom;
|
||||
IsGameObject = isGameObject;
|
||||
}
|
||||
|
||||
public string Name;
|
||||
public Component BindCom;
|
||||
public bool IsGameObject;
|
||||
}
|
||||
|
||||
public List<BindData> BindDatas = new List<BindData>();
|
||||
|
||||
[SerializeField]
|
||||
private string m_ClassName;
|
||||
|
||||
[SerializeField]
|
||||
private string m_Namespace;
|
||||
|
||||
[SerializeField]
|
||||
private string m_CodePath;
|
||||
|
||||
[SerializeField]
|
||||
private bool m_IsWidget;
|
||||
|
||||
public string ClassName => m_ClassName;
|
||||
|
||||
public string Namespace => m_Namespace;
|
||||
|
||||
public string CodePath => m_CodePath;
|
||||
|
||||
|
||||
public bool IsWidget => m_IsWidget;
|
||||
|
||||
public IAutoBindRuleHelper RuleHelper
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
#endif
|
||||
|
||||
[SerializeField]
|
||||
public List<Component> bindComponents = new List<Component>();
|
||||
|
||||
public T GetBindComponent<T>(int index) where T : Component
|
||||
{
|
||||
if (index >= bindComponents.Count)
|
||||
{
|
||||
Debug.LogError("索引无效");
|
||||
return null;
|
||||
}
|
||||
|
||||
T bindCom = bindComponents[index] as T;
|
||||
|
||||
if (bindCom == null)
|
||||
{
|
||||
Debug.LogError("类型无效");
|
||||
return null;
|
||||
}
|
||||
|
||||
return bindCom;
|
||||
}
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59269a619eb50d048840c07a31b0d368
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -0,0 +1,77 @@
|
||||
#if false
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 默认自动绑定规则辅助器
|
||||
/// </summary>
|
||||
public class DefaultAutoBindRuleHelper : IAutoBindRuleHelper
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 命名前缀与类型的映射
|
||||
/// </summary>
|
||||
private Dictionary<string, string> m_PrefixesDict = new Dictionary<string, string>()
|
||||
{
|
||||
{"Trans","Transform" },
|
||||
{"OldAnim","Animation"},
|
||||
{"NewAnim","Animator"},
|
||||
|
||||
{"Rect","RectTransform"},
|
||||
{"Canvas","Canvas"},
|
||||
{"Group","CanvasGroup"},
|
||||
{"VGroup","VerticalLayoutGroup"},
|
||||
{"HGroup","HorizontalLayoutGroup"},
|
||||
{"GGroup","GridLayoutGroup"},
|
||||
{"TGroup","ToggleGroup"},
|
||||
|
||||
{"Btn","UIButtonSuper"},
|
||||
{"Img","Image"},
|
||||
{"RImg","RawImage"},
|
||||
{"Txt","Text"},
|
||||
{"TxtM","TextMeshProUGUI"},
|
||||
{"Input","TMP_InputField"},
|
||||
{"Slider","Slider"},
|
||||
{"Mask","Mask"},
|
||||
{"Mask2D","RectMask2D"},
|
||||
{"Tog","Toggle"},
|
||||
{"Sbar","Scrollbar"},
|
||||
{"SRect","ScrollRect"},
|
||||
{"Drop","Dropdown"},
|
||||
{"USpriteAni","UGUISpriteAnimation"},
|
||||
{"VGridV","LoopGridView"},
|
||||
{"HGridV","LoopGridView"},
|
||||
{"VListV","LoopListView2"},
|
||||
{"HListV","LoopListView2"},
|
||||
};
|
||||
|
||||
public bool IsValidBind( Transform target, List<string> filedNames, List<string> componentTypeNames)
|
||||
{
|
||||
string[] strArray = target.name.Split('_');
|
||||
|
||||
if (strArray.Length == 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string filedName = strArray[^1];
|
||||
|
||||
for (int i = 0; i < strArray.Length - 1; i++)
|
||||
{
|
||||
string str = strArray[i];
|
||||
if (m_PrefixesDict.TryGetValue(str,out var componentName))
|
||||
{
|
||||
filedNames.Add($"{str}_{filedName}");
|
||||
componentTypeNames.Add(componentName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"{target.name}的命名中{str}不存在对应的组件类型,绑定失败");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 48a5faab9d684f14a38c5a001326eb03
|
||||
timeCreated: 1698301686
|
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// 自动绑定规则辅助器接口
|
||||
/// </summary>
|
||||
public interface IAutoBindRuleHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 是否为有效绑定
|
||||
/// </summary>
|
||||
bool IsValidBind(Transform target,List<string> filedNames,List<string> componentTypeNames);
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5654ec3474b94b7fa84dfdc684fe2fa9
|
||||
timeCreated: 1698301670
|
@@ -0,0 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// UI自动绑定规则辅助器
|
||||
/// </summary>
|
||||
public class UIAutoBindRuleHelper: IAutoBindRuleHelper
|
||||
{
|
||||
public bool IsValidBind( Transform targetTransform, List<string> filedNames, List<string> componentTypeNames)
|
||||
{
|
||||
string uiElementName = targetTransform.name;
|
||||
string[] strArray = targetTransform.name.Split('_');
|
||||
|
||||
if (strArray.Length == 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
string filedName = strArray[^1];
|
||||
var rule = SettingsUtils.GetScriptGenerateRule().Find(t => uiElementName.StartsWith(t.uiElementRegex));
|
||||
|
||||
if (rule != null)
|
||||
{
|
||||
filedNames.Add($"{filedName}");
|
||||
componentTypeNames.Add(rule.componentName);
|
||||
return true;
|
||||
}
|
||||
Debug.LogWarning($"{targetTransform.name}的命名中{uiElementName}不存在对应的组件类型,绑定失败");
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a4256bde2e254a7f91fc70ea1f7420d8
|
||||
timeCreated: 1698306650
|
Reference in New Issue
Block a user