1. 程式人生 > >UGUI揹包系統

UGUI揹包系統

博主cc最近用UGUI實現了一個拼圖遊戲,類似於揹包系統(缺少揹包系統中資料部分),包含了物品的拖拉拽,物品交換等操作 物品交換操作原理

  1. 指令碼實現 IBeginDragHandler, IDragHandler, IEndDragHandler中的方法,進行物品移動的操作
  2. 結束拖拽時,判斷滑鼠下面是否有物品,如果有就交換(交換父物體,並重置),否則物品歸回原位置
  3. 難點:物品拖拽時,若其他物品先渲染,會遮擋住正在拖拽的物體;解決方法:拖拽時,將物體父級設定為Canvas,結束拖拽後再設為原父級。

在這裡插入圖片描述

ImageLoad:用來載入圖片

using UnityEngine;
using UnityEngine.
UI; using System.Collections.Generic; public class ImageLoad : MonoBehaviour { public GameObject gridItemPrefab; private List<Item> itemList; void Start() { itemList = new List<Item>(); InitItems(); } //檢測是否遊戲成功 private void OnItemEndDrag() {
bool isSuccess = true; foreach (Item item in itemList) { if (item.IsPositionRight == false) { isSuccess = false; break; } } if (isSuccess) { Debug.Log("拼圖成功!"); }
else { Debug.Log("圖片還未拼成功!"); } } private void InitItems() { Sprite[] imgArr = Resources.LoadAll<Sprite>("Image/testImg"); int length = imgArr.Length; //儲存實際的realIndex int[] realIndexArr = new int[length]; for (int i = 0; i < length; i++) { realIndexArr[i] = i; } //打亂順序 for (int i = 0; i < length; i++) { int numRandom = Random.Range(0, length); Sprite tempSprite = imgArr[i]; imgArr[i] = imgArr[numRandom]; imgArr[numRandom] = tempSprite; int tempIndex = realIndexArr[i]; realIndexArr[i] = realIndexArr[numRandom]; realIndexArr[numRandom] = tempIndex; } for (int i = 0; i < length; i++) { Transform gridItem = GameObject.Instantiate(gridItemPrefab, transform).transform; Transform item = gridItem.Find("Item"); item.tag = "ItemGrid"; item.GetComponent<Image>().sprite = imgArr[i]; Item tempItem = item.GetComponent<Item>(); tempItem.SetIndex(realIndexArr[i], i); tempItem.AddEndDragListener(OnItemEndDrag); itemList.Add(tempItem); } } }

Item:用來控制物品的交換

using UnityEngine;
using UnityEngine.EventSystems;
using System;

public class Item : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private Vector3 offset;         //偏移量
    private Vector3 originPosition;  //拖拽前的位置
    private Transform canvasTransform;
    private Transform originParentTransform;   //父物件
    private CanvasGroup group;

    private int realIndex;  //實際的index
    [HideInInspector]
    public int curIndex;   //當前的index

    public bool IsPositionRight
    {
        get
        {
            if(realIndex != curIndex)
            {
                Debug.Log("實際位置:" + realIndex + ",當前位置:" + curIndex);
            }
            return realIndex == curIndex;
        }
    }

    private event Action EndDragEvent;

    void Awake()
    {
        canvasTransform = GameObject.Find("Canvas").transform;
        group = transform.GetComponent<CanvasGroup>();
    }

    public void SetIndex(int realIndex, int curIndex)
    {
        this.realIndex = realIndex;
        this.curIndex = curIndex;
    }

    public void AddEndDragListener(Action method)
    {
        EndDragEvent += method;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        originParentTransform = transform.parent;
        offset = transform.position - Input.mousePosition;
        originPosition = transform.position;
        transform.SetParent(canvasTransform);
        group.blocksRaycasts = false;
    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = Input.mousePosition + offset;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        GameObject tempGameObject = eventData.pointerCurrentRaycast.gameObject;
        if (tempGameObject != null && tempGameObject.tag == "ItemGrid")
        {
            transform.SetParent(tempGameObject.transform.parent);

            tempGameObject.transform.SetParent(originParentTransform);
            tempGameObject.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;

            Item tempItem = tempGameObject.GetComponent<Item>();

            int tempCurIndex = tempItem.curIndex;
            tempItem.curIndex = this.curIndex;
            this.curIndex = tempCurIndex;
        }
        else
        {
            transform.SetParent(originParentTransform);
        }
        transform.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
        group.blocksRaycasts = true;

        if (EndDragEvent != null)
        {
            EndDragEvent();
        }
    }

}