Unity之黑暗之光按教程学习第七天

        今天主要就是完成整个背包系统。

        一、物品的拾取

                      这里我们将物品拾取分为三种情况,第一种情况是,我们的物品栏没有物品,则直接将物品实例化,第二种情况我们物品栏中有物品,相同的物品则只增加物品数目,不相同的则寻找空的物品栏,再实例化。


public class Inventory : MonoBehaviour
{

		public static Inventory _instance;

		private TweenPosition tween;
		//private int coinCount = 1000;//金币数量

		public List<Inventory_item_grid> itemGridList = new List<Inventory_item_grid> ();
		public UILabel coinNumberLabel;
		public GameObject inventoryItem;

		void Awake ()
		{
				_instance = this;
				tween = this.GetComponent<TweenPosition> ();
		}

		void Update ()
		{
				if (Input.GetKeyDown (KeyCode.X)) {
						GetId (Random.Range (1001, 1004));
				}
		}

		//拾取到id的物品,并添加到物品栏里面
		//处理拾取物品的功能
		public void GetId (int id)
		{
				//第一步是查找在所有的物品中是否存在该物品
				//第二 如果存在,让num +1

				Inventory_item_grid grid = null;
				foreach (Inventory_item_grid temp in itemGridList) {
						if (temp.id == id) {
								grid = temp; break;
						}
				}
				if (grid != null) {//存在的情况 
						grid.PlusNumber ();
				} else {//不存在的情况
						foreach (Inventory_item_grid temp in itemGridList) {
								if (temp.id == 0) {
										grid = temp; break;
								}
						}
						if (grid != null) {//第三 不过不存在,查找空的方格,然后把新创建的Inventoryitem放到这个空的方格里面
								GameObject itemGo = NGUITools.AddChild (grid.gameObject, inventoryItem);
								itemGo.transform.localPosition = Vector3.zero;
								itemGo.GetComponent<UISprite> ().depth = 4;
								grid.SetId (id);
						}
				}
		}
public class Inventory_item_grid : MonoBehaviour {

		public int id = 0;
		public ObjectInfo info = null;
		public int num = 0;
		private UILabel numLabel;



		void Start ()
		{
				numLabel = this.GetComponentInChildren<UILabel> ();
		}

		public void SetId (int id, int num = 1)
		{
				this.id = id;
				info = ObjectsInfo._instance.GetObjectInfoById (id);
				InventoryItem item = this.GetComponentInChildren<InventoryItem> ();
				item.SetIconName (id,info.icon_name);
				numLabel.enabled = true;
				this.num = num;
				numLabel.text = num.ToString ();
		}


		public void PlusNumber (int num = 1)
		{
				this.num += num;
				numLabel.text = this.num.ToString ();
		}
		//清空格子存储的物品信息
		public void ClearInfo ()
		{
				id = 0;
				info = null;
				num = 0;
				numLabel.enabled = false;
		}
}

               第二步,我们需要实现背包系统中,物品移动的问题,物品移动分多种情况,第一种情况,我们拖拽了物品,但没有将它移到别的位置,则它返回,我们因此定义了一个ResetPosition类,第二种情况就是,将物体拖到空格处,则原来的物品栏信息清空,现有的物品栏添加信息和实例化。第三种情况是两个物品交换位置,则先保存一个物体的ID,num,将grid1用SetID类实现,在将保存的ID,num赋给grid2。<拖拽功能继承于NGUI提供的UIDragDropItem>

public class InventoryItem :UIDragDropItem {

		private UISprite sprite;
		private int id;

		protected override void Awake ()
		{
				base.Awake ();
				sprite = this.GetComponent<UISprite> ();

		}

		protected override void Update ()
		{
				base.Update ();
				if (isHover) {
						InventoryDes._instance.Show (id);
				}
		}

		protected override void OnDragDropRelease (GameObject surface)
		{
				base.OnDragDropRelease (surface);
				if (surface != null) {
						if (surface.tag == Tags.inventory_item_grid) {//当拖放到一个空的格子里面
								if (surface == this.transform.parent.gameObject) {//拖放到了自己的格子里面

								} else {
										Inventory_item_grid OldParent = this.transform.parent.GetComponent<Inventory_item_grid> ();

										this.transform.parent = surface.transform;
										ResetPosition ();
										Inventory_item_grid NewPatent = surface.GetComponent<Inventory_item_grid> ();
										NewPatent.SetId (OldParent.id, OldParent.num);

										OldParent.ClearInfo ();

								}
						} else if (surface.tag == Tags.inventory_item) {//当拖放到了一个有物体的格子里面
								Inventory_item_grid grid1 = this.transform.parent.GetComponent<Inventory_item_grid> ();
								Inventory_item_grid grid2 = surface.transform.parent.GetComponent<Inventory_item_grid> ();
								int id = grid1.id;int num = grid1.num;
								grid1.SetId (grid2.id, grid2.num);
								grid2.SetId (id, num);
						} 
				}

				ResetPosition ();
		}


		void ResetPosition ()
		{
				transform.localPosition = Vector3.zero;
		}

		public void SetId (int id)
		{
				ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById (id);
				sprite.spriteName = info.icon_name;
		}

		public void SetIconName(int id,string icon_name)
		{
				sprite.spriteName = icon_name;
				this.id = id;
		}
		private bool isHover = false;
		public void OnHoverOver ()//鼠标移上
		{
				isHover = true;
		}
		public void OnHoverOut()//鼠标移除
		{
				isHover = false;
		}

}

               最后给Inventory增加一个背景,能够实现我们鼠标移动到物品上能够显示它的属性类。

public class InventoryDes : MonoBehaviour {

		public static InventoryDes _instance;
		private UILabel Label;
		private float timer = 0f;

		void Awake ()
		{
				_instance = this;
				Label = this.GetComponentInChildren<UILabel> ();
				this.gameObject.SetActive (false);
		}
		
	

	    void Update () {
				if(this.gameObject.activeInHierarchy == true) {
						timer -= Time.deltaTime;
						if(timer <= 0) {
								this.gameObject.SetActive (false);
						}
				}
	    }


		public void Show (int id)
		{
				this.gameObject.SetActive (true);timer = 0.1f;
				transform.position =  UICamera.currentCamera.ScreenToWorldPoint (Input.mousePosition);
				ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById (id);
				string des = "";
				switch (info.type) {
				case ObjectType.Drug:
						des = GetDrugDes (info);
						break;
				}
				Label.text = des;
		}

		string GetDrugDes (ObjectInfo info)
		{
				string str = "";
				str += "名称:" + info.name + "\n";
				str += "HP:" + info.hp + "\n";
				str += "MP:" + info.mp + "\n";
				str += "出售价:" + info.price_sell + "\n";
				str += "购买价:" + info.price_buy + "\n";


				return str;
		}
}
Inventory._instance.TransformState ();

             不知是不是NGUI的版本不一样,视频中的Awake,UPdate说不用复写,但是我的版本上都有复写,我也用Protecet orride void XX{ base.XX};复写了,没有错误。

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李曼竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值