打包时候能转动camera的脚本

本文介绍如何使用三个Unity脚本来实现相机的基本设置、旋转及缩放操作,并展示了一个简单的FPS显示脚本。适用于Unity游戏开发中需要实现特定相机效果的场景。

需要把以下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;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿拉平平的小屋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值