中文字幕无码a片久久,亚洲日韩在线观看浪潮,人人超人人超碰超国产二区,国产人妻久久一区二区,国产人妻久久一区二区

福感科技有限公司 歡迎您!
聯(lián)系方式

    地址:北京市平谷區(qū)馬坊鎮(zhèn)金河北街17號(hào)院3號(hào)樓7層712

    電話:010-89968230

    網(wǎng)站:http://0743119.com

Uniry手機(jī)端手勢(shì)的基本操作介紹

2021-3-15 17:51:31??????點(diǎn)擊:

Uniry手機(jī)端手勢(shì)的基本操作主要有單指移動(dòng)3D物體、單指旋轉(zhuǎn)3D物體、雙指縮放3D物體。

基類

using UnityEngine;
using System.Collections;
/// <summary>
/// 手勢(shì)操作父類,并用于互斥三種手勢(shì)
/// </summary>
public class GestureControl : MonoBehaviour
{
    //記錄手勢(shì)狀態(tài):
    //-1——沒有任何手勢(shì)在操作
    //0——移動(dòng)手勢(shì)正在操作
    //1——旋轉(zhuǎn)手勢(shì)正在操作
    //2——縮放手勢(shì)正在操作
    public static int status = -1;
    //用于記錄觸碰物體的時(shí)間(區(qū)分同為單指時(shí)移動(dòng)與旋轉(zhuǎn),詳見相應(yīng)代碼)
    public static float TouchTime = 0;
 
    protected bool isSelected = false;
 
    //判斷是否事先選擇到了某物體
    protected void OnMouseDown() {
        isSelected = true;
    }
 
    //手指抬起,記錄歸零
    protected void OnMouseUp() {
        isSelected = false;
        status = -1;
        TouchTime = 0;
    }
    // Update is called once per frame
     protected void Update()
    {
        if (!isSelected)
        {
            return;
        }
        else if(status == -1)
        {
            InputCheck();
        }
    }
    /// <summary>
    /// 之類相應(yīng)操作
    /// </summary>
     protected virtual void InputCheck() { }
}

 單指移動(dòng)3D物體

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
 
public class MoveControl : GestureControl
{  
    protected override void InputCheck()
    {
        //單指移動(dòng)
        if (Input.touchCount == 1)
        {
            //觸碰按住3D物體不動(dòng)1秒后物體隨手指一起移動(dòng)
            if (Input.GetTouch(0).phase == TouchPhase.Stationary)
            {
                TouchTime += Time.deltaTime;
                if (TouchTime > 1)
                {
                    status = 0;
                }
            }
            if (status == 0)
            {
                StartCoroutine(CustomOnMouseDown());
            }
        }
    }
    IEnumerator CustomOnMouseDown()
    {
        //將物體由世界坐標(biāo)系轉(zhuǎn)化為屏幕坐標(biāo)系,由vector3 結(jié)構(gòu)體變量ScreenSpace存儲(chǔ),以用來明確屏幕坐標(biāo)系Z軸的位置
        Vector3 ScreenPoint = Camera.main.WorldToScreenPoint(transform.position);
 
        //由于鼠標(biāo)的坐標(biāo)系是二維的,需要轉(zhuǎn)化成三維的世界坐標(biāo)系;
        Vector3 WorldPostion = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenPoint.z));
 
        //三維的情況下才能來計(jì)算鼠標(biāo)位置與物體的距離
        Vector3 distance = transform.position - WorldPostion;
 
        //當(dāng)鼠標(biāo)左鍵按下時(shí)
        while (Input.GetMouseButton(0))
        {
            //得到現(xiàn)在鼠標(biāo)的二維坐標(biāo)系位置
            Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, ScreenPoint.z);
            //將當(dāng)前鼠標(biāo)的2維位置轉(zhuǎn)化成三維的位置,再加上鼠標(biāo)的移動(dòng)距離
            Vector3 CurPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + distance;
            //CurPosition就是物體應(yīng)該的移動(dòng)向量賦給transform的position屬性        
            transform.position = CurPosition;
            //鼠標(biāo)釋放前都起作用
            yield return new WaitForFixedUpdate();
        }
    }
}

單指旋轉(zhuǎn)3D物體

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
 
public class RotateControl : GestureControl
{
 
    protected override void InputCheck()
    {
        #region  單點(diǎn)觸發(fā)旋轉(zhuǎn)(真實(shí)模型旋轉(zhuǎn))
        if (Input.touchCount == 1)
        {
            //觸摸為移動(dòng)類型
            if (Input.GetTouch(0).phase == TouchPhase.Moved)
            {
                status = 1;
                try
                {
                    StartCoroutine(CustomOnMouseDown());
                }
                catch (Exception e)
                {
                    Debug.Log(e.ToString());
                }
            }
            
        }
        #endregion
 
        #region   鍵盤A、D、W、S模擬旋轉(zhuǎn)(真實(shí)模型旋轉(zhuǎn))
        if (Input.GetKeyDown(KeyCode.A))
        {
            transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.World);
        }
 
        if (Input.GetKeyDown(KeyCode.D))
        {
            transform.Rotate(Vector3.up, -45 * Time.deltaTime, Space.World);
        }
 
        if (Input.GetKeyDown(KeyCode.W))
        {
            transform.Rotate(Vector3.left, 45 * Time.deltaTime, Space.World);
        }
 
        if (Input.GetKeyDown(KeyCode.S))
        {
            transform.Rotate(Vector3.left, -45 * Time.deltaTime, Space.World);
        }
        #endregion
    }
 
    IEnumerator CustomOnMouseDown()
    {
        //當(dāng)檢測(cè)到一直觸碰時(shí),會(huì)不斷循環(huán)運(yùn)行
        while (Input.GetMouseButton(0))
        {
            //判斷是否點(diǎn)擊在UI上
#if UNITY_ANDROID || UNITY_IPHONE
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
#else
                if (EventSystem.current.IsPointerOverGameObject())
#endif
            {
                Debug.Log("當(dāng)前點(diǎn)擊在UI上");
            }
            else
            {
                float XX = Input.GetAxis("Mouse X");
                float YY = Input.GetAxis("Mouse Y");
                #region
                //判斷左右滑動(dòng)的距離與上下滑動(dòng)距離大小
                if (Mathf.Abs(XX) >= Mathf.Abs(YY))
                {
                    //單指向左滑動(dòng)情況
                    if (XX < 0)
                    {
                        transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.World);
                    }
                    //單指向右滑動(dòng)情況
                    if (XX > 0)
                    {
                        transform.Rotate(-Vector3.up, 45 * Time.deltaTime, Space.World);
                    }
                }
                else
                {
                    //單指向下滑動(dòng)情況
                    if (YY < 0)
                    {
                        transform.Rotate(Vector3.left, 45 * Time.deltaTime, Space.World);
                    }
                    //單指向上滑動(dòng)情況
                    if (YY > 0)
                    {
                        transform.Rotate(-Vector3.left, 45 * Time.deltaTime, Space.World);
                    }
                }
                #endregion
            }
            yield return new WaitForFixedUpdate();
        }
    }
}

雙指縮放3D物體

using UnityEngine;
using System.Collections;
 
public class ZoomControl : GestureControl
{
    //記錄上一次手機(jī)觸摸位置判斷用戶是在左放大還是縮小手勢(shì)  
    private Vector2 oldPosition1;
    private Vector2 oldPosition2;
    //實(shí)時(shí)大小
    Vector3 RealScale = new Vector3(1f, 1f, 1f);
    //原始大小
    float InitialScale = 0;
    //縮放速度
    public float ScaleSpeed = 0.1f;
    //縮放比例
    public float MaxScale = 2.5f;
    public float MinScale = 0.5f;
 
    void Start()
    {
        //獲取物體最原始大小
        InitialScale = this.transform.localScale.x;
    }
 
    protected override void InputCheck()
    {
        #region 多點(diǎn)觸摸縮放(真實(shí)模型縮放)
        
        if (Input.touchCount > 1)
        {
            status = 2;
            StartCoroutine(CustomOnMouseDown());
        }
        #endregion
    }
 
    IEnumerator CustomOnMouseDown()
    {
        //當(dāng)檢測(cè)到一直觸碰時(shí),會(huì)不斷循環(huán)運(yùn)行
        while (Input.GetMouseButton(0))
        {
            //實(shí)時(shí)記錄模型大小
            RealScale = this.transform.localScale;
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                //觸摸位置
                Vector3 tempPosition1 = Input.GetTouch(0).position;
                Vector3 tempPosition2 = Input.GetTouch(1).position;
 
                //函數(shù)返回真為放大,返回假為縮小 
                if (isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2))
                {
                    //判斷是否超過邊界
                    if (RealScale.x < InitialScale * MaxScale)
                    {
                        this.transform.localScale += this.transform.localScale * ScaleSpeed;
                    }
                }
                else
                {
                    //判斷是否超過邊界
                    if (RealScale.x > InitialScale * MinScale)
                    {
                        this.transform.localScale -= this.transform.localScale * ScaleSpeed;
                    }
                }
                //備份上一次的觸摸位置
                oldPosition1 = tempPosition1;
                oldPosition2 = tempPosition2;
            }
 
            yield return new WaitForFixedUpdate();
        }
    }
 
    //記錄手指位置與初始位置是縮小或放大
    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
        if (leng1 < leng2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

聲明:文章內(nèi)容整理來源于網(wǎng)絡(luò),版權(quán)屬于原作者,如有問題,請(qǐng)聯(lián)系我們!

來源:https://www.cnblogs.com/unity3ds/p/10983038.html


Copyright 2019 0743119.com

福感科技有限公司 版權(quán)所有 All Rights Reserved

京ICP備20002031號(hào)

010-89968230