OOIII/

Input.GetTouch实现缩放拖拽正交相机

using UnityEngine;
using System.Collections;

public class TopCamControl : MonoBehaviour
{
    private Camera cam;
    private Vector3 hit_position;
    private Vector3 current_position;
    private Vector3 camera_position;
    private Vector2 oldPosition1;
    private Vector2 oldPosition2;
    public float scalespeed = 0.02f;

    // Use this for initialization
    void Start()
    {
        cam = GetComponent<Camera>();
    }
    // Update is called once per frame
    void LateUpdate()
    {
        //控制相机的范围
        cam.orthographicSize = Mathf.Clamp(cam.orthographicSize, 1f, 15f);
    }

    void Update()
    {
        //缩放
        if (Input.touchCount > 1)
        {
            if (!OnUI(Input.GetTouch(0)) && !OnUI(Input.GetTouch(1)))
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
                {
                    oldPosition1 = Input.GetTouch(0).position;
                    oldPosition2 = Input.GetTouch(1).position;
                }

                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;
                    float tempdis = isEnlarge(oldPosition1, oldPosition2, tempPosition1, tempPosition2);

                    cam.orthographicSize -= tempdis * scalespeed;

                    oldPosition1 = tempPosition1;
                    oldPosition2 = tempPosition2;
                }

                if (Input.GetTouch(0).phase == TouchPhase.Ended)
                {
                    hit_position = Input.GetTouch(1).position;
                    camera_position = transform.position;
                }

                if (Input.GetTouch(1).phase == TouchPhase.Ended)
                {
                    hit_position = Input.GetTouch(0).position;
                    camera_position = transform.position;
                }
            }
        }
        //拖拽
        if (Input.touchCount == 1)
        {
            if (!OnUI(Input.GetTouch(0)))
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    hit_position = Input.mousePosition;
                    camera_position = transform.position;
                }
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    current_position = Input.mousePosition;
                    LeftMouseDrag();
                }
            }
        }
    }

    float isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        float leng1 = Vector2.Distance(oP1, oP2);
        float leng2 = Vector2.Distance(nP1, nP2);
        return leng2 - leng1;
    }
    void LeftMouseDrag()
    {
        // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
        // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
        current_position.z = hit_position.z = camera_position.y;

        // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
        // anyways.  
        Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);

        // Invert direction to that terrain appears to move with the mouse.

        direction = direction * -1;
        Vector3 position = camera_position + direction;
        transform.position = position;
    }
}