这个组合功能功能类似于角色扮演类游戏《妖精组合》中的组合功能,一般是用两个低等级的宠物合成更高等级的宠物。xml表中有合成的公式。
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
public class PetCombine : MonoBehaviour {
public static PetCombine self;
public GameObject parentObj;
public GameObject Box1;
public GameObject Box2;
public GameObject Box3;
public Text TextRemindTwoPets;
public Text TextSorryData;
public IEnumerator WaitSecondShow(){
yield return new WaitForSeconds(1);
if (Box3.transform.FindChild("petItem(Clone)")!=null)
Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject);
InitPetHaveData();
}
public IEnumerator RemindChooseTwoPets()
{
TextRemindTwoPets.gameObject.SetActive(true);
yield return new WaitForSeconds(1);
TextRemindTwoPets.gameObject.SetActive(false);
}
public IEnumerator SorryNoData()
{
TextSorryData.gameObject.SetActive(true);
yield return new WaitForSeconds(1);
TextSorryData.gameObject.SetActive(false);
}
public int ItemNumber
{
get
{
return Fighting.self.PlayerPetItemList.Count;
}
}
private List<GameObject>gridItemobj = new List<GameObject>();
private Hashtable recordPetData = new Hashtable();
void Awake()
{
self = this;
}
void Start()
{
TextRemindTwoPets.gameObject.SetActive(false);
}
void OnEnable()
{
InitPetHaveData();
}
void Update()
{
if (Input.GetMouseButtonUp(0))
{
int id1 = 0, recordid1 = 0;
int id2 = 0, recordid2 = 0;
if ((Box1.transform.FindChild("PlayerPetChooseItem(Clone)") == null || Box2.transform.FindChild("PlayerPetChooseItem(Clone)")==null) && Box3.transform.FindChild("petItem(Clone)") != null)
Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject);
if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)") && Box2.transform.FindChild("PlayerPetChooseItem(Clone)"))
{
GameObject item1 = Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;
id1 = item1.GetComponent<PlayerPetChooseItem>().data.id;
recordid1 = item1.GetComponent<PlayerPetChooseItem>().recordId;
GameObject item2 = Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;
id2 = item2.GetComponent<PlayerPetChooseItem>().data.id;
recordid2 = item2.GetComponent<PlayerPetChooseItem>().recordId;
PetCombineXmlData data = ConfigManager.Instance.GetPetCombineDicData(id1, id2);
if (data == null)
{
if (Box3.transform.FindChild("petItem(Clone)") != null) { Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject); }
}
else
{
int CombineId = 0;
CombineId = data.CombineId;
GameObject gridItem = Resources.Load("Prefabs/item/petItem", typeof(GameObject)) as GameObject;
if (Box3.transform.FindChild("petItem(Clone)") != null)
Debug.Log("原有组合id" + Box3.transform.FindChild("petItem(Clone)").gameObject.GetComponent<PlayerPetChooseItem>().data.id);
Debug.Log("新组合id" + CombineId);
if (Box3.transform.FindChild("petItem(Clone)") != null && Box3.transform.FindChild("petItem(Clone)").gameObject.GetComponent<PlayerPetChooseItem>().data.id != CombineId)
{
Destroy(Box3.transform.FindChild("petItem(Clone)").gameObject);
GameObject obj = GameObject.Instantiate(gridItem) as GameObject;
obj.GetComponent<petItem>().UpdatepetItemData(CombineId);
obj.transform.parent = Box3.transform;
obj.transform.localPosition = Vector3.zero;
obj.transform.localScale = Vector3.one;
}
if (Box3.transform.FindChild("petItem(Clone)") == null)
{
GameObject obj = GameObject.Instantiate(gridItem) as GameObject;
obj.GetComponent<petItem>().UpdatepetItemData(CombineId);
obj.transform.parent = Box3.transform;
obj.transform.localPosition = Vector3.zero;
obj.transform.localScale = Vector3.one;
}
if (Box3.transform.FindChild("petItem(Clone)").gameObject.GetComponent<PlayerPetChooseItem>().data.id == CombineId)
{
}
}
}
}
}
public void InitPetHaveData()
{
recordPetData = DataRecord.GetPetList();
foreach(GameObject obj in gridItemobj)
{
Destroy(obj);
}
gridItemobj.Clear();
GameObject BoxgridItem = Resources.Load("Prefabs/item/BoxGrid", typeof(GameObject)) as GameObject;
GameObject PetgridItem = Resources.Load("Prefabs/item/PlayerPetChooseItem", typeof(GameObject)) as GameObject;
foreach (DictionaryEntry itemx in recordPetData)
{
if (itemx.Value == null)
continue;
Hashtable data = itemx.Value as Hashtable;
GameObject gridobj = GameObject.Instantiate(BoxgridItem) as GameObject;
gridobj.name = "BoxGrid" + (gridItemobj.Count + 1).ToString();
gridobj.transform.parent = parentObj.transform;
gridobj.transform.localPosition = Vector3.zero;
gridobj.transform.localScale = Vector3.one;
gridItemobj.Add(gridobj);
GameObject petobj = GameObject.Instantiate(PetgridItem) as GameObject; ;
petobj.GetComponent<PlayerPetChooseItem>().UpdatePlayerPetChooseItemData(int.Parse(itemx.Key.ToString()), DataRecord.TranslateData(data));
petobj.transform.parent = gridobj.transform;
petobj.transform.localPosition = Vector3.zero;
petobj.transform.localScale = Vector3.one;
}
}
public void OnClickBtnBack()
{
UIManager.instance.HidePanel(UIPanelNames.PetCombinePanel);
RoleControl.self.IsCanMove = false;
RoleControl.self.IsInFight = false;
if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)")!=null)
{ Destroy( Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject); }
if (Box2.transform.FindChild("PlayerPetChooseItem(Clone)")!=null)
{ Destroy( Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject); }
}
public void OnClickCombine()
{
int id1 = 0, recordid1=0;
int id2 = 0, recordid2=0;
if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)") && Box2.transform.FindChild("PlayerPetChooseItem(Clone)"))
{
GameObject item1 = Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;
id1 = item1.GetComponent<PlayerPetChooseItem>().data.id;
recordid1 = item1.GetComponent<PlayerPetChooseItem>().recordId;
GameObject item2 = Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject;
id2 = item2.GetComponent<PlayerPetChooseItem>().data.id;
recordid2 = item2.GetComponent<PlayerPetChooseItem>().recordId;
}
else
{
Debug.Log("error");
StartCoroutine(RemindChooseTwoPets());
return;
}
PetCombineXmlData data = ConfigManager.Instance.GetPetCombineDicData(id1, id2);
if (data == null)
{
Debug.Log("XML数据缺少,待增加");
StartCoroutine(SorryNoData());
}
else
{
int CombineId = 0;
CombineId = data.CombineId;
if (Box1.transform.FindChild("PlayerPetChooseItem(Clone)")) Destroy(Box1.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject);
if (Box2.transform.FindChild("PlayerPetChooseItem(Clone)")) Destroy(Box2.transform.FindChild("PlayerPetChooseItem(Clone)").gameObject);
StartCoroutine(WaitSecondShow());
DataRecord.DelPet(recordid1);
DataRecord.DelPet(recordid2);
DataRecord.AddPet(ConfigManager.Instance.GetPetData(CombineId));
}
}
}
运行动态图: