美文网首页
二、摇杆控制:自制摇杆

二、摇杆控制:自制摇杆

作者: GameObjectLgy | 来源:发表于2021-01-16 10:32 被阅读0次

集成目前市面游戏三种主流摇杆方式:固定底盘的、底盘在检测可变化位置的、摇杆可跟随移动的。

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public enum E_JoytickType
{
    Normal,//固定摇滚
    CanChangePos,//可改变位置摇杆
    CanMove,//可移动遥感
}

public class TouchCtl2D : MonoBehaviour
{
    public E_JoytickType e_JoytickType = E_JoytickType.Normal;
    public GameObject TouchRect;
    public GameObject ImgBg;
    public GameObject ImgDir;

    PEListener listener;
    private float maxPointDis = 180;
    private Vector2 startPos = Vector2.zero;
    private Vector2 defaultPos = Vector2.zero;
    private Vector2 currentDir;
    private Vector2 localPos;

    private void Start()
    {
        listener = TouchRect.GetComponent<PEListener>();
        if (listener == null)
        {
            TouchRect.AddComponent<PEListener>();
        }
        defaultPos = ImgBg.transform.position;

        RegisterTouchEvts();

        if (e_JoytickType != E_JoytickType.Normal)
        {
            //方式二,第二种遥感形式
            ImgBg.gameObject.SetActive(false);
        }
    }

    //调用者定义cb执行的内容,PEListener里的事件触发后会进行调用
    private void OnClickDown(GameObject go, Action<PointerEventData> cb)
    {
        listener.onClickDown = cb;
    }

    private void OnClickUp(GameObject go, Action<PointerEventData> cb)
    {
        listener.onClickUp = cb;
    }

    private void OnDrag(GameObject go, Action<PointerEventData> cb)
    {
        listener.onDrag = cb;
    }

    public void RegisterTouchEvts()
    {
        OnClickDown(TouchRect, (PointerEventData evt) => 
        {
            //方法1
            //startPos = evt.position;
            //ImgBg.transform.position = evt.position;

            if (e_JoytickType != E_JoytickType.Normal)
            {
                //方式二,第二种遥感形式
                ImgBg.gameObject.SetActive(true);
                RectTransformUtility.ScreenPointToLocalPointInRectangle(
                        TouchRect.GetComponent<Image>().rectTransform,
                        (evt as PointerEventData).position,
                        (evt as PointerEventData).pressEventCamera,
                        out localPos);
                ImgBg.transform.localPosition = localPos;
            }
            
        });
        OnClickUp(TouchRect, (PointerEventData evt) => 
        {
            //方法1
            //ImgBg.transform.position = defaultPos;
            //ImgDir.transform.localPosition = Vector2.zero;
            //currentDir = Vector2.zero;
            //Debug.Log(currentDir);


            ImgDir.transform.localPosition = Vector3.zero;

            if (e_JoytickType != E_JoytickType.Normal)
            {
                //方式二,第二种遥感形式
                ImgBg.gameObject.SetActive(false);
            }
            EventManager.DispatchEvent<Vector2>(EventTypeList.EventTouchDir, Vector2.zero);
        });
        OnDrag(TouchRect, (PointerEventData evt) => 
        {
            //方法1
            //Vector2 dir = evt.position - startPos;
            //float len = dir.magnitude;
            //if (len > pointDis)
            //{
            //    Vector2 clampDir = Vector2.ClampMagnitude(dir, pointDis);
            //    ImgDir.transform.position = startPos + clampDir;
            //}
            //else
            //{
            //    ImgDir.transform.position = evt.position;
            //}
            //currentDir = dir.normalized;
            //Debug.Log(currentDir);

            //得到所点击的屏幕位置,相对于父物体的UI本地坐标
            RectTransformUtility.ScreenPointToLocalPointInRectangle(
                ImgBg.GetComponent<Image>().rectTransform,
                (evt as PointerEventData).position,
                (evt as PointerEventData).pressEventCamera,
                out localPos);
            ImgDir.transform.localPosition = localPos;
            if (localPos.magnitude > maxPointDis)
            {
                if (e_JoytickType == E_JoytickType.CanMove)
                {
                    ImgBg.transform.localPosition += (Vector3)(localPos.normalized * (localPos.magnitude - maxPointDis));
                }
                ImgDir.transform.localPosition = localPos.normalized * 180;
            }
            //Debug.Log(localPos.normalized);
            EventManager.DispatchEvent<Vector2>(EventTypeList.EventTouchDir, localPos.normalized);
        });
    }
}

1.png 2.png

相关文章

网友评论

      本文标题:二、摇杆控制:自制摇杆

      本文链接:https://www.haomeiwen.com/subject/vdleaktx.html