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;
}
}
}
}
}
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;
}
}
}
}
}
相关文章推荐
- Unity用GUI在角色头上显示名字(C#脚本)
- Unity 3D功能主界面C#脚本
- C#Light(包括unity一切C#环境可用嵌入式脚本)0.10B稳定版发布,功能已定型
- Unity 3D视频播放器场景C#脚本
- C#开发Unity游戏教程之Scene视图与脚本的使用
- 2.《Unity中C#脚本语法-常用变量类型》
- unity C#脚本开发基础
- Unity 通过代码获取从面板传入的mono脚本,并添加为GameObject的脚本组件
- Unity修改C#默认创建Script 脚本格式
- 【Unity】2.10 利用VS2015编辑Unity的C#脚本
- Unity。。。刚体在c#脚本里调用rigidbody.freezePosition或者冻结任意一个轴或组合冻结
- 【Unity开发技巧】Unity脚本自动添加注释头
- Unity脚本开发基础 C#
- C#开发Unity游戏教程之使用脚本变量
- Unity 修改默认创建 C# Script 脚本格式
- Unity 给Mono脚本添加Try Catch工具
- Unity引擎与C#脚本简介
- C# 脚本存在unsafe字段导致Unity编译不通过
- unity JavaScript 与 c# 脚本之间的通信
- Unity 脚本 自动添加 脚本信息