1. 程式人生 > >Unity做360°全景圖

Unity做360°全景圖

最近接了個用unity做全景圖的活,我的想法是在unity內建一個球,將shader選為Mobile/particles/Alpha Blended,然後將做好的全景圖貼上去就OK了,下面貼上一段攝像機放大縮小的程式碼,怕自己忘了..參考了部分網上的程式碼

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Test01 : MonoBehaviour
{
    public float moveSpeed = 1;//物體旋轉速度  
    public GameObject target;

    private Vector2 oldPosition;
    private Vector2 oldPosition1;
    private Vector2 oldPosition2;


    private float distance = 0;
    private bool flag = false;
    //攝像頭的位置
    private float x = 0f;
    private float y = 0f;
    //左右滑動移動速度
    public float xSpeed = 250f;
    public float ySpeed = 120f;
    //縮放限制係數
    public float yMinLimit = -360;
    public float yMaxLimit = 360;
    //是否旋轉
    private bool isRotate = true;
    //計數器
    private float count = 0;

    public static Test01 _instance;
    //初始化遊戲資訊設定
    void Start()
    {
        _instance = this;
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
        if (GetComponent< Rigidbody >())
            GetComponent< Rigidbody >().freezeRotation = true;
    }

    

    // Update is called once per frame  
    void Update()
    {

        if (isRotate)
        {

            target.transform.Rotate(Vector3.down, Time.deltaTime * moveSpeed,Space.World);
            
        }
        if (!isRotate)
        {
            count += Time.deltaTime;
            if (count > 5)
            {
                count = 0;
                isRotate = true;
            }
        }

            //觸控型別為移動觸控
            if (Input.GetMouseButton(0))
            {
                //根據觸控點計算X與Y位置
                x += Input.GetAxis("Mouse X") * xSpeed *Time.deltaTime;
                y -= Input.GetAxis("Mouse Y") * ySpeed * Time.deltaTime;
            isRotate = false;
            }
            //判斷滑鼠滑輪是否輸入
        float temp = Input.GetAxis("Mouse ScrollWheel");
        if (temp!=0)
        {
            if (temp>0)
            {
                // 這裡的資料是根據我專案中的模型而調節的,大家可以自己任意修改
                if (distance > -15)
                {
                    distance -= 0.5f;
                }
            }
            if (temp<0)
            {
                // 這裡的資料是根據我專案中的模型而調節的,大家可以自己任意修改
                if (distance < 20)
                {
                    distance += 0.5f;
                }
            }
        }

    }

    //計算距離,判斷放大還是縮小。放大返回true,縮小返回false  
    bool IsEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
    {
        //old distance  
        float oldDistance = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
        //new distance  
        float newDistance = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));

        if (oldDistance < newDistance)
        {
            //zoom+  
            return true;
        }
        else
        {
            //zoom-  
            return false;
        }
    }

    //每幀執行,在Update後  
    void LateUpdate()
    {
        if (target)
        {
            //重置攝像機的位置
            y = ClampAngle(y, yMinLimit, yMaxLimit);
            var rotation = Quaternion.Euler(y, x, 0);
            var position = rotation *(new Vector3(0.0f, 0.0f, -distance)) + target.transform.position;

            transform.rotation = rotation;
            transform.position = position;
        }
    }
    float ClampAngle(float angle,float min,float max) {
        if (angle < -360)
            angle += 360;
        if (angle > 360)
            angle -= 360;
        return Mathf.Clamp(angle, min, max);

    }
   
}