1. 程式人生 > >unity3d:控制模型單指旋轉,雙指縮放,並停止控制一段時間後自轉

unity3d:控制模型單指旋轉,雙指縮放,並停止控制一段時間後自轉

控制模型:移動端:單指旋轉,雙指縮放
PC:左鍵旋轉,滾輪縮放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.Event;
public class ModelCtrl : MonoBehaviour {
    public Transform m_tar;

    public float m_xAngles = 0.0f;
    public float m_yAngles = 0.0f;

    //物體的旋轉角度
    public float
m_rotateSpeed = 0.2f; public float m_scale = 1.0f; public float m_scaleStep = 0.5f; public float m_scaleMax = 10.0f; public float m_scaleMin = 1.0f; bool m_isRotate = false; bool m_isScale = false; private float m_yMinLimit = -90; private float m_yMaxLimit = 90; Vector2 m_oldPos0; Vector2 m_oldPos1; // Use this for initialization
void Start () { m_scale = m_tar.localScale.x; m_scaleMax = 1.5f * m_scale; m_scaleMin = 0.5f * m_scale; } // Update is called once per frame void Update () { m_isRotate = false; m_isScale = false; if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) { if
(Input.touchCount == 1) { if (Input.GetTouch(0).phase == TouchPhase.Moved) { m_xAngles = Input.GetAxis("Mouse X"); m_yAngles = Input.GetAxis("Mouse Y"); EventManager.Instance.DispatchEvent((uint)ZCY.EventType.StopSelfRotate); m_isRotate = true; } } else if (Input.touchCount == 2) { if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved) { var curPos0 = Input.GetTouch(0).position; var curPos1 = Input.GetTouch(1).position; if (IsEnlarge(m_oldPos0, m_oldPos1, curPos0, curPos1)) { FullRenderScale(true, m_scaleStep); } else { FullRenderScale(false, m_scaleStep); } EventManager.Instance.DispatchEvent((uint)ZCY.EventType.StopSelfRotate); m_oldPos0 = curPos0; m_oldPos1 = curPos1; m_isScale = true; } } } else //別的平臺 { if (Input.GetAxis("Mouse ScrollWheel") < 0) { FullRenderScale(false, m_scaleStep); EventManager.Instance.DispatchEvent((uint)ZCY.EventType.StopSelfRotate); m_isScale = true; } else if (Input.GetAxis("Mouse ScrollWheel") > 0) { FullRenderScale(true, m_scaleStep); EventManager.Instance.DispatchEvent((uint)ZCY.EventType.StopSelfRotate); m_isScale = true; } else if (Input.GetMouseButton(0)) { m_xAngles = Input.GetAxis("Mouse X"); m_yAngles = Input.GetAxis("Mouse Y"); EventManager.Instance.DispatchEvent((uint)ZCY.EventType.StopSelfRotate); m_isRotate = true; } } } bool IsEnlarge(Vector2 old0, Vector2 old1, Vector2 cur0, Vector2 cur1) { var leng1 = Mathf.Sqrt((old0.x - old1.x) * (old0.x - old1.x) + (old0.y - old1.y) * (old0.y - old1.y)); var leng2 = Mathf.Sqrt((cur0.x - cur1.x) * (cur0.x - cur1.x) + (cur0.y - cur1.y) * (cur0.y - cur1.y)); if (leng1 < leng2) { return true; } else { return false; } } void FullRenderScale(bool isBig, float scale) { if (isBig) { if (m_scale < m_scaleMax) { m_scale += scale; } } else { if (m_scale > m_scaleMin) { m_scale -= scale; } } } private void LateUpdate() { if (m_isRotate == true) { m_tar.Rotate(Vector3.up, -m_xAngles, Space.World); m_tar.Rotate(Vector3.right, m_yAngles , Space.World); //m_tar.GetComponent<Rigidbody>().AddTorque( m_yAngles* m_rotateSpeed, -m_xAngles* m_rotateSpeed, 0); } if (m_isScale == true) { m_tar.transform.localScale = new Vector3(m_scale, m_scale, m_scale); } } static float ClampAngle(float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; if (angle > 180) angle -= 360; return Mathf.Clamp(angle, min, max); } }

當有控制模型旋轉,縮放是,停止模型的自轉,等待5s後無控制事件,再恢復自轉

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.Event;
using System.Diagnostics;

public class SelfRotate : MonoBehaviour {
    float _RotationSpeed = 1.0f;
    Stopwatch m_watch = new Stopwatch();
    bool m_isEnable = true;
    // Use this for initialization
    void Start () {
        m_watch.Stop();
        m_watch.Reset();
        EventManager.Instance.AddEventListener((uint)ZCY.EventType.StopSelfRotate, OnStopSelfRotate);

    }

    private void OnDestroy()
    {
        EventManager.Instance.RemoveEventListener((uint)ZCY.EventType.StopSelfRotate, OnStopSelfRotate);
    }

    // Update is called once per frame
    void Update ()
    {
        if (m_isEnable == false && m_watch.ElapsedMilliseconds > 5000)
        {
            m_isEnable = true;
            m_watch.Stop();
            m_watch.Reset();
        }
        if (m_isEnable)
        {
            transform.Rotate(Vector3.down * _RotationSpeed, Space.World); //物體自轉
        }
    }

    void OnStopSelfRotate(EventData note)
    {
        m_isEnable = false;
        m_watch.Stop();
        m_watch.Reset();
        m_watch.Start();
    }
}