unity中摇杆的使用
创始人
2024-11-16 03:35:31
0

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

namespace JoystickPlugin
{
    public class Joystick : MonoBehaviour, IPointerUpHandler, IDragHandler, IPointerDownHandler
    {
        public float Horizontal { get { return (snapX) ? SnapFloat(input.x, AxisOptions.Horizontal) : input.x; } }
        public float Vertical { get { return (snapY) ? SnapFloat(input.y, AxisOptions.Vertical) : input.y; } }
        public Vector2 Direction { get { return new Vector2(Horizontal, Vertical); } }

        public float HandleRange
        {
            get { return handleRange; }
            set { handleRange = Mathf.Abs(value); }
        }

        public float DeadZone
        {
            get { return deadZone; }
            set { deadZone = Mathf.Abs(value); }
        }

        public AxisOptions AxisOptions { get { return AxisOptions; } set { axisOptions = value; } }
        public bool SnapX { get { return snapX; } set { snapX = value; } }
        public bool SnapY { get { return snapY; } set { snapY = value; } }

        [SerializeField] private float handleRange = 1;
        [SerializeField] private float deadZone = 0;
        [SerializeField] private AxisOptions axisOptions = AxisOptions.Both;
        [SerializeField] private bool snapX = false;
        [SerializeField] private bool snapY = false;

        [SerializeField] public RectTransform background = null;
        [SerializeField] public RectTransform handle = null;
        private RectTransform baseRect = null;

        private Canvas canvas;
        private Camera cam;

        private Vector2 input = Vector2.zero;

        private void Awake()
        {

            HandleRange = 0.5f;
            DeadZone = 0;
            AxisOptions = AxisOptions.Both;
            SnapX = false;
            SnapY = false;
            background = transform.Find("Background").GetComponent();
            handle = transform.Find("Background/Handle").GetComponent();
            baseRect = GetComponent();
            canvas = transform.parent.GetComponent();
            //            background = transform.GetComponent();
            Debug.Log("canvas:::" + canvas);
            Debug.Log("transform:::" + transform);
//            handle = transform.Find("Handle").GetComponent();
        }

        protected virtual void Start()
        {
            HandleRange = handleRange;
            DeadZone = deadZone;
            baseRect = GetComponent();
            canvas = GetComponentInParent();
            if (canvas == null)
                Debug.LogError("The Joystick is not placed inside a canvas");
            Vector2 center = new Vector2(0.5f, 0.5f);
            background.pivot = center;
            handle.anchorMin = center;
            handle.anchorMax = center;
            handle.pivot = center;
            handle.anchoredPosition = Vector2.zero;
            input = Vector2.zero;
        }

        public virtual void OnPointerDown(PointerEventData eventData)
        {
            OnDrag(eventData);
        }

        public void OnDrag(PointerEventData eventData)
        {
            cam = null;
            if (canvas.renderMode == RenderMode.ScreenSpaceCamera)
                cam = canvas.worldCamera;

            Vector2 position = RectTransformUtility.WorldToScreenPoint(cam, background.position);
            Vector2 radius = background.sizeDelta / 2;
            input = (eventData.position - position) / (radius * canvas.scaleFactor);
            FormatInput();
            HandleInput(input.magnitude, input.normalized, radius, cam);
            handle.anchoredPosition = input * radius * handleRange;
        }

        protected virtual void HandleInput(float magnitude, Vector2 normalised, Vector2 radius, Camera cam)
        {
            if (magnitude > deadZone)
            {
                if (magnitude > 1)
                    input = normalised;
            }
            else
                input = Vector2.zero;
        }

        private void FormatInput()
        {
            if (axisOptions == AxisOptions.Horizontal)
                input = new Vector2(input.x, 0f);
            else if (axisOptions == AxisOptions.Vertical)
                input = new Vector2(0f, input.y);
        }

        private float SnapFloat(float value, AxisOptions snapAxis)
        {
            if (value == 0)
                return value;

            if (axisOptions == AxisOptions.Both)
            {
                float angle = Vector2.Angle(input, Vector2.up);
                if (snapAxis == AxisOptions.Horizontal)
                {
                    if (angle < 22.5f || angle > 157.5f)
                        return 0;
                    else
                        return (value > 0) ? 1 : -1;
                }
                else if (snapAxis == AxisOptions.Vertical)
                {
                    if (angle > 67.5f && angle < 112.5f)
                        return 0;
                    else
                        return (value > 0) ? 1 : -1;
                }
                return value;
            }
            else
            {
                if (value > 0)
                    return 1;
                if (value < 0)
                    return -1;
            }
            return 0;
        }

        public virtual void OnPointerUp(PointerEventData eventData)
        {
            input = Vector2.zero;
            handle.anchoredPosition = Vector2.zero;
        }

        protected Vector2 ScreenPointToAnchoredPosition(Vector2 screenPosition)
        {
            Vector2 localPoint = Vector2.zero;
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(baseRect, screenPosition, cam, out localPoint))
            {
                Vector2 pivotOffset = baseRect.pivot * baseRect.sizeDelta;
                return localPoint - (background.anchorMax * baseRect.sizeDelta) + pivotOffset;
            }
            return Vector2.zero;
        }
    }

    public enum AxisOptions { Both, Horizontal, Vertical }

}
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using JoystickPlugin;
public class testTANK : MonoBehaviour
{
    FixedJoystick fx;
    public Transform t;
    // Start is called before the first frame update
    void Start()
    {
        //滑轮
       fx = transform.Find("touch_bg").gameObject.AddComponent();

        fx.HandleRange = 0.5f;
        fx.enabled = true;
    }

    // Update is called once per frame
    void Update()
    {
        //Quaternion rotation = Quaternion.LookRotation(fx.Direction);

        // 应用旋转到GameObject  
        //t.localEulerAngles =new Vector3(0,0, Vector2ToAngle(fx.Direction));

        t.rotation = Quaternion.Euler(new Vector3(0, 0, Vector2ToAngle(fx.Direction)));
        //t.rotation = Quaternion.Euler();
        t.transform.Translate(fx.Direction * Time.fixedDeltaTime*50 , Space.World);
    }
    float Vector2ToAngle(Vector2 vector)
    {
        // 使用Atan2计算角度,并将弧度转换为度数
        float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg-90f;

        // 保证角度为正数
        if (angle < 0) angle += 360;

        return angle;
    }
}
 

相关内容

热门资讯

来临!德州圈脚本,佛手在线大菠... 您好:这款佛手在线大菠萝技巧游戏是可以开挂的,确实是有挂的,很多玩家在这款佛手在线大菠萝技巧游戏中打...
来临!遇悦游戏辅助,九九山城插... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
此事备受玩家关注!hhpoke... 此事备受玩家关注!hhpoker辅助器视频,hhpoker是正规的吗,烘培教程(其实透视真的是有挂)...
无独有偶!欢乐达人暗宝辅助,科... 无独有偶!欢乐达人暗宝辅助,科乐填大坑辅助码,办法教程(其实是有挂)-哔哩哔哩;无需打开直接搜索加(...
记者获悉!欢乐茶馆修改器,边锋... 边锋老友填大坑辅助 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: ...
昨日!hhpoker买挂,we... wepokerplus辅助开挂教程视频分享装挂详细步骤在当今的网络游戏中,wepokerplus辅助...
现场直击!途游四川麻将辅助器软... 现场直击!途游四川麻将辅助器软件,四川途游辅助软件,要领教程(一直确实是有挂的)-哔哩哔哩;无需打开...
记者获悉!hhpoker辅助软... 记者获悉!hhpoker辅助软件是真的么,wepoker黑侠破解,指南书教程(一直透视确实有挂)-哔...
出现新变化!新518互娱脚本下... 出现新变化!新518互娱脚本下载,阿拉斗牌辅助视频,方式教程(果然是有挂)-哔哩哔哩您好:阿拉斗牌辅...
据悉!wepoker黑侠辅助器... 据悉!wepoker黑侠辅助器,werplan外卦神器,讲义教程(果然透视是有挂的)-哔哩哔哩;无需...