做2D游戏的时候,无非有三种模式,一种是只有竖直向上,一种是只有水平方向,一种是有水平又有竖直方向,我最近做游戏多关卡模式,不同模式就有着不一样的相机控制,按照平时的写法,也许有很多人就一下子写了三个相机脚本,现在我用了一个枚举类型来控制三个不一样的相机,贴代码:
using UnityEngine;
using System.Collections;
public class CameraCtrl : MonoBehaviour {
public enum CameraType
{
Vertical,
Horizontal,
Normal
}
public CameraType cameraType;
public float dampTime = 1.5f;
public Transform target;
// 相机移动速度,初始速度清零
private Vector3 velocity = Vector3.zero;
// 相机单例
private static CameraCtrl instance;
public static CameraCtrl Instance {
get {
return instance;
}
}
// 屏幕的默认宽高的1/100 (预编译)
#if UNITY_ANDROID
private static float devHeight = 8.54f;
private static float devWidth = 4.8f;
#elif UNITY_IPHONE
private static float devHeight = 9.6f;
private static float devWidth = 6.4f;
#else
private static float devHeight =