using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AddEquip : MonoBehaviour {
public GameObject equipPrefab;
Transform Kanpsack;
public GameObject itemPrefab;
void Awake(){
Kanpsack = GameObject.Find ("Knapsack").transform;
}
void Start () {
for (int i = 0; i < 26; i++) {
Sprite sp = Resources.Load<Sprite> (i.ToString ());
GameObject g = Instantiate<GameObject> (equipPrefab, transform);
g.GetComponent<Image> ().sprite = sp;
g.GetComponent<Button> ().onClick.AddListener (delegate () {
AddEquips(sp);
});
}
}
/// <summary>
/// 往背包的格子里加物品
/// </summary>
/// <param name="sp">Sp.</param>
void AddEquips(Sprite sp){
for (int i = 0; i < Kanpsack.childCount; i++) {
Transform cell = Kanpsack.GetChild (i);
if (cell.childCount == 0) {
GameObject go = Instantiate<GameObject> (itemPrefab, cell);
go.transform.localPosition = Vector3.zero;
go.GetComponent<Image> ().sprite = sp;
return;
}
if (cell.childCount > 0) {
if (cell.GetChild (0).GetComponent<Image> ().sprite.name==sp.name) {
Text text = cell.GetChild (0).GetChild(0).GetComponent<Text> ();
int count = int.Parse (text.text);
count++;
text.text = count.ToString ();
return;
}
}
}
}
}
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AddEquip : MonoBehaviour {
public GameObject equipPrefab;
Transform Kanpsack;
public GameObject itemPrefab;
void Awake(){
Kanpsack = GameObject.Find ("Knapsack").transform;
}
void Start () {
for (int i = 0; i < 26; i++) {
Sprite sp = Resources.Load<Sprite> (i.ToString ());
GameObject g = Instantiate<GameObject> (equipPrefab, transform);
g.GetComponent<Image> ().sprite = sp;
g.GetComponent<Button> ().onClick.AddListener (delegate () {
AddEquips(sp);
});
}
}
/// <summary>
/// 往背包的格子里加物品
/// </summary>
/// <param name="sp">Sp.</param>
void AddEquips(Sprite sp){
for (int i = 0; i < Kanpsack.childCount; i++) {
Transform cell = Kanpsack.GetChild (i);
if (cell.childCount == 0) {
GameObject go = Instantiate<GameObject> (itemPrefab, cell);
go.transform.localPosition = Vector3.zero;
go.GetComponent<Image> ().sprite = sp;
return;
}
if (cell.childCount > 0) {
if (cell.GetChild (0).GetComponent<Image> ().sprite.name==sp.name) {
Text text = cell.GetChild (0).GetChild(0).GetComponent<Text> ();
int count = int.Parse (text.text);
count++;
text.text = count.ToString ();
return;
}
}
}
}
}