Unity用UGUI来实现一个摇杆操作

下面附上所需要的代码模块

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


public enum E_JoystickType
{
    
    
    Normal,
    CanChangePos,
    CanMove
}


public class JoystickPanel : BasePanel
{
    
    
    private Image imgTouchRect;

    private Image imgBk;

    private Image imgControl;

    public E_JoystickType type = E_JoystickType.Normal;
    public float maxL = 180;
    // Start is called before the first frame update
    void Start()
    {
    
    
        imgBk = GetControl<Image>("imgBK");
        imgControl = GetControl<Image>("imgControl");
        imgTouchRect = GetControl<Image>("ImgTouchRect");

        UIManager.AddCustomEventListener(imgTouchRect, EventTriggerType.PointerDown, PointerDown);
        UIManager.AddCustomEventListener(imgTouchRect, EventTriggerType.PointerUp, PointerUp);
        UIManager.AddCustomEventListener(imgTouchRect, EventTriggerType.Drag, Drag);
        Debug.Log("Ojbk");

        if(type!=E_JoystickType.Normal)
        imgBk.gameObject.SetActive(false);
    }

    private void Drag(BaseEventData data)
    {
    
    
        Debug.Log("tttt");
        Vector2 localPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(imgBk.rectTransform, (data as PointerEventData).position,
         (data as PointerEventData).pressEventCamera, out localPos
            );
        imgControl.transform.localPosition = localPos;

        if (localPos.magnitude > maxL)
        {
    
    
            if (type == E_JoystickType.CanMove)
            {
    
    
                imgBk.transform.localPosition += (Vector3)(localPos.normalized * (localPos.magnitude - maxL));
            }
            imgControl.transform.localPosition = localPos.normalized * maxL;
        }
        EventCenter.GetInstance().EventTrigger<Vector2>("JoyStick", localPos.normalized);
    }

    private void PointerUp(BaseEventData data)
    {
    
    
        Debug.Log("222");
        imgControl.transform.localPosition = Vector3.zero;

        EventCenter.GetInstance().EventTrigger<Vector2>("JoyStick", Vector2.zero);

        if (type != E_JoystickType.Normal)
        {
    
    
            imgBk.gameObject.SetActive(false);
        }
    }

    private void PointerDown(BaseEventData data)
    {
    
    
        Debug.Log("111");
        imgBk.gameObject.SetActive(true);

        if (type != E_JoystickType.Normal)
        {
    
    
            Vector2 localPos;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(imgTouchRect.rectTransform, (data as PointerEventData).position,
             (data as PointerEventData).pressEventCamera, out localPos
                );
            imgBk.transform.localPosition = localPos;

        }
    }

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

然后具体的代码模块具体操作可以更改
最主要的核心代码在于

  private void Drag(BaseEventData data)
    {
    
    
        Debug.Log("tttt");
        Vector2 localPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(imgBk.rectTransform, (data as PointerEventData).position,
         (data as PointerEventData).pressEventCamera, out localPos
            );
        imgControl.transform.localPosition = localPos;

        if (localPos.magnitude > maxL)
        {
    
    
            if (type == E_JoystickType.CanMove)
            {
    
    
                imgBk.transform.localPosition += (Vector3)(localPos.normalized * (localPos.magnitude - maxL));
            }
            imgControl.transform.localPosition = localPos.normalized * maxL;
        }
        EventCenter.GetInstance().EventTrigger<Vector2>("JoyStick", localPos.normalized);
    }
```csharp


猜你喜欢

转载自blog.csdn.net/charlsdm/article/details/123845161
今日推荐