阵型设置

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
using System.IO;
//using System.Linq;


public class PetListSet : MonoBehaviour {
//GameObject pet;
public Button petButton;
public Image formationImage;
public PetList selectPetList;
ArrayList battleList;
Dictionary<string, PetList> formation;
Dictionary<string, PetList> petDictionary;
// Use this for initialization
void Start () {
battleList = new ArrayList (5);


string data = System.IO.File.ReadAllText ("Assets/GameData/PetList.txt");
petDictionary = fastJSON.JSON.ToObject<Dictionary<string, PetList>> (data);
//遍历dictionary类型的需要keyValuePair<key,value>方式
foreach(KeyValuePair<string, PetList> petInfo in petDictionary){


Button go = Instantiate (petButton) as Button;
//获取组件要和类的名字对应(FormationButton),而不是和声明的属性对应
PetList pet = go.GetComponent<FormationButton> ().petList;
pet = petInfo.Value;
go.name = pet.Name;
//注意,这里是获取自己子元素的组件的文本
go.GetComponentInChildren<Text>().text = pet.Name;
go.transform.SetParent (transform);
go.GetComponent<Image> ().sprite  = Resources.Load(pet.Icon,typeof(Sprite))as Sprite;
//Addlistener()只能传没有参数的方法,传有参数的方法需要用delegate
go.onClick.AddListener (delegate {
selectFormation(pet);


});    
}


Button[] buttons = formationImage.GetComponentsInChildren<Button>();
foreach (Button button in buttons) {


button.onClick.AddListener (delegate {
setFormation(button);
});
}
}
// private void petShow(){
// Debug.Log ("----------------");
// Destroy (pet);
// petPrefab = Resources.Load ("C324_root",typeof(GameObject)) as GameObject;
// pet = Instantiate (petPrefab) as GameObject;
// pet.transform.SetParent (petShowObj.transform);
// }
private void selectFormation(PetList petList){
selectPetList = null;
selectPetList = petList;
Debug.Log (selectPetList.Name);
}
private void setFormation(Button button){
//需要判断battleList是否已满


PetList pet = button.GetComponent<FormationButton> ().petList;


if (pet == null) {
if (selectPetList != null) {
//如果布阵栏为空,且有选中的目标时,新增对象
if (battleList.Count < 5) {
pet = selectPetList;
button.GetComponentInChildren<Text> ().text = pet.Name;
button.GetComponent<Image> ().sprite = Resources.Load (pet.Icon, typeof(Sprite))as Sprite;
battleList.Add (pet);
selectPetList = null;
Debug.Log (battleList.Count + "个宠物已出战");
} else {
Debug.Log ("出阵的宠物已满");
selectPetList = null;
}
}
} else {
if (selectPetList != null) {
//如果布阵栏不为空,且有选中的目标时,从布阵栏删除当前已布阵的对象,并且新增选中的对象
battleList.Remove (pet);
pet = selectPetList;
button.GetComponentInChildren<Text> ().text = pet.Name;
button.GetComponent<Image> ().sprite = Resources.Load (pet.Icon, typeof(Sprite))as Sprite;
battleList.Add (pet);
selectPetList = null;
} else {
//如果布阵栏不为空,且没有选中的目标时,从布阵栏删除当前已布阵的对象
pet = null;
button.GetComponentInChildren<Text> ().text = null;
button.GetComponent<Image> ().sprite = null;
battleList.Remove (pet);
}
}


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值