(client) feat:添加预制件脚本,规范化Dev菜单加载
This commit is contained in:
@ -12,6 +12,7 @@ GameObject:
|
||||
- component: {fileID: 6659858081700418582}
|
||||
- component: {fileID: 7794534526379226234}
|
||||
- component: {fileID: 8827200521422920569}
|
||||
- component: {fileID: 3166707847097429176}
|
||||
m_Layer: 5
|
||||
m_Name: ButtonTemplate
|
||||
m_TagString: Untagged
|
||||
@ -121,6 +122,20 @@ MonoBehaviour:
|
||||
m_OnClick:
|
||||
m_PersistentCalls:
|
||||
m_Calls: []
|
||||
--- !u!114 &3166707847097429176
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 190104900697248908}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 229699c7a1dc4caa84d46e6cfba6a8d8, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
button: {fileID: 8827200521422920569}
|
||||
text: {fileID: 5723833987058492246}
|
||||
--- !u!1 &2444016289016785342
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -11,6 +11,7 @@ GameObject:
|
||||
- component: {fileID: 632585612639644796}
|
||||
- component: {fileID: 6803973761688315705}
|
||||
- component: {fileID: 7303780691383776844}
|
||||
- component: {fileID: 2137672851208466200}
|
||||
m_Layer: 5
|
||||
m_Name: TextTemplate
|
||||
m_TagString: Untagged
|
||||
@ -136,3 +137,16 @@ MonoBehaviour:
|
||||
m_hasFontAssetChanged: 0
|
||||
m_baseMaterial: {fileID: 0}
|
||||
m_maskOffset: {x: 0, y: 0, z: 0, w: 0}
|
||||
--- !u!114 &2137672851208466200
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 27210933623785530}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 13c16a6a94284487838eb5c60f53ed0d, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
text: {fileID: 7303780691383776844}
|
||||
|
@ -1263,8 +1263,8 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
menuContent: {fileID: 175226513}
|
||||
textTemplate: {fileID: 27210933623785530, guid: 4572fd0db4eb91d4588451064f59c91b, type: 3}
|
||||
buttonTemplate: {fileID: 190104900697248908, guid: f0afd08be12de0d43af753af4f618da4, type: 3}
|
||||
textTemplate: {fileID: 2137672851208466200, guid: 4572fd0db4eb91d4588451064f59c91b, type: 3}
|
||||
buttonTemplate: {fileID: 3166707847097429176, guid: f0afd08be12de0d43af753af4f618da4, type: 3}
|
||||
--- !u!1 &2001208285
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
using Utils;
|
||||
@ -27,12 +29,20 @@ namespace Managers
|
||||
/// </remarks>
|
||||
public void Init()
|
||||
{
|
||||
if (packs.Count > 0)
|
||||
return;
|
||||
// 单线程
|
||||
var packFolder = Configs.ConfigProcessor.GetSubFolders(new(dataSetFilePath));
|
||||
foreach (var folder in packFolder)
|
||||
{
|
||||
var pack = new DefinePack();
|
||||
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
|
||||
if (pack.LoadPack(folder))
|
||||
{
|
||||
packs.Add(pack.packID, pack);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Dictionary<Type, FieldInfo[]> fieldCache = new();
|
||||
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
|
||||
HashSet<Define> processedDefines = new(); // 用于跟踪已处理的 Define 对象
|
||||
@ -102,7 +112,14 @@ namespace Managers
|
||||
{
|
||||
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
|
||||
}
|
||||
Debug.Log(anonymousDefines.Count);
|
||||
}
|
||||
|
||||
public void Reload()
|
||||
{
|
||||
defines.Clear();
|
||||
packs.Clear();
|
||||
anonymousDefines.Clear();
|
||||
Init();
|
||||
}
|
||||
/// <summary>
|
||||
/// 查找指定定义类型的定义名对应的 Define 对象。
|
||||
@ -158,36 +175,6 @@ namespace Managers
|
||||
return defineList.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询 Define 对象。
|
||||
/// </summary>
|
||||
/// <param name="defineType">定义类型(外层字典的键)。</param>
|
||||
/// <param name="defineName">定义名(内层字典的键)。</param>
|
||||
/// <returns>如果找到,则返回 Define 对象;否则返回 null。</returns>
|
||||
public Define QueryDefine(string defineType, string defineName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
{
|
||||
Debug.LogError("查询失败:定义类型参数不能为空!");
|
||||
return null;
|
||||
}
|
||||
if (string.IsNullOrEmpty(defineName))
|
||||
{
|
||||
Debug.LogError("查询失败:定义名参数不能为空!");
|
||||
return null;
|
||||
}
|
||||
if (!defines.TryGetValue(defineType, out var typeDefinitions))
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
|
||||
return null;
|
||||
}
|
||||
if (!typeDefinitions.TryGetValue(defineName, out var targetDefine))
|
||||
{
|
||||
Debug.LogWarning($"查询失败:定义类型 '{defineType}' 中未找到定义名 '{defineName}'");
|
||||
return null;
|
||||
}
|
||||
return targetDefine;
|
||||
}
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象。(包括匿名定义)
|
||||
/// </summary>
|
||||
@ -264,6 +251,78 @@ namespace Managers
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象(仅包含命名定义,不包括匿名定义)。
|
||||
/// </summary>
|
||||
/// <param name="defineType">定义类型(外层字典的键)。</param>
|
||||
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
|
||||
public Define[] QueryNamedDefinesByType(string defineType)
|
||||
{
|
||||
if (string.IsNullOrEmpty(defineType))
|
||||
{
|
||||
Debug.LogError("查询失败:定义类型参数不能为空!");
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Define> result = new List<Define>();
|
||||
|
||||
// 仅从命名定义中查询
|
||||
if (defines.TryGetValue(defineType, out var namedDefinitions))
|
||||
{
|
||||
result.AddRange(namedDefinitions.Values);
|
||||
}
|
||||
|
||||
// 如果结果为空,则返回 null
|
||||
if (result.Count == 0)
|
||||
{
|
||||
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}' 的命名定义");
|
||||
return null;
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型(仅包含命名定义,不包括匿名定义)。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">目标类型。</typeparam>
|
||||
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
|
||||
public T[] QueryNamedDefinesByType<T>()
|
||||
{
|
||||
var defineType = typeof(T).Name;
|
||||
|
||||
List<Define> allDefines = QueryNamedDefinesByType(defineType)?.ToList();
|
||||
if (allDefines == null || allDefines.Count == 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// 尝试将所有 Define 对象转换为目标类型 T
|
||||
var result = new List<T>();
|
||||
foreach (var item in allDefines)
|
||||
{
|
||||
if (item is T converted)
|
||||
{
|
||||
result.Add(converted);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"类型转换失败:无法将 {item.GetType().Name} 转换为 {typeof(T).Name}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToArray();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"类型转换失败:从 Define 转换为 {typeof(T).Name} 时出错。错误信息:{ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public override string ToString()
|
||||
{
|
||||
if (packs == null || packs.Count == 0)
|
||||
|
3
Client/Assets/Scripts/Prefab.meta
Normal file
3
Client/Assets/Scripts/Prefab.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 765878e48a7349a98e57309179eebe4d
|
||||
timeCreated: 1752918159
|
26
Client/Assets/Scripts/Prefab/ButtonPrefab.cs
Normal file
26
Client/Assets/Scripts/Prefab/ButtonPrefab.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class ButtonPrefab:MonoBehaviour
|
||||
{
|
||||
public Button button;
|
||||
public TMP_Text text;
|
||||
|
||||
public string Label
|
||||
{
|
||||
get{return text.text;}
|
||||
set{text.text = value;}
|
||||
}
|
||||
|
||||
public void AddListener(UnityAction callback)
|
||||
{
|
||||
button.onClick.AddListener(callback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
3
Client/Assets/Scripts/Prefab/ButtonPrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/ButtonPrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 229699c7a1dc4caa84d46e6cfba6a8d8
|
||||
timeCreated: 1752918181
|
17
Client/Assets/Scripts/Prefab/TextPrefab.cs
Normal file
17
Client/Assets/Scripts/Prefab/TextPrefab.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class TextPrefab : MonoBehaviour
|
||||
{
|
||||
public TMP_Text text;
|
||||
|
||||
public string Label
|
||||
{
|
||||
get{return text.text;}
|
||||
set{text.text = value;}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/TextPrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/TextPrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 13c16a6a94284487838eb5c60f53ed0d
|
||||
timeCreated: 1752918417
|
@ -8,9 +8,10 @@ namespace UI
|
||||
public class DevMenuUI : MonoBehaviour
|
||||
{
|
||||
public GameObject menuContent;
|
||||
|
||||
public GameObject textTemplate;
|
||||
public GameObject buttonTemplate;
|
||||
|
||||
public Prefab.TextPrefab textTemplate;
|
||||
public Prefab.ButtonPrefab buttonTemplate;
|
||||
|
||||
void Start()
|
||||
{
|
||||
Init();
|
||||
@ -18,9 +19,62 @@ namespace UI
|
||||
|
||||
void Init()
|
||||
{
|
||||
|
||||
Managers.DefineManager.Instance.Init();
|
||||
InitEvent();
|
||||
InitCharacter();
|
||||
}
|
||||
|
||||
void InitEvent()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "事件菜单";
|
||||
// for (int i = 0; i < 30; i++)
|
||||
// {
|
||||
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
// button.text.text = i.ToString();
|
||||
// }
|
||||
}
|
||||
|
||||
void InitCharacter()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "点击切换人物";
|
||||
|
||||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
|
||||
foreach (var def in defList)
|
||||
{
|
||||
var button=InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
button.Label = def.label;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 通用的实例化函数,返回实例化的预制件脚本组件。
|
||||
/// </summary>
|
||||
/// <typeparam name="T">预制件脚本的类型</typeparam>
|
||||
/// <param name="prefab">要实例化的预制件</param>
|
||||
/// <param name="parent">实例化对象的父对象</param>
|
||||
/// <returns>实例化的预制件脚本组件</returns>
|
||||
T InstantiatePrefab<T>(T prefab, Transform parent) where T : Component
|
||||
{
|
||||
if (prefab == null || parent == null)
|
||||
{
|
||||
Debug.LogError("Prefab or parent is null!");
|
||||
return null;
|
||||
}
|
||||
|
||||
// 实例化预制件
|
||||
GameObject instance = Instantiate(prefab.gameObject, parent);
|
||||
|
||||
// 获取实例化对象的脚本组件
|
||||
T instantiatedComponent = instance.GetComponent<T>();
|
||||
|
||||
if (instantiatedComponent == null)
|
||||
{
|
||||
Debug.LogError($"Failed to get component of type {typeof(T).Name} from the instantiated prefab!");
|
||||
}
|
||||
|
||||
return instantiatedComponent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user