考虑到适配的动态摇杆(本人资料备份)

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

public class YaoGan : MonoBehaviour
{
    public static event Action<bool, Vector3> OnYaoGanEvent;
    Vector2 oldFingerPosition;
    Vector2 newFingerPosition;
    Vector2 FirstMovePosition;


    private Vector3 yaoGanDir = new Vector3();

    bool first = false;//第一次滑动帧记录

    int kkk = 0;

    private RectTransform img;//
    private RectTransform img2;//
                               //  Text text;
                               // Text text1;
                               //  int j = 0;
    int i = 0;
    //  int k = 0;
    // int l = 0;

    bool HaveOne = false;//表示已经有一个划圈了

    float panWidth;
    int touchCount;
    int lastFingerID = -111;//指头id 标记谁最后碰到了

    float pos;//屏幕摇杆限制位置
    private Image PanOld;

    void Start()
    {
        //允许多点触屏  
        Input.multiTouchEnabled = true;
        img = GameObject.Find("YaoGan/Pan").GetComponent<RectTransform>();
        panWidth = img.GetComponent<RectTransform>().sizeDelta.x / 2;
        //  Debug.Log("盘子的宽度:"+panWidth);

        img2 = GameObject.Find("YaoGan/Pan/Niu").GetComponent<RectTransform>();
        PanOld = GameObject.Find("YaoGan/PanOld").GetComponent<Image>();

        //获取屏幕的高度
        //   Debug.Log("屏幕高:"+Screen.height+"   宽: "+Screen.width);
        pos = Screen.width / 5 * 3;//屏幕的宽度

    }


    void Update()
    {
        // text.text = "按钮位置:" + img2.transform.position + "盘子位置:" + img.transform.position;//盘子位置
        //text1.text = "手指头最后滑动的id:" + lastFingerID + "   " + "当前触碰数量:" + Input.touchCount;
        if (Input.touchCount <= 0) { return; }

        else if (Input.touchCount > 0)//当有多个手指触屏   
        {
            touchCount = Input.touchCount;

            for (int i = 0; i < touchCount; i++)
            {
                Touch touch = Input.GetTouch(i);
                // Debug.Log("遍历的i:"+i+"                 id:"+touch.fingerId);
                if (touch.phase == TouchPhase.Began)
                {
                }
                else if (touch.phase == TouchPhase.Moved)
                {
                    //Debug.Log("Moved");
                    if (lastFingerID != -111)//说明已经有个滑动的了,那么就以他为标准
                    {

                        if (touch.fingerId != lastFingerID)//不是它就直接退出
                        {
                            continue;//这个地方以前用了return所以导致了严重的错误,就是两手滑动的时候不抬起来了
                        }
                    }

                    if (!first)
                    {
                        // Debug.Log("first");
                        if (touch.position.x > pos) continue;
                        lastFingerID = touch.fingerId;//只有第一次滑动的时候我们才记录id 
                                                      //  Debug.Log("赋值滑杆控制的id:" + lastFingerID +" i:"+i+"                          "+(kkk++));
                        newFingerPosition = FirstMovePosition = touch.position;//记录第一次滑动的位置
                        PanOld.enabled = false;
                        img.gameObject.SetActive(true);
                        img2.gameObject.SetActive(true);
                        //   Debug.Log("开始滑动,我们产生的位置盘在当前");//生成就在此
                        //    newFingerPosition = FirstMovePosition = touch.position;//刚要滑动的位置就赋值为旧位置并且生成一个大圆盘

                        ////log
                        //  Debug.Log(" touch.position:" + touch.position); // 400 100  ////800*500
                        float xxx = touch.position.x / Screen.width;///400/800  0.5
                        float yyy = touch.position.y / Screen.height; ///100 /500   0.2
                        float zzz = Screen.height * 1.0f / Screen.width;///500/800约等于0.6
                       ///// new Vector2(1334 * xxx, 1334 * yyy * zzz)///先求出全部的高度 然后乘上比例
                        img.localPosition = new Vector3(1334 * xxx, 1334 * zzz * yyy);//盘子位置
                        img2.localPosition = Vector3.zero;//

猜你喜欢

转载自blog.csdn.net/qq_22012149/article/details/78345019