当前位置:网站首页>Ngui, backpack drag and drop, and random cloning of picture knowledge points

Ngui, backpack drag and drop, and random cloning of picture knowledge points

2022-06-11 10:27:00 ying1228475251

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

/*
* File description :
* founder :
* Creation time :
* Modification time :
* edition :1.0
*/

public class Knapsackltem : UIDragDropItem{
    int count = 1;
    public UILabel numTxt;// The text above the default body
    public void Add()// How to add the quantity of items
    {
        count++;// Always add up
        numTxt.text = count.ToString();// Assign to text
    }

    protected override void OnDragDropRelease(GameObject surface)//OnDragDropRelease: Drag and drop , discarded , Release
    {
        base.OnDragDropRelease(surface);// Calls a method of the parent class
        print(surface);// Colliding objects

        if (surface.CompareTag("GeZi"))
        {
            // Item Center
         
  transform.parent = surface.transform;// Put your boots in the grid
            transform.localPosition = Vector3.zero;// Put your boots right in the middle of the grid

        }
        else if (surface.CompareTag("WuPin"))
        {
            // Goods exchange
            Transform parent = surface.transform.parent;// The parent of the wrist guard

            surface.transform.parent = transform.parent;// Put the wrist guard under your boots
            surface.transform.localPosition = Vector3.zero;// The wrist guard is centered  

            transform.parent = parent;// Put the boots under the wrist guard
            transform.localPosition = Vector3.zero;// Boots centered

        }
        else
        {
            Destroy(gameObject);
        }
    }
}
 

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

/*
* File description :
* founder :
* Creation time :
* Modification time :
* edition :1.0
*/

public class MyKnapsack : MonoBehaviour {
    public GameObject[] cells;// lattice
    public string[] names;// Item name
    public GameObject item;// Current items

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            PickUp();
        }
    }
    void PickUp()
    {
        int index = Random.Range(0, names.Length);// Location
        string name = names[index];// Random item picture name

        for (int i = 0; i < cells.Length; i++)// Random lattice
        {
            if (cells[i].transform.childCount == 0)// There are no items in the grid
            {
                GameObject go = NGUITools.AddChild(cells[i], item);// Clone an item and put it in the grid
                go.transform.GetComponent<UISprite>().spriteName = name;// Change item name
                go.transform.localPosition = Vector3.zero;// Item Center
                break;

            }
            else// There are items in the grid
            {
                // Find the picture above the preset
                UISprite sprite = cells[i].transform.GetChild(0).GetComponent<UISprite>();
                if (sprite.spriteName.Equals(name))
                {
                    sprite.GetComponent<Knapsackltem>().Add();// Call the script above the preset body
                    break;

                }
            }
        }
    }
}
 

原网站

版权声明
本文为[ying1228475251]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111020207400.html