NGUI,背包拖拽,以及随机克隆图片知识点

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
*文件描述:
*创始人:
*创建时间:
*修改时间:
*版本:1.0
*/

public class Knapsackltem : UIDragDropItem{
    int count = 1;
    public UILabel numTxt;//预设体上面的文本
    public void Add()//添加物品的数量方法
    {
        count++;//一直累加
        numTxt.text = count.ToString();//赋值给文本
    }

    protected override void OnDragDropRelease(GameObject surface)//OnDragDropRelease:拖放,丢弃,释放
    {
        base.OnDragDropRelease(surface);//调用父类的方法
        print(surface);//碰撞的物体

        if (surface.CompareTag("GeZi"))
        {
            //物品居中
         
  transform.parent = surface.transform;//把靴子放到格子里面
            transform.localPosition = Vector3.zero;//把靴子放在格子的正中间

        }
        else if (surface.CompareTag("WuPin"))
        {
            //物品交换
            Transform parent = surface.transform.parent;//护腕的父对象

            surface.transform.parent = transform.parent;//把护腕放入靴子下面
            surface.transform.localPosition = Vector3.zero;//护腕居中 

            transform.parent = parent;//把靴子放入护腕下面
            transform.localPosition = Vector3.zero;//靴子居中

        }
        else
        {
            Destroy(gameObject);
        }
    }
}
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*
*文件描述:
*创始人:
*创建时间:
*修改时间:
*版本:1.0
*/

public class MyKnapsack : MonoBehaviour {
    public GameObject[] cells;//格子
    public string[] names;//物品名称
    public GameObject item;//当前物品

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            PickUp();
        }
    }
    void PickUp()
    {
        int index = Random.Range(0, names.Length);//位置
        string name = names[index];//随机物品图片名称

        for (int i = 0; i < cells.Length; i++)//随机格子
        {
            if (cells[i].transform.childCount == 0)//格子里面没有物品
            {
                GameObject go = NGUITools.AddChild(cells[i], item);//克隆一个物品放到格子里面
                go.transform.GetComponent<UISprite>().spriteName = name;//更换物品名称
                go.transform.localPosition = Vector3.zero;//物品居中
                break;

            }
            else//格子里面有物品
            {
                //找到预设体上面的图片
                UISprite sprite = cells[i].transform.GetChild(0).GetComponent<UISprite>();
                if (sprite.spriteName.Equals(name))
                {
                    sprite.GetComponent<Knapsackltem>().Add();//调用预设体上面的脚本
                    break;

                }
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值