• pc,android,iphone三个平台上摄像机复杂操作


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

    public class TestFocus : MonoBehaviour
    {
    public Text tex;
    public Transform kTarget;
    public float initDistance = 13.7f;
    public float distanceMax = 30f;
    public float desiredDistance;
    public float scrollSpeed = 12f;
    public float rotateSpeed = 8f;
    public float panSpeed = 0.3f;

    private float DPI = 0;
    private float currentDistance = 0f;
    private float mouseX = 0f;
    private float mouseY = 0f;
    private Vector3 _tInitPos;
    private Vector3 velocity = Vector3.zero;
    private float velocityZ = 0f;
    private float delayZ = 0.3f;
    private Quaternion currentRotation;
    private Quaternion desiredRotation;

    private float lastDistance; //实现距离拉近拉远的平滑效果,上一次相机停留点
    private float perDistance; //实现距离拉近拉远的平滑效果,每一帧相机要移动到的位置

    private Vector2 endone; //用来记录缩放趋势
    private Vector2 endtwo;

    private bool isTouch = false; //是否是触屏
    private bool isPc = false;
    private bool centerFinish = true;


    RaycastHit hit;
    bool _isFocus = false;
    Transform mTrans;

    void Awake()
    {
    mTrans = this.transform;

    #if UNITY_ANDROID
    isTouch = true;
    Debug.Log("这里是安卓设备^_^");
    #endif

    #if UNITY_IPHONE
    isTouch = true;
    Debug.Log("这里是苹果设备>_<");
    #endif

    #if UNITY_STANDALONE_WIN
    isPc = true;
    Debug.Log("我是从Windows的电脑上运行的T_T");
    #endif
    }

    // Use this for initialization
    void Start ()
    {
    DPI = 1080f / Screen.width;
    _tInitPos = kTarget.position;
    initDistance = Mathf.Clamp(initDistance, 1.25f, distanceMax);
    currentDistance = desiredDistance = initDistance;

    }

    // Update is called once per frame
    void LateUpdate()
    {
    if (_isFocus)
    {
    kTarget.position = Vector3.SmoothDamp(kTarget.position, hit.transform.position, ref velocity, delayZ - 0.1f);
    desiredDistance = initDistance;
    }
    }


    void Update()
    {

    //键盘控制中的居中显示
    if (Input.GetMouseButtonUp(0) && isPc)
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(ray, out hit))
    {
    if (hit.collider != null)
    _isFocus = true;
    tex.text = "键盘控制居中";
    }
    }

    //触屏中的居中显示
    if (Input.touchCount == 1 && isTouch && Input.GetTouch(0).phase == TouchPhase.Stationary)
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    if (Physics.Raycast(ray, out hit))
    {
    if (hit.collider != null)
    _isFocus = true;
    tex.text = "触屏控制居中";
    }
    }
    //触屏控制
    if (isTouch)
    {
    if (Input.touchCount > 0)
    {
    tex.text = "触屏";
    if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
    {
    _isFocus = false;
    mouseX += Input.GetTouch(0).deltaPosition.x * rotateSpeed * DPI * 0.3f;
    mouseY -= Input.GetTouch(0).deltaPosition.y * rotateSpeed * DPI * 0.3f;
    tex.text = "触屏旋转中";
    }

    //触屏缩放
    if (Input.touchCount == 2)
    {
    _isFocus = false;

    if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
    {
    Vector2 startone = Input.GetTouch(0).position;
    Vector2 starttwo = Input.GetTouch(1).position;
    if (isEnlarge(startone, starttwo, endone, endtwo))
    {
    tex.text = "触屏缩放中";
    if (desiredDistance < distanceMax)
    desiredDistance += 1f;
    }
    else
    {
    tex.text = "触屏放大中";
    if (desiredDistance > 5f)
    desiredDistance -= 1f;
    }
    endone = startone;
    endtwo = starttwo;
    }
    }

    //触屏实现拖拽
    if (Input.touchCount == 3)
    {
    _isFocus = false;
    if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved
    && Input.GetTouch(2).phase == TouchPhase.Moved)
    {
    tex.text = "触屏拖动中";
    kTarget.rotation = mTrans.rotation;
    kTarget.Translate(Vector2.right * -Input.GetTouch(0).deltaPosition.x *panSpeed);
    kTarget.Translate(mTrans.up * -Input.GetTouch(0).deltaPosition.y * panSpeed);
    }
    }
    }
    }


    //Pc机上控制
    if (isPc)
    {
    //键盘操作,旋转和拖拽
    if (Input.GetMouseButton(0))
    {
    _isFocus = false;
    mouseX += Input.GetAxis("Mouse X") * rotateSpeed;
    mouseY -= Input.GetAxis("Mouse Y") * rotateSpeed;
    tex.text = "键盘旋转中";

    }

    if (Input.GetMouseButton(1))
    {
    _isFocus = false;
    kTarget.rotation = mTrans.rotation;
    kTarget.Translate(Vector2.right * -Input.GetAxis("Mouse X") * panSpeed);
    kTarget.Translate(mTrans.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
    }



    //键盘沿Z轴缩放
    float scrollValue = Input.GetAxis("Mouse ScrollWheel");
    if (scrollValue != 0)
    {
    _isFocus = false;
    desiredDistance -= scrollValue * scrollSpeed;
    tex.text = "键盘控制距离拉近或拉远";
    }
    }

    mouseY = ClampAngle(mouseY, -30f, 30f);
    kTarget.position = new Vector3(Mathf.Clamp(kTarget.position.x, -15f, 15f), Mathf.Clamp(kTarget.position.y, -10f, 10f), Mathf.Clamp(kTarget.position.z, -30f, 30f));

    //限制距离
    if (desiredDistance > distanceMax)
    desiredDistance = distanceMax;

    if (desiredDistance < 1.25f)
    {
    desiredDistance = 1.25f;
    }

    currentRotation = mTrans.rotation;
    desiredRotation = Quaternion.Euler(mouseY, mouseX, 0);

    Quaternion rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * 15f);


    perDistance = desiredDistance;
    perDistance = Mathf.SmoothDamp(lastDistance, perDistance, ref velocityZ, delayZ);
    lastDistance = perDistance;

    Vector3 position = rotation * new Vector3(0f, 0f, -perDistance) + kTarget.position;
    mTrans.rotation = rotation;
    mTrans.position = position;

    }

    float ClampAngle(float angle, float min, float max)
    {
    if (angle < -360)
    angle += 360;
    if (angle > 360)
    angle -= 360;
    return Mathf.Clamp(angle, min, max);
    }

    bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
    float length1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
    float length2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));

    if (length1 < length2)
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }

  • 相关阅读:
    DataPipeline CTO陈肃:驱动软件国产化,客户需求是核心引擎
    DataPipeline王睿:业务异常实时自动化检测 — 基于人工智能的系统实战
    Q&A系列一:DataPipeline常见问题回答
    下篇丨数据融合平台DataPipeline的实际应用场景
    上篇丨数据融合平台DataPipeline的应用场景
    这些传统数据集成的痛,你还在经历吗?
    ETL的两种架构——ETL架构和ELT架构优劣势对比
    2020即将到来,看完这篇帮你详细了解数据策略
    DataPipeline丨「自定义」数据源,解决复杂请求逻辑外部数据获取难题
    SQLI-LABS LESS 1-LESS 22
  • 原文地址:https://www.cnblogs.com/xwwFrank/p/4797978.html
Copyright © 2020-2023  润新知