您的位置:首页 > 移动开发 > Unity3D

Unity物品栏添加物品c#脚本

2018-01-09 16:24 417 查看
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;
}
}
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: