[ユニティ] [カメラアングル]複数のカメラを切り替える



Switch Between Multiple Cameras



異なるカメラを切り替えるには2つの方法があります。

まず、アレイを介してシーンに設定されたカメラを保存します



1.カメラのゲームオブジェクトのアクティブを変更します



2.カメラの奥行きを変更します


カメラの奥行きは、カメラ間の切り替え順序に影響します。すべて0であるか、0、1、2、3、4、5の順序で配置されます。



Depthが-1の場合、Depthが0のカメラがその前にランク付けされます。

シーン内のカメラコンポーネントを持つすべてのオブジェクトを検索するための2つの検索方法。

1.そのtransform.tagのTAG文字列を判断することにより、目的のカメラを見つけることができます。

前提は、必要なすべてのカメラオブジェクトのTAGを設定することです。

2.次の図に示すように、すべてのカメラを空のオブジェクトCamerasなどの空のオブジェクトに配置することにより、この空のオブジェクトCamerasを見つけ、その子オブジェクトをトラバースしてカメラを見つけることができます。 (下図の方法)

using System.Collections using System.Collections.Generic using UnityEngine public class Test_Cameras : MonoBehaviour { /// /// The child object is the parent object of the camera /// public Transform cameras_trans /// /// /// public List cameras_trans_list= new List() /// /// /// public Camera[] cameras_trans_list1 /// /// /// public Camera[] cameras_trans_list2 /// /// Index /// public int index = -1 /// /// The length of the camera /// public int cameras_length /// /// Previous camera /// public Camera upCamera = null // Use this for initialization void Start () { index = -1 findCameras() } /// /// /// private void findCameras() { cameras_trans_list1 = Resources.FindObjectsOfTypeAll(typeof(Camera)) as Camera[] //GameObject.FindObjectsOfTypeAll(typeof(Camera))//Deprecated if (cameras_trans != null) { cameras_trans_list2 = cameras_trans.GetComponentsInChildren()//traverse all child objects cameras_length = cameras_trans_list2.Length } else { cameras_length = cameras_trans_list1.Length } } /// /// Previous camera /// public void UpCamera() { if (index == -1) { index = 0 } if (index > 0 && index 0) { for (int i=0 i 0 && upCamera == null) { index = 0 for (int i = 0 i 0 && upCamera == null ) { for (int i = 0 i 0 && upCamera != null && upCamera.gameObject.activeSelf == true ) { for (int i = 0 i 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) } }