unity虚拟摇杆 + 多点触控方案

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

using UnityEngine.EventSystems;
using UnityEngine.UI;

public class Rocker : MonoBehaviour {

    public RectTransform RockerPoint;       //触控点
    public RectTransform BottomPlane;       //触控盘父物体
    public RectTransform BottomImage;       //边框
    public RectTransform RockerEffect;      //特效显示(头朝上)
    private float DragRadius; //拖动半径
    private Vector2 RockerPointPosition; //触点坐标
    private Vector2 initPosition; //按下时坐标
    public static Vector2 RockerXY =new Vector2(0,0);       //摇杆坐标数据等同于Input.GetAxis (0~1) 
    private float DragMin = 0.015f;         //最小移动距离

    private void Start()
    {  //根据分辨率自适应摇杆区域大小
        RockerPointPosition = new Vector2(Screen.height * 0.2f, Screen.height * 0.2f);
        BottomPlane.position = RockerPointPosition;
        BottomPlane.sizeDelta = new Vector2(Screen.width * 0.3f, Screen.width * 0.3f);
        BottomImage.sizeDelta = new Vector2(Screen.width * 0.16f, Screen.width * 0.16f);
        RockerEffect.sizeDelta = BottomImage.sizeDelta * 0.9f;
        RockerPoint.sizeDelta = new Vector2(Screen.width * 0.03f, Screen.width * 0.03f);
        RockerPoint.localPosition = new Vector2(0,0);
        DragRadius = Screen.width * 0.05f; //拖动距离
        DragMin = Screen.width * DragMin;  //最小拖动半径
        BottomImage.GetComponent<Image>().color = new Color(1, 1, 1, 0);
        RockerEffect.GetComponent<Image>().color = new Color(1, 1, 1, 0);
    }
    
    /// <summary>
    /// 淡出淡如特效
    /// </summary>
    /// <param name="start"></param>
    /// <returns></returns>
    IEnumerator EffectAnime(bool start)
    { //我这里设置10帧
        int x1 = start ? 0 : 10;
        int x2 = start ? 10 : 0;
        do
        {
            if (x1 >= 6)
            { RockerPoint.GetComponent<Image>().color = new Color(1, 1, 1, x1 * 0.1f); }
            RockerEffect.GetComponent<Image>().color = new Color(1, 1, 1, x1 * 0.1f);
            BottomImage.GetComponent<Image>().color = new Color(1, 1, 1, x1 * 0.1f);
            x1 += start ? 1 : -1;
            yield return new WaitForSeconds(0.03f);
        }
        while (start ? x1 <= x2 : x1 >= x2);
    }

    /// <summary>
    /// 按下摇杆
    /// </summary>
    /// <param name="eventData">触发物体</param>
    public void ClickDown()
    {
        StartCoroutine("EffectAnime",true);
        initPosition = GetTouches(1);
        if (initPosition.x > RockerPointPosition.x & initPosition.y > RockerPointPosition.y)
        { BottomPlane.position = initPosition; }
    }

    /// <summary>
    /// 抬起摇杆
    /// </summary>
    public void ClickUP()
    {
        StartCoroutine("EffectAnime", false);
        RockerXY = new Vector2(0, 0);
        RockerPoint.localPosition = new Vector2(0, 0);
        BottomPlane.position = RockerPointPosition;
        TouchUp(1);
    }
    //拖动摇杆
    public void RockerDrag()
    {
        Vector2 dir = GameToolScript.tool.GetTouches(1) - initPosition;
        if (Mathf.Abs(dir.x) > DragMin | Mathf.Abs(dir.y) > DragMin)
        {
            RockerEffect.gameObject.SetActive(true);
            RockerPoint.localPosition = dir.normalized * DragRadius;
            RockerXY = RockerPoint.localPosition.normalized;
            Vector3 v = (RockerPoint.transform.position - BottomPlane.transform.position).normalized;
            RockerEffect.up= v;
        }
        else
        {
            RockerEffect.gameObject.SetActive(false);
            RockerPoint.localPosition = dir;
            RockerXY = new Vector2(0, 0);
        }
    }

    private List<int> touchesList = new List<int> { };
    /// <summary>
    /// 查找是哪个触点
    /// <para>适用于多点触控自动判定触点, 推荐放置于静态变量中</para>
    /// </summary>
    /// <param name="touch">触摸的物体</param>
    /// <returns></returns>
    public Vector2 GetTouches(int touch)
    {
        int index = touchesList.IndexOf(touch);
        if (index > -1)
        {
            return Input.touches[index].position;
        }
        touchesList.Add(touch);
        return Input.touches[Input.touchCount - 1].position;
    }
    public void TouchUp(int touch)
    {
        touchesList.Remove(touch);
    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值