欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Unity][摄像机视角]多个摄像机之间切换

程序员文章站 2022-07-05 21:52:29
...

 

 

有2种方法来切换不同的摄像机,

先通过数组来存放已经在场景设置好的摄像机

1.改变摄像机的gameobject的active

[Unity][摄像机视角]多个摄像机之间切换

 

2.改变摄像机的depth

[Unity][摄像机视角]多个摄像机之间切换

 


Camera的Depth影响,摄像机之间切换的排序。都是0,或者是按照顺序0,1,2,3,4,5...进行排列。

如果Depth为-1则,Depth为0的摄像机会排在其前面。

 

 

两种查找方式,查找场景中所有带有Camera组件的物体,

1.可以通过判断其transform.tag的TAG的字符串来 找到所需的摄像机。

前提是得设置所有所需的摄像机的物体的TAG。

2.通过把所有摄像机放入一个空的物体中,如下图所示的空物体Cameras,找到这个空物体Cameras,就能遍历其子物体,找到摄像机。(下图所示的方法)

 

[Unity][摄像机视角]多个摄像机之间切换

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test_Cameras : MonoBehaviour {

    /// <summary>
    /// 子物体为 摄像机的父物体
    /// </summary>
    public Transform cameras_trans;

    /// <summary>
    /// 
    /// </summary>
    public List<Transform> cameras_trans_list= new List<Transform>();

    /// <summary>
    /// 
    /// </summary>
    public Camera[] cameras_trans_list1;

    /// <summary>
    /// 
    /// </summary>
    public Camera[] cameras_trans_list2;

    /// <summary>
    /// 索引
    /// </summary>
    public int index = -1;

    /// <summary>
    /// 摄像机的长度
    /// </summary>
    public int cameras_length;

    /// <summary>
    /// 上一个摄像机
    /// </summary>
    public Camera upCamera = null;

	// Use this for initialization
	void Start () {
        index = -1;
        findCameras();
    }

    /// <summary>
    /// 
    /// </summary>
    private void findCameras()
    {
        cameras_trans_list1 = Resources.FindObjectsOfTypeAll(typeof(Camera)) as Camera[];
        //GameObject.FindObjectsOfTypeAll(typeof(Camera));//已经弃用
        if (cameras_trans != null)
        {
            cameras_trans_list2 = cameras_trans.GetComponentsInChildren<Camera>();//遍历所有的子物体
            cameras_length = cameras_trans_list2.Length;
        }
        else
        {
            cameras_length = cameras_trans_list1.Length;
        }
    }

    /// <summary>
    /// 上一个摄像机
    /// </summary>
    public void UpCamera()
    {
        if (index == -1)
        {
            index = 0;
        }
        if (index > 0
            && index <= cameras_length)
        {
            index = index - 1;
        }
        else if (index ==0)
        {
            index = cameras_length-1;
        }

        if (cameras_trans_list2!=null
            && cameras_trans_list2.Length >0)
        {
            for (int i=0; i< cameras_trans_list2.Length; i++)
            {
                //cameras_trans_list2[i].gameObject.SetActive(false);
                cameras_trans_list2[i].depth = -1;
            }
            //cameras_trans_list2[index].gameObject.SetActive(true);
            cameras_trans_list2[index].depth = 0;
        }
    }
    /// <summary>
    /// 下一个摄像机
    /// </summary>
    public void NextCamera()
    {

        if (index == -1
            && cameras_trans_list2 != null
            && cameras_trans_list2.Length > 0
            && upCamera == null)
        {
            index = 0;
            for (int i = 0; i < cameras_trans_list2.Length; i++)
            {
                cameras_trans_list2[i].gameObject.SetActive(false);
            }
            cameras_trans_list2[index].gameObject.SetActive(true);
        }
        else
        {
            upCamera = cameras_trans_list2[index];
            upCamera.gameObject.SetActive(false);
        }
        /*
        if (cameras_trans_list2 != null
            && cameras_trans_list2.Length > 0
            && upCamera == null
            )
        {
            for (int i = 0; i < cameras_trans_list2.Length; i++)
            {
                cameras_trans_list2[i].gameObject.SetActive(false);
            }
        }
        else if (cameras_trans_list2 != null
            && cameras_trans_list2.Length > 0
            && upCamera != null
            && upCamera.gameObject.activeSelf == true
            )
        {
            for (int i = 0; i < cameras_trans_list2.Length; i++)
            {
                cameras_trans_list2[i].gameObject.SetActive(false);
            }
        }*/
        if (cameras_trans_list2 != null
           && cameras_trans_list2.Length > 0
           && upCamera != null
           && upCamera.gameObject.activeSelf == true
           )
        {
            upCamera.gameObject.SetActive(false);
        }

        if (index >= 0
            && index < (cameras_length - 1))
        {
            index = index + 1;
        }
        else if (index == (cameras_length - 1))
        {
            index = 0;
        }
        cameras_trans_list2[index].gameObject.SetActive(true);
    }
}