【Unity学习笔记】背包制作

一、实现背包生成物体

1、第一步,新建一个空物体当做背包背景,命名背包,给它加上Grid Lay Out组件。
2、在背包下面多放几个image视为cell,调好大小、间隔
3、做好cell预置体、和Item预置体
(cell即为背包里的空格子,item是即将要在背包里生成的道具)
4、开始写代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UIBag : MonoBehaviour
{
    public GameObject Cell;
    public GameObject Item;
    private string[] spriteName = {  "Sword", "Thunder" , "redPotion", "bluePotion"};
    List<Transform> cells=new List<Transform>();//存的cell的位置
    void Start()
    {
        bagCell();
    }
    void Update()
    {    
        if (Input.GetKeyDown(KeyCode.A))
        {
            if(Input.GetKey(KeyCode.W))
                createItem(spriteName[0], -1);
            else
                createItem(spriteName[0], 1);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            createItem(spriteName[1], 1);
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            createItem(spriteName[2], 1);
        }
        if (Input.GetKeyDown(KeyCode.G))
        {
            createItem(spriteName[3], 1);
        }
    }
    void bagCell()
    {
        for(int i = 0; i < 9; i++)
        {
            //实例化一个对象,transform为新对象的父对象。
            GameObject cell = Instantiate(Cell,transform);
            cells.Add(cell.transform);
        }
    }
    private void createItem(string Obj,int number)
    {
        //先遍历有Item的cell
        for(int i = 0; i < cells.Count; i++)
        {
            if (cells[i].childCount > 0)
            {
                Transform pos = cells[i].GetChild(0);
               // Debug.Log(pos.GetComponent<Image>().sprite.name);
                if (pos.GetComponent<Image>().sprite.name == Obj)
                {
                    Add(pos.GetComponentInChildren<Text>(), number);
                    return;
                }
            }
        }
        //若所有的cell里没有此装备的话再从第一个空cell里生成
        for (int i = 0; i < cells.Count; i++)
        {
            if (cells[i].childCount == 0)
            {
                //先生成一个物体
                GameObject temp = Instantiate(Item);
                //给它加上Sprite
                temp.GetComponent<Image>().sprite = Resources.Load<Sprite>(Obj);
                //找好爸爸并归零
                temp.transform.SetParent(cells[i]);
                //temp相对于父物体的位置 即localPosition=原点
                temp.transform.localPosition = Vector3.zero;
                //已生成物体,停止for循环
                return;
            }
                
        }
    }
    private void Add(Text text,int number)
    {
        int temp = int.Parse(text.text );
        temp += number;
        if (temp <= 0)
        {
            Destroy(text.transform.parent.gameObject);
        }
            
        text.text = ""+temp;
    }
}

二、实现拖拽物体

1、新建脚本UIDrag并先添加命名空间UnityEngine.EventSystems并添加四个接口: IDragHandler, IEndDragHandler, IBeginDragHandler, ICanvasRaycastFilter
2、开始写代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class UIDrag : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler, ICanvasRaycastFilter
{
    bool isbool = true;//先让此物体能被鼠标检测到
    Transform start;
    Transform canvas;

    public void Awake()
    {
        canvas = GameObject.Find("Canvas").transform;
    }

    //是否能被物体检测到
    public bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
    {
        return isbool;
    }
    public void OnBeginDrag(PointerEventData eventData)//开始拖拽
    {
        //start记录item初始的cell位置
        start = transform.parent;
        //暂时先让item放在canvas下
        transform.SetParent(canvas);
        //使物体不能被检测到,若不这样设置则下面的 go 只能检测到此item
        isbool = false;
    }

    public void OnDrag(PointerEventData eventData)//拖拽中
    {
        //让物体的位置跟随鼠标位置
        transform.position = Input.mousePosition;
    }

    public void OnEndDrag(PointerEventData eventData)//拖拽结束时
    {
        //go为被检测到的物体
        GameObject go = eventData.pointerCurrentRaycast.gameObject;
        if (go)
        {
            Debug.Log(go.tag);
            if (go.tag == "Cell")
            {
                //若此物体为空的cell,则只需将此item放到这个cell下
                ChangeAndPosition(transform, go.transform);
            }
            else if (go.tag == "Item")
            {
                //若此物体为item,则需将两item交换
                ChangeAndPosition(transform, go.transform.parent);
                ChangeAndPosition(go.transform, start);
            }
            else
            {
                //若为别处,则归回到原处
                ChangeAndPosition(transform, start);
            }
            //拖拽完成后使该物体能被检测到
            isbool = true;
        }
    }
    void ChangeAndPosition(Transform child,Transform parent)
    {
        child.SetParent(parent);
        child.position = parent.position;
        child.localScale = Vector3.one;
    }
    
   
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值