上海杨浦大连路地铁站单机版电子沙盘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

179 lines
5.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using AX.MessageSystem;
using System;
public class BagPanel : ResourceLoadPanel<BagPanel>
{
private Transform content;
private GameObject equipItem;
private const string imagepath = "UI/EquipImage/";
public GameObject selectFire;
// Use this for initialization
void Awake()
{
content = transform.Find("Main/Viewport/Content");
equipItem = Resources.Load("UI/EquipUI/BagItem") as GameObject;
MessageDispatcher.AddListener("BagChaneg", BagInfoChange);
// MessageDispatcher.AddListener("RADIO_SELECTED_COMMAND", ChangeSelect);//单选选中处理
MessageDispatcher.AddListener("SelectChange", selectchange);
}
private void OnEnable()
{
MessageDispatcher.AddListener("BagChaneg", BagInfoChange);
// MessageDispatcher.AddListener("RADIO_SELECTED_COMMAND", ChangeSelect);//单选选中处理
//每次打开背包根据选择消防员加载数据
if (content.childCount > 0)
{
Empty();
}
if (SelectedObjs.selectedCharacters[0] != null)
{
Load(SelectedObjs.selectedCharacters[0].GetComponent<Bag>().EquipList);
}
}
private void OnDisable()
{
//隐藏背包时清空
Empty();
MessageDispatcher.RemoveListener("BagChaneg", BagInfoChange);
}
private void OnDestroy()
{
MessageDispatcher.RemoveListener("BagChaneg", BagInfoChange);
// MessageDispatcher.RemoveListener("RADIO_SELECTED_COMMAND", ChangeSelect);//单选选中处理
MessageDispatcher.RemoveListener("SelectChange", selectchange);
}
private void selectchange(IMessage obj)
{
gameObject.SetActive(false);
}
private void Empty()
{
for (int i = 0; i < content.childCount; i++)
{
Destroy(content.GetChild(i).gameObject);
}
}
private void Load(List<Equip> Equipdata)
{
for (int i = 0; i < Equipdata.Count; i++)
{
//原来没有,新添
if (!content.Find(Equipdata[i].Name))
{
if (!Equipdata[i].IsSelectMore||(Equipdata[i].IsSelectMore&& Equipdata[i].Number>0))
{
GameObject item = Instantiate(equipItem, content);
item.name = Equipdata[i].Name;
//设置名称
item.transform.Find("Name").GetComponent<Text>().text = Equipdata[i].Name;
//设置图片
item.transform.Find("Image").GetComponent<Image>().sprite =
Resources.Load(imagepath + Equipdata[i].PicName, typeof(Sprite)) as Sprite;
//设置数量
if (Equipdata[i].IsSelectMore)
{
item.transform.Find("NUM").gameObject.SetActive(true);
item.transform.Find("NUM").GetComponent<Text>().text = Equipdata[i].Number.ToString();
}
}
}
////数量叠加
//else
//{
// Transform item = content.Find(Equipdata[i].Name);
// if (Equipdata[i].IsSelectMore)
// {
// Text num = item.transform.Find("NUM").GetComponent<Text>();
// num.text = (Equipdata[i].Number + int.Parse(num.text)).ToString();
// }
//}
}
}
/// <summary>
/// 装备更改时刷新UI
/// </summary>
/// <param name="Equipdata"></param>
IEnumerator Refush(List<Equip> Equipdata)
{
yield return new WaitForSeconds(0.05f);
Load(Equipdata);
}
/// <summary>
/// 当前消防员背包信息修改
/// </summary>
/// <param name="obj"></param>
private void BagInfoChange(IMessage obj)
{
if (gameObject.activeInHierarchy)
{
GameObject select = (GameObject)obj.Data;
if (select == SelectedObjs.selectedCharacters[0])
{
Empty();
StartCoroutine( Refush(select.GetComponent<Bag>().EquipList));
}
}
}
/// <summary>
/// 打开背包的情况下换选消防员
/// </summary>
/// <param name="obj"></param>
private void ChangeSelect(IMessage obj)
{
if (gameObject.activeInHierarchy)
{
var gameObjID = (long)obj.Sender;
var hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID);
if (hitObj.CompareTag("Player"))
{
Empty();
Refush(hitObj.GetComponent<Bag>().EquipList);
}
}
}
public void ClearAll()
{
List<string> eqname = new List<string>();
Toggle[] all = content.GetComponentsInChildren<Toggle>(true);
for (int i = 0; i < all.Length; i++)
{
eqname.Add(all[i].name);
}
MessageDispatcher.SendMessage("DelectEquip", eqname);
}
public void ClearSelect()
{
MessageDispatcher.SendMessage("DelectEquip", getClearlistname());
}
/// <summary>
/// 获取要清空的装备的名字的列表
/// </summary>
/// <returns></returns>
private List<string> getClearlistname()
{
List<string> eqname = new List<string>();
Toggle[] all = content.GetComponentsInChildren<Toggle>();
for (int i = 0; i < all.Length; i++)
{
if (all[i].isOn)
{
eqname.Add(all[i].name);
}
}
return eqname;
}
}