场景里需要两个摄像机,两个Sprite。其中一组作为正在使用的对象,另一组作为复位的默认位置。
通过鼠标或者最多两个手指的手势可以控制摄像机观察点的移动。
以下这段代码在Windows和Android下测试通过,虽然参数不一定设置地非常恰当,不过功能已经没有问题。
using UnityEngine;
using System.Collections;
public class ControlCamera : MonoBehaviour {
private Vector2 lastTouch1Postion = Vector2.zero;
private Vector2 lastTouch2Postion = Vector2.zero;
private float lastDistance = 60f;
private float touchDistance;
private float touch1Distance;
private float touch2Distance;
private float fieldOfView = DefaultFieldOfView;
public float ScaleDump = 0.5f;
public float RotateDump = 0.5f;
public float TranslateDump = 0.5f;
Vector3 lastMouseButton1Position = Vector3.zero;
Vector3 lastMouseButton2Position = Vector3.zero;
Vector3 lastMouseButton3Position = Vector3.zero;
private Touch touch1;
private Touch touch2;
private Camera thisCamera;
public Camera DefaultCamera;
public GameObject DefaultCameraPoint;
public GameObject lookAtObject;
public bool showLookAtObject = false;
private static Color Transparent_Color_Half = new Color(1.0f, 1.0f, 1.0f, 0.4f);
private static Color Transparent_Color_Full = new Color(1.0f, 1.0f, 1.0f, 0.0f);
private static float DefaultFieldOfView = 60f;
void Start()
{
thisCamera = this.GetComponent<Camera>();
lookAtObject.GetComponent<SpriteRenderer>().color = Transparent_Color_Full;
resetCamera();
}
private void resetCamera()
{
thisCamera.transform.position = DefaultCamera.transform.position;
thisCamera.transform.rotation = DefaultCamera.transform.rotation;
lookAtObject.transform.position = DefaultCameraPoint.transform.position;
lookAtObject.transform.rotation = DefaultCameraPoint.transform.rotation;
thisCamera.transform.LookAt(lookAtObject.transform);
lookAtObject.transform.rotation = thisCamera.transform.rotation;
fieldOfView = DefaultFieldOfView;
thisCamera.fieldOfView = fieldOfView;
}
private void DoShowLookAtObject()
{
if (showLookAtObject)
{
lookAtObject.GetComponent<SpriteRenderer>().color = Transparent_Color_Half;
}
else
{
lookAtObject.GetComponent<SpriteRenderer>().color = Transparent_Color_F