Unity Camera Change C#

本文关键字:Change Camera Unity | 更新日期: 2023-09-27 17:59:50

我有三个字符,每个字符都有一个相机。默认情况下,它们仅被禁用一个。我做了一个字符选择器,用来改变它们。我有一个问题,我可以移动选定的相机,但相机停留在最后一个。这是脚本:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityStandardAssets.Utility;

    public class GameManageer : MonoBehaviour {

        public Camera[] cams = new Camera[3];
        public Character CurrentCharacter;
        public List<Character> Characters = new List<Character>();
        public List<Item> AllItems;
        bool ShowCharWheel;
        public int SelectedCharacter;
        public int lastCharacter;
        public static GameManageer Instance;

        void Awake(){
            Instance = this;
            foreach (Character c in Characters){
                c.Instance = Instantiate(c.PlayerPrefab, c.HomeSpawn.position, c.HomeSpawn.rotation) as GameObject;
                c.Instance.GetComponent<PlayerController>().LocalCharacter = c;
            }
            ChangeCharacter(Characters[PlayerPrefs.GetInt("SelectedChar")]);
        }

        // Use this for initialization
        void Start () {

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

            if (Input.GetKey (KeyCode.C)) {
                ShowCharWheel = true;
            } else {
                ShowCharWheel = false;
            }

        }

        void ChangeCharacter(Character c){
            lastCharacter = SelectedCharacter;
            SelectedCharacter = Characters.IndexOf (c);
            cams [SelectedCharacter].gameObject.SetActive (true);
            cams [lastCharacter].gameObject.SetActive (false);
            CurrentCharacter = c;
            Characters [lastCharacter].Instance.GetComponent<PlayerController> ().CanPlay = false;
            Characters [SelectedCharacter].Instance.GetComponent<PlayerController> ().CanPlay = true;              
            PlayerPrefs.SetInt ("SelectedChar", SelectedCharacter);
        }

        void OnGUI(){
            if (ShowCharWheel) {

                GUILayout.BeginArea(new Rect(Screen.width - 64, Screen.height - 192,64,192));
                foreach (Character c in Characters){
                    if (GUILayout.Button(c.Icon,GUILayout.Width(64),GUILayout.Height(64))){
                        ChangeCharacter(c);
                    }
                }
                GUILayout.EndArea();
            }
        }
    }
    [System.Serializable]
    public class Character {
        public string Name;
        public Texture2D Icon;
        public GameObject PlayerPrefab;
        public GameObject Instance;
        public Transform HomeSpawn;
    }
    [System.Serializable]
    public class Item{
        public string Name;
        public Texture2D Icon;
        public ItemInstance InstancePrefab;
    }

Unity Camera Change C#

这应该完成任务:

cams[SelectedCharacter].enabled = true;
cams[lastCharacter].enabled = false;

使用深度:

foreach (Camera cam in cams)
{
    cam.depth = cam == cams[SelectedCharacter] ? 10 : 0;
}

不过,我认为这里的真正问题是,场景中有更多的相机,你也必须管理这些相机,除了只有最后和当前选定的角色。。。在这种情况下:

foreach (Camera cam in cams)
{
    cam.SetActive(cam == cams[SelectedCharacter]);
}