unity經過手指觸摸或者鼠標位置,控制ScrollerView移動

功能有待完善佈局

UI佈局以下:this

總體上下滑動,右側左右滑動。(仿股票)orm

 

代碼以下:blog

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FigureInputController : MonoBehaviour
{

    private float fingerActionSensitivity = Screen.width * 0.05f; //手指動做的敏感度,這裏設定爲二十分之一的屏幕寬度。
    private float fingerBeginX;
    private float fingerBeginY;
    private float fingerCurrentX;
    private float fingerCurrentY;
    private float fingerSegmentX;
    private float fingerSegmentY;
    private int fingerTouchState;
    private int FINGER_STATE_NULL = 0;
    private int FINGER_STATE_TOUCH = 1;
    private int FINGER_STATE_ADD = 2;
    public Scrollbar scrollbarV;
    public Scrollbar scrollbarH;
    public Camera cameraUI;
    public bool isTest;
    // Use this for initialization
    void Start()
    {
        fingerActionSensitivity = Screen.width * 0.05f;
        fingerBeginX = 0;
        fingerBeginY = 0;
        fingerCurrentX = 0;
        fingerCurrentY = 0;
        fingerSegmentX = 0;
        fingerSegmentY = 0;
        fingerTouchState = FINGER_STATE_NULL;
    }
    RaycastHit2D hit;
    Ray ray;
    // Update is called once per frame
    void Update()
    {
        //if (Input.GetMouseButton(0))//|| Input.touches[0].phase == TouchPhase.Moved)
        //{
        //    //Debug.LogError(Input.mousePosition);
        //}

#if UNITY_EDITOR
        if (Input.GetMouseButton(0))
#else
        if(Input.touches[0].phase == TouchPhase.Moved)
#endif
        {
            isTest = true;
                    //if (Input.GetMouseButtonDown(0))// || Input.touches[0].phase == TouchPhase.Began)
                    {
                    //Vector3 mousePos = cameraUI.ScreenToWorldPoint(Input.mousePosition);
                    //Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
                    //hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
                    //if (hit.transform != null)
                    //{
                    //    Debug.LogError(hit.transform.gameObject.name);
                    //}                     
                    //}					
                    if (fingerTouchState == FINGER_STATE_NULL)
                    {
                        fingerTouchState = FINGER_STATE_TOUCH;
                        fingerBeginX = Input.mousePosition.x;
                        fingerBeginY = Input.mousePosition.y;
                    }

            }
		
            //if (fingerTouchState == FINGER_STATE_TOUCH)
            {
                //Debug.LogError("2");
                fingerCurrentX = Input.mousePosition.x;
                fingerCurrentY = Input.mousePosition.y;
                fingerSegmentX = fingerCurrentX - fingerBeginX;
                fingerSegmentY = fingerCurrentY - fingerBeginY;

            }
            //if (fingerTouchState == FINGER_STATE_TOUCH)
            {
                float fingerDistance = fingerSegmentX * fingerSegmentX + fingerSegmentY * fingerSegmentY;
                if (fingerDistance > (fingerActionSensitivity * fingerActionSensitivity))
                {
                    if (Input.GetMouseButton(0))
                    {
                        Debug.LogError("3");
                        toAddFingerAction();
                    }
                }
            }
			
            if (Input.GetKeyUp(KeyCode.Mouse0))
            {
                fingerTouchState = FINGER_STATE_NULL;
                isTest = false;
            }
        }
        }
        private void toAddFingerAction()
        {
            fingerTouchState = FINGER_STATE_ADD;
            if (Mathf.Abs(fingerSegmentX) > Mathf.Abs(fingerSegmentY))
            {
                fingerSegmentY = 0;
            }
            else
            {
                fingerSegmentX = 0;
            }
            if (fingerSegmentX == 0)
            {
                if (fingerSegmentY > 0)
                {
                    Debug.Log("up" + fingerSegmentY);
                    float temp = fingerSegmentY * 0.00005f;
                    if (scrollbarV.value < 1)
                    {
                        if (temp > 0 && temp < 1)
                        {
                            scrollbarV.value += temp;
                        if (scrollbarV.value >= 1)
                        { scrollbarV.value = 1; }
                        }
                    }
                }
                else
                {
                    Debug.Log("down" + fingerSegmentY);
                    float temp = Mathf.Abs(fingerSegmentY) * 0.00005f;
                    if (scrollbarV.value > 0)
                    {
                        if (temp > 0 && temp < 1)
                        {
                            scrollbarV.value -= temp;

                        if (scrollbarV.value < 0)
                        {
                            scrollbarV.value = 0;
                        }
                        }
                    }
                }
            }
            else if (fingerSegmentY == 0)
            {
                if (fingerSegmentX > 0)
                {
                    //Debug.Log("右" + fingerSegmentX);
                    //float temp = Mathf.Abs(fingerSegmentY) * 0.00005f;
                    //if (scrollbarH.value >= 0)
                    //{
                    //    if (temp > 0 && temp < 1)
                    //    {
                    //        scrollbarH.value -= temp;
                    //    }
                    //}
                }
                else
                {
                    //Debug.Log("左" + fingerSegmentX);
                    //float temp = fingerSegmentY * 0.00005f;
                    //if (scrollbarH.value < 1)
                    //{
                    //    if (temp > 0 && temp < 1)
                    //    {
                    //        scrollbarH.value += temp;
                    //    }
                    //}
                }
            }
        }
   
}
//    private float fingerActionSensitivity = Screen.width * 0.05f; //手指動做的敏感度,這裏設定爲二十分之一的屏幕寬度。
//    private float fingerBeginX;
//    private float fingerBeginY;
//    private float fingerCurrentX;
//    private float fingerCurrentY;
//    private float fingerSegmentX;
//    private float fingerSegmentY;
//    private int fingerTouchState;
//    private int FINGER_STATE_NULL = 0;
//    private int FINGER_STATE_TOUCH = 1;
//    private int FINGER_STATE_ADD = 2;
//    public Scrollbar scrollbar1;
//    public Scrollbar scrollbar2;
//    // Use this for initialization
//    void Start()
//    {
//        fingerActionSensitivity = Screen.width * 0.05f;
//        fingerBeginX = 0;
//        fingerBeginY = 0;
//        fingerCurrentX = 0;
//        fingerCurrentY = 0;
//        fingerSegmentX = 0;
//        fingerSegmentY = 0;
//        fingerTouchState = FINGER_STATE_NULL;
//    }
//    // Update is called once per frame
//    void Update()
//    {
//        if (Input.GetMouseButton(0))//|| Input.touches[0].phase == TouchPhase.Moved)
//        {
//            //Debug.LogError(Input.mousePosition);
//        }
//        if (Input.GetKeyDown(KeyCode.Mouse0))
//        {
//            if (fingerTouchState == FINGER_STATE_NULL)
//            {
//                fingerTouchState = FINGER_STATE_TOUCH;
//                fingerBeginX = Input.mousePosition.x;
//                fingerBeginY = Input.mousePosition.y;
//            }

//        }
//        if (fingerTouchState == FINGER_STATE_TOUCH)
//        {
//            Debug.LogError("2");
//            fingerCurrentX = Input.mousePosition.x;
//            fingerCurrentY = Input.mousePosition.y;
//            fingerSegmentX = fingerCurrentX - fingerBeginX;
//            fingerSegmentY = fingerCurrentY - fingerBeginY;
//            //toAddFingerAction();
//        }
//        if (fingerTouchState == FINGER_STATE_TOUCH)
//        {
//            float fingerDistance = fingerSegmentX * fingerSegmentX + fingerSegmentY * fingerSegmentY;
//            if (fingerDistance>(fingerActionSensitivity * fingerActionSensitivity))
//            {
//                Debug.LogError("3");
//                toAddFingerAction();
//            }
//        }
//        if (Input.GetKeyUp(KeyCode.Mouse0))
//        {
//          fingerTouchState = FINGER_STATE_NULL;
//        }
//    }
//    private void toAddFingerAction()
//    {
//        fingerTouchState = FINGER_STATE_ADD;
//        if(Mathf.Abs(fingerSegmentX)> Mathf.Abs(fingerSegmentY))
//        {
//            fingerSegmentY = 0;
//        }
//        else
//        {
//            fingerSegmentX = 0;
//        }
//        if(fingerSegmentX == 0) 
//        {
//            if(fingerSegmentY> 0)
//            {
//                Debug.Log("up"+fingerSegmentY);
//            } 
//            else
//            {
//                Debug.Log("down"+fingerSegmentY);
//            }
//        } 
//        else if(fingerSegmentY == 0) 
//        {
//            if(fingerSegmentX> 0)
//            {
//                Debug.Log("右");
//            }          
//            else
//            {
//                Debug.Log("左");
//            }
//        }
//    }
//}
相關文章
相關標籤/搜索