1. 程式人生 > >UGui揹包系統(例項化裝備並且給角色換裝,實現不同的裝備放入相應的人物物品欄中)

UGui揹包系統(例項化裝備並且給角色換裝,實現不同的裝備放入相應的人物物品欄中)

最近也是跟著玩玩UGUI揹包系統,簡單的寫了一下。希望對初學者有所幫助。

首先就是Unity介面的搭建(這裡我用的是網上找到圖片):

如上圖所示:

隨便找一個遊戲背景圖片(我的是BG),BG下面佈局好Text,內容為“物品欄”,然後在建立一個Image,下面是物品欄裡裡面的裝備圖片,如下圖:

然後建立Scroll View,下面的元件Content,設定如下(別忘記新增Grid layput Group):

Prefabwuqi 是用Button做的預製體:

接下倆就是程式碼(這段程式碼新增到人物物品欄,也就是上面的rw下面的圖片都要新增這個指令碼):

public class Beibaofangzi : MonoBehaviour, IDropHandler
{
    public Transform myTR;
    public Sprite myImage;

    // Use this for initialization
    void Start()
    {
      //  MyBag();

    }
    // Update is called once per frame
    void Update()
    {

    }
    //void MyBag()
    //{
    //    foreach (Transform child in myTR.GetComponentsInChildren<Transform>(true))
    //    {

    //    }
    //}
    public void OnDrop(PointerEventData data)
    {
        var originalObj = data.pointerDrag;
        if (originalObj == null)
        {
            return;
        }
        var srcImage = originalObj.GetComponent<Image>();
        if (srcImage == null)
        {
            return;
        }
       // Debug.LogError(data.pointerDrag.GetComponent<Image>().sprite.name);
       // Debug.Log(myImage.name);
        if (data.pointerDrag.GetComponent<Image>().sprite.name == myImage.name)
        {
            GetComponent<Image>().sprite = data.pointerDrag.GetComponent<Image>().sprite;
        }
        else
        {
            return;
        }
       // GetComponent<Image>().sprite = data.pointerDrag.GetComponent<Image>().sprite;
    }
   
}

例項化程式碼(新增到Content上):

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

public class Instan : MonoBehaviour {
    public GameObject Prefab;
    public Transform ContTran;
    public Dictionary<int, GameObject> dic = new Dictionary<int, GameObject>();
    public Text MyText;
    public static Instan instance;
    public  GameObject instate;
    // Use this for initialization
     void Awake()
    {
        instance = this;
    }
    void Start () {
        Int();
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    void Int()
    {
        for (int i =0 ; i < 12; i++)
        {
            Sprite sp = Resources.Load<Sprite>(i.ToString());
            instate = Instantiate(Prefab, ContTran) as GameObject;
            instate.GetComponent<Image>().sprite = sp;
            instate.gameObject.SetActive(true);
            instate.name=sp.name;
           // Debug.LogError(instate.name);
            StoreKey(i,instate);
            MyText.text = (i+1).ToString();
        }
    }
  public  void StoreKey(int a,GameObject value)
    {
        dic.Add(a, value);
        foreach (KeyValuePair<int, GameObject> pair in dic)
        {
           Debug.LogError(pair.Key + " " + pair.Value);
        }
    }
}
物品程式碼(放到預製體上)別忘記在MonoBehaviour後新增引用 IBeginDragHandler, IDragHandler, IEndDragHandler:

public class wuPinBeiBao : MonoBehaviour
{ // 引用canvas 一會座標要用
    public Canvas cavs;
    // 拖動物品時,在滑鼠下面會有一個物品跟隨
    private GameObject m_DraggingIcon;
    // 放下物品的位置
    private RectTransform m_DraggingPlane;
    // Use this for initialization
    void Start()
    {
        // 重新生成物品角度方向和原來方向角度相同
        m_DraggingPlane = cavs.transform as RectTransform;
    }
    // Update is called once per frame
    void Update()
    {
    }
    public void OnClick(GameObject name)
    {

        name.name=name.GetComponent<Text>().text;
       // Debug.LogError(name.name);
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
      //  Debug.Log("OnBeginDrag: 拖動事件:" + eventData.position);
        m_DraggingIcon = new GameObject("icon");
      
        //public void SetParent(Transform parent, bool worldPositionStays);設定變換的父。設定生成的這個物體它的父親是cavs.transform false座標和父物體座標一置
        m_DraggingIcon.transform.SetParent(cavs.transform, false);
        //將變換移到區域性變換列表的結尾。生成的物體做為Cavas的最後一個子物體。
        m_DraggingIcon.transform.SetAsLastSibling();
        var image = m_DraggingIcon.AddComponent<Image>();

       // Debug.LogError(image);
        CanvasGroup group = m_DraggingIcon.AddComponent<CanvasGroup>();
        group.blocksRaycasts = false;
        image.sprite = eventData.pointerDrag.GetComponent<Image>().sprite;
       
        /// 生成的物品不影響射線檢測
       // image.SetNativeSize();
        if (m_DraggingIcon != null)
        {
            var rt = m_DraggingIcon.GetComponent<RectTransform>();
            Vector3 globalMousePos;
            if(RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane,eventData.position, eventData.pressEventCamera, out globalMousePos))
            {
                rt.position = globalMousePos;
                rt.rotation = m_DraggingPlane.rotation;
            }
        }
    }

    internal string OnClick(string name)
    {
        throw new NotImplementedException();
    }

    public void OnDrag(PointerEventData eventDate)
    {
      //  Debug.Log("OnDrag: 拖動中事件:" + eventDate.position);
        if (m_DraggingIcon != null)
        {
            var rt = m_DraggingIcon.GetComponent<RectTransform>();
            Vector3 globalMousePos;
            if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, eventDate.position, eventDate.pressEventCamera,out globalMousePos))
            {
                rt.position = globalMousePos;
                rt.rotation = m_DraggingPlane.rotation;
            }
        }
    }
   
}
好了 基本沒有什麼大問題,就是需要自己新增一個方法去Destroy掉物品就可以了。

效果圖(裝備欄和物品欄是對應的,比如武器只能放到武器欄,其他欄是放不進去的):

至於裝備顯示的文字資訊大家可以根據自己需要填寫