• unity 之 背包系统


    此方法只是用于学习和实验所以细节不必要求

    一、Ui设置。

    画布配置如下:

    布局:

     说明:

    画布里面首先建立一个panel命名为weapon1,在其内部再建立4个panel用于装备的卡槽,装备以image来显示,并且添加自定义hp脚本(hp脚本主要配置物品的名称、稀有度、说明)。message主要用来显示武器信息。image配置如下:

     weapon1配置此处使用两种方法来检测鼠标事件(1.直接添加event trigger组建绑定函数  2.在dragManager代码中监听):

    二、代码。

    dragManager

    //继承接口实现鼠标进入和移除事件
    public class dragManager : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler
    {
    //定义偏移量,用来记录鼠标和装备图片的偏移
    public Vector3 offet;
    //提示武器信息即message
    public GameObject g;
    //是否拖放成功
    private bool massger;
    
    public Action a;
    public Action b;
    //拖拽武器
    public GameObject game;
    // Start is called before the first frame update
    void Start()
    {
    
    }
    
    // Update is called once per frame
    void Update()
    {
    if (a != null)
    a();
    // print(game.activeSelf);
    }
    //开始拖拽确定物体
    public void beginDrag()
    {
    for (int i = 0; i < transform.childCount; i++)
    {
    RectTransform rect =(RectTransform) transform.GetChild(i);
    if (rect.rect.Contains(Input.mousePosition - rect.position) && rect.GetChild(0).gameObject.activeSelf)
    {
    offet = Input.mousePosition - rect.position;
    g = rect.GetChild(0).gameObject;
    return;
    }
    }
    }
    //释放
    public void endDrag()
    {
    massger = false;
    for (int i = 0; i < transform.childCount; i++)
    {
    RectTransform rect = (RectTransform)transform.GetChild(i);
    if (rect.rect.Contains(Input.mousePosition - rect.position) && !rect.GetChild(0).gameObject.activeSelf && g!=null)
    {
    rect.GetChild(0).GetComponent<Image>().sprite=g.GetComponent<Image>().sprite;
    g.GetComponent<UnityEngine.UI.Image>().sprite = null;
    g.gameObject.SetActive(false);
    rect.GetChild(0).gameObject.SetActive(true);
    massger = true;
    g.GetComponent<RectTransform>().localPosition = Vector3.zero;
    g = null;offet = Vector3.zero;
    return;
    }
    }
    if (!massger && g!=null)
    {
    g.GetComponent<RectTransform>().localPosition = Vector3.zero;
    g = null; offet = Vector3.zero;
    }
    }
    //拖拽中
    public void draging()
    {
    if (g != null)
    {
    g.GetComponent<RectTransform>().position = Input.mousePosition - offet;
    }
    }
    //鼠标进入
    public void OnPointerEnter(PointerEventData eventData)
    {
    a += NewMethod;
    }
    
    private void NewMethod()
    {
    for (int i = 0; i < transform.childCount; i++)
    {
    RectTransform rect = (RectTransform)transform.GetChild(i);
    if (rect.rect.Contains(Input.mousePosition - rect.position) && rect.GetChild(0).gameObject.activeSelf)
    {
    Text t = game.transform.GetChild(0).GetComponent<Text>();
    hp h= rect.GetChild(0).GetComponent<hp>();
    string s= "<color=" + h.color + ">"+ h.name +"</color>
    "+h.description;
    t.text =s.ToString();
    
    break;
    }
    }
    game.transform.position = new Vector3(Input.mousePosition.x + game.GetComponent<RectTransform>().rect.width / 2,
    Input.mousePosition.y - game.GetComponent<RectTransform>().rect.height / 2, 0);
    if(!game.activeSelf)game.SetActive(true);
    }
    
    //鼠标移出
    public void OnPointerExit(PointerEventData eventData)
    {
    b += NewMethod2;
    b();
    }
    private void NewMethod2()
    {
    game.SetActive(false);
    b = null;
    a = null;
    }
    }

    三、运行结果有点丑,但只是实验用。

     

    四、解决个别问题。屏幕放大图片信息一直闪烁。

     将text的Raycast Target取消勾选。

     

  • 相关阅读:
    软件工程第四次作业
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    软件工程最后一次作业
    软件工程第四次作业
    软件工程第二次作业
    软件工程最后一次作业
    软件工程第二次结对作业
    软件工程第三次作业
  • 原文地址:https://www.cnblogs.com/unknown6248/p/11700080.html
Copyright © 2020-2023  润新知