168 lines
5.9 KiB
C#
168 lines
5.9 KiB
C#
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Data;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
namespace UI
|
||
{
|
||
public class DevMenuUI : UIBase
|
||
{
|
||
public GameObject menuContent;
|
||
|
||
public EntityPlacementUI entityPlacementUI;
|
||
|
||
public Prefab.TextPrefab textTemplate;
|
||
public Prefab.ButtonPrefab buttonTemplate;
|
||
|
||
private void Start()
|
||
{
|
||
Init();
|
||
}
|
||
|
||
private void Init()
|
||
{
|
||
InitEvent();
|
||
InitCharacter();
|
||
InitMonster();
|
||
InitBuilding();
|
||
}
|
||
|
||
|
||
private void InitEvent()
|
||
{
|
||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "事件菜单";
|
||
|
||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "未定义任何事件";
|
||
title.text.color = Color.red;
|
||
// for (int i = 0; i < 30; i++)
|
||
// {
|
||
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||
// button.text.text = i.ToString();
|
||
// }
|
||
|
||
}
|
||
|
||
private void InitCharacter()
|
||
{
|
||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "生成人物";
|
||
|
||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
|
||
if (defList == null || defList.Length == 0)
|
||
{
|
||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "未定义任何角色";
|
||
title.text.color = Color.red;
|
||
}
|
||
else
|
||
foreach (var def in defList)
|
||
{
|
||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||
button.Label = def.label;
|
||
var pawnDef = def;
|
||
button.AddListener(() => GenerateEntityCallback(pawnDef));
|
||
}
|
||
|
||
}
|
||
|
||
private void InitMonster()
|
||
{
|
||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "生成怪物";
|
||
|
||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.MonsterDef>();
|
||
if (defList == null || defList.Length == 0)
|
||
{
|
||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "未定义任何怪物";
|
||
title.text.color = Color.red;
|
||
}
|
||
else
|
||
foreach (var def in defList)
|
||
{
|
||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||
button.Label = def.label;
|
||
var pawnDef = def;
|
||
button.AddListener(() => GenerateEntityCallback(pawnDef));
|
||
}
|
||
}
|
||
|
||
private void InitBuilding()
|
||
{
|
||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "生成建筑";
|
||
|
||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.BuildingDef>();
|
||
if (defList == null || defList.Length == 0)
|
||
{
|
||
title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||
title.Label = "未定义任何建筑";
|
||
title.text.color = Color.red;
|
||
}
|
||
else
|
||
foreach (var def in defList)
|
||
{
|
||
var button = InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||
button.Label = def.label;
|
||
var pawnDef = def;
|
||
button.AddListener(() => GenerateBuildingCallback(pawnDef));
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 通用的实例化函数,返回实例化的预制件脚本组件。
|
||
/// </summary>
|
||
/// <typeparam name="T">预制件脚本的类型</typeparam>
|
||
/// <param name="prefab">要实例化的预制件</param>
|
||
/// <param name="parent">实例化对象的父对象</param>
|
||
/// <returns>实例化的预制件脚本组件</returns>
|
||
private T InstantiatePrefab<T>(T prefab, Transform parent) where T : Component
|
||
{
|
||
if (prefab == null || parent == null)
|
||
{
|
||
Debug.LogError("Prefab or parent is null!");
|
||
return null;
|
||
}
|
||
|
||
// 实例化预制件
|
||
var instance = Instantiate(prefab.gameObject, parent);
|
||
|
||
// 获取实例化对象的脚本组件
|
||
var instantiatedComponent = instance.GetComponent<T>();
|
||
|
||
if (instantiatedComponent == null)
|
||
{
|
||
Debug.LogError($"Failed to get component of type {typeof(T).Name} from the instantiated prefab!");
|
||
}
|
||
|
||
return instantiatedComponent;
|
||
}
|
||
|
||
private void GenerateEntityCallback(PawnDef pawnDef)
|
||
{
|
||
entityPlacementUI.currentAction = () =>
|
||
{
|
||
Managers.EntityManage.Instance.GenerateEntity(pawnDef, Utils.MousePosition.GetWorldPosition());
|
||
};
|
||
entityPlacementUI.Prompt = $"当前生成器:\n名称:{pawnDef.label}\n描述:{pawnDef.description}";
|
||
entityPlacementUI.snapEnabled = false;
|
||
Base.UIInputControl.Instance.Show(entityPlacementUI);
|
||
}
|
||
|
||
private void GenerateBuildingCallback(BuildingDef def)
|
||
{
|
||
entityPlacementUI.currentAction = () =>
|
||
{
|
||
Managers.EntityManage.Instance.GenerateBuildingEntity(def, Utils.MousePosition.GetSnappedWorldPosition());
|
||
};
|
||
entityPlacementUI.Prompt = $"当前生成器:\n名称:{def.label}\n描述:{def.description}";
|
||
entityPlacementUI.snapEnabled = true;
|
||
Base.UIInputControl.Instance.Show(entityPlacementUI);
|
||
}
|
||
}
|
||
|
||
}
|