Files
HX_MapEditor/Assets/Scripts/UI/UINpcsPanel.cs

130 lines
3.6 KiB
C#
Raw Normal View History

2025-06-14 13:46:24 +08:00
using HxGame;
using HxGame.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UINpcsPanel : MonoBehaviour
{
[HideInInspector]
public InputField curActiveInput;
[HideInInspector]
public List<string> curActiveList;
public Transform itemParent;
public Button btnAdd;
// Start is called before the first frame update
void Start()
{
btnAdd.onClick.AddListener(AddItem);
}
void AddItem()
{
UnityEngine.Object obj = Resources.Load("Prefabs/npcItem");
if (obj == null)
{
UIWindow.Instance.ShowMessage("npcItem.prefabʧ<62><CAA7>");
return;
}
GameObject go = Instantiate(obj) as GameObject; ;
go.transform.SetParent(itemParent, false);
int size = MapManager.Instance.AddNpcPointSize();
UINpcItem item = go.GetComponent<UINpcItem>();
item.itemIdx = size;
}
public void AddItem(NpcConfig nc)
{
UnityEngine.Object obj = Resources.Load("Prefabs/npcItem");
if (obj == null)
{
UIWindow.Instance.ShowMessage("npcItem.prefabʧ<62><CAA7>");
return;
}
GameObject go = Instantiate(obj) as GameObject; ;
go.transform.SetParent(itemParent, false);
int size = MapManager.Instance.AddNpcPointSize();
MapManager.Instance.SetCurNpcPointIdx(size);
UINpcItem item = go.GetComponent<UINpcItem>();
item.itemIdx = size;
item.txtID.text = nc.id.ToString();
item.txtPos.text = $"{nc.pos.x},{nc.pos.y}";
item.dropDir.value = (int)nc.dir - 1;
item.paths = nc.paths;
MapManager.Instance.CreateSpecialPoint(nc.pos.x, nc.pos.y, MapManager.EditCellType.NpcCell);
MapManager.Instance.SetCurNpcPathIdx(item.pathIdx);
for (int i=0; i<item.paths.Count; i++)
{
string[] pos = item.paths[i].Split('_');
int x = Convert.ToInt32(pos[0]);
int y = Convert.ToInt32(pos[1]);
item.txtPath.text += $"[{item.paths[i].Replace('_', ',')}]";
MapManager.Instance.AddNpcPathSize();
MapManager.Instance.CreateSpecialPoint(x, y, MapManager.EditCellType.NpcPath);
}
}
public void SaveNpcsConfig()
{
int x, y;
NpcsConfig nc = new NpcsConfig();
for(int i=0; i<itemParent.childCount; i++)
{
UINpcItem item = itemParent.GetChild(i).GetComponent<UINpcItem>();
if (!item.CheckValid())
return;
NpcConfig npc = new NpcConfig();
npc.id = Convert.ToInt32(item.txtID.text);
string[] tmp = item.txtPos.text.Split(',');
if (tmp.Length != 2)
return;
x = Convert.ToInt32(tmp[0]);
y = Convert.ToInt32(tmp[1]);
npc.pos = new Vector2Int(x, y);
npc.dir = (Direction)(item.dropDir.value + 1);
npc.paths = item.paths;
nc.npcConfigs.Add(npc);
}
int mapId = Convert.ToInt32(UIWindow.Instance.uiCreateMap.txtMapID.text);
nc.SaveXML(mapId);
}
public void LoadNpcsConfig(int mapId)
{
NpcsConfig nc = new NpcsConfig();
if (!nc.LoadXML(mapId))
return;
for (int i = 0; i < nc.npcConfigs.Count; i++)
{
AddItem(nc.npcConfigs[i]);
}
}
public void RemoveAll()
{
int count = itemParent.childCount;
for (int i = 0; i < count; i++)
{
DestroyImmediate(itemParent.GetChild(0).gameObject);
}
}
}