需要把以下3个脚本加到camera上面,并且把Rotate那个脚本的target设为某个物体即可,可以显示fps,如下:
一个是cameraSetup脚本
using UnityEngine;
using System.Collections;
public class CameraSetup : MonoBehaviour {
public int ScreenWid = 1280;
public int ScreenHei = 800;
private int scaleWidth ;
private int scaleHeight ;
// Use this for initialization
void Start () {
setDesignContentScale();
}
public void setDesignContentScale()
{
#if UNITY_ANDROID
//if(scaleWidth ==0 && scaleHeight ==0)
//{
int width = Screen.currentResolution.width;
int height = Screen.currentResolution.height;
int designWidth = ScreenWid;
int designHeight = ScreenHei;
float s1 = (float)designWidth / (float)designHeight;
float s2 = (float)width / (float)height;
if(s1 < s2) {
designWidth = (int)Mathf.FloorToInt(designHeight * s2);
} else if(s1 > s2) {
designHeight = (int)Mathf.FloorToInt(designWidth / s2);
}
float contentScale = (float)designWidth/(float)width;
if(contentScale < 1.0f) {
scaleWidth = designWidth;
scaleHeight = designHeight;
}
//}
if(scaleWidth >0 && scaleHeight >0)
{
if(scaleWidth % 2 == 0) {
scaleWidth += 1;
} else {
scaleWidth -= 1;
}
Screen.SetResolution(scaleWidth,scaleHeight,true);
}
#endif
}
void OnApplicationPause(bool paused)
{
if (paused) {
} else {
setDesignContentScale();
}
}
// Update is called once per frame
void Update () {
}
}
另一个转动的
using UnityEngine;
using System.Collections;
public class RotateAndPinch_2 : MonoBehaviour
{
public Transform target;
private float minVertical = 0f;
private float maxVertical = 85f;
private float x = 0.0f;
private float y = 0.0f;
private float distance = 0.0f;
private float newdis = 0;
private float olddis = 0;
// Use this for initialization
void Start()
{
distance = (transform.position - target.position).magnitude;
}
// Update is called once per frame
void Update()
{
transform.LookAt(target);
float dt = Time.deltaTime;
x = transform.eulerAngles.y;
y = transform.eulerAngles.x;
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
float x1 = Input.GetAxis("Mouse X");
float y1 = Input.GetAxis("Mouse Y");
x += x1 * dt * 150;
y += -y1 * dt * 150;
SetPos(x, y);
}
}
if (Input.touchCount == 2)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
Vector3 s1 = Input.GetTouch(0).position;
Vector3 s2 = Input.GetTouch(1).position;
newdis=Vector2.Distance(s1,s2);
if(newdis>olddis)
{
distance-=Time.deltaTime*2f;
}
if(newdis<olddis)
{
distance+=Time.deltaTime*2f;
}
// print("distance = "+distance);
SetPos(x,y);
olddis=newdis;
}
}
}
else
{
if (Input.GetMouseButton(0))
{
float x1 = Input.GetAxis("Mouse X");
float y1 = Input.GetAxis("Mouse Y");
x += x1 * dt * 150f;
y += -y1 * dt * 150f;
SetPos(x, y);
}
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
distance -= Input.GetAxis("Mouse ScrollWheel");
SetPos(x, y);
}
}
}
void SetPos(float x, float y)
{
y = ClampAngle(y, minVertical, maxVertical);
var rotation = Quaternion.Euler(y, x, 0.0f);
var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
将某个物体拖到该脚本的Target里
下面是一个显示FPS的脚本
using UnityEngine;
using System.Collections;
public class showFPS : MonoBehaviour {
public float updateInterval = 0.5F;
private float accum = 0; // FPS accumulated over the interval
private int frames = 0; // Frames drawn over the interval
private float timeleft; // Left time for current interval
private string FPS;
void Start()
{
timeleft = updateInterval;
}
void OnGUI(){
GUIStyle style = new GUIStyle();
style.normal.textColor = new Color( 1, 1, 1);
style.fontSize = 40;
GUI.skin.label.alignment = TextAnchor.UpperCenter;
GUI.Label(new Rect(0,0,200,60),FPS,style);
}
void Update()
{
timeleft -= Time.deltaTime;
accum += Time.timeScale/Time.deltaTime;
++frames;
// Interval ended - update GUI text and start new interval
if( timeleft <= 0.0 )
{
float fps = accum/frames;
FPS ="FPS:"+ System.String.Format("{0:F2}",fps);
timeleft = updateInterval;
accum = 0.0F;
frames = 0;
}
}
}