2.2 在多个摄像机间转换 Unity
Unity技术Thepoly 14728 0
实名

通过了实名认证的内容创造者

发布于 2022-3-24 17:50:58

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 Thepoly 于 2022-3-24 20:05 编辑

src=http___xqimg.imedao.com_17b70f518351426d3fd73b36.png!800.jpgrefer=http___xqimg.imedao.webp.jpg
174551k9r66ex7z719e99h.jpg
Hello,大家好。
本内容有Kinglary分享
游戏引擎相关内容

2.2 Switching between multiple cameras
2.2 在多个摄像机间转换
Choosing from a variety of cameras is a common feature in many genres: race sims, sports sims, tycoon/strategy, and many others. In this recipe, we will learn how to give players the ability of choosing an option from many cameras using their keyboard.
多类镜头之间进行切换、选择,是很多游戏体裁中常见的功能,比如:竞速类游戏、体育游戏、策略类游戏,等等。在本节内容中,我们将学习如何让玩家使用键盘在多个镜头间进行选择。

Getting ready (准备工作)
In order to follow this recipe, we have prepared a package containing a basic level named basicScene . The package is in the folder  0423_02_01_02
为了学习本节内容,我们已经准备好了一个包,里面包含了一个名为basicScene的关卡。这个包在0423_02_01_02文件夹中(这里面的这个文件夹应该是本书自带的电子资源,目前没有,不过不影响学习)。

How to do it (如何去做)
To implement switchable cameras, follow these steps:
实现可选控摄像机,需要遵循以下步骤:

1. Import the  basicLevel package into your Unity project.
1. 将basicLevel 包导入到你的unity工程项目中。

2. In the Project view, open basicScene from the 02_01_02 folder. This is a basic scene featuring a directional light, a camera, and some geometry.
2. 在Project面板,从02_01_02文件夹中打开basicScene。这个基本场景,包括一个平行光源、一个摄像机和一些几何形体。

3. Add two more cameras to the scene. You can do it through the Create drop-down menu on top of the Hierarchy view. Rename them  cam1 and  cam2.
3. 在本场景中加入两个摄像机。你可以在Hierarchy面板中,点击Create,在下拉菜单中进行创建。将他们重命名为cam1和cam2。

4. Change the cam2 camera's position and rotation so it won't be identical to cam1.
4. 调整cam2的摄像机位置和转向,让它和cam1明显不同。

5. Create an Empty game object by navigating to Game Object | Create Empty. Then,rename it  Switchboard.
5. 创建一个Empty(空的)游戏对象,点击Game Object | Create Empty进行创建,让后将其重命名为Switchboard。

6. In the Inspector view, disable the Camera and Audio Listener components of both cam1 and cam2.
6. 在Inspector面板中,将cam1和cam2的Camera和Audio Listener选项都取消掉,如下图:
7. In the Project view, create a new C# script. Rename it CameraSwitch and open it in your editor.
7. 在Project视窗中,创建一个新的C#脚本,将其重命名为CameraSwitch,在你的编译器中打开它(就是双击打开,开始敲代码)。
8. Open your script and replace everything with the following code:
8. 打开脚本,然后将所有内容替换为以下代码:
using UnityEngine;

public class CameraSwitch : MonoBehaviour {
        public GameObject[] cameras;
        public string[] shortcuts;
        public bool changeAudioListener = true;
        void Update (){
                int i = 0;
                for(i=0; i<cameras.Length; i++){
                        if (Input.GetKeyUp(shortcuts))
                                SwitchCamera(i);
                }
        }
        void SwitchCamera ( int index ){
                int i = 0;
                for(i=0; i<cameras.Length; i++){
                        if(i != index){
                                if(changeAudioListener){
                                        cameras.
                                        GetComponent<AudioListener>().enabled = false;
                                }
                                cameras.camera.enabled = false;/* cameras.GetComponent<Camera>().enabled = false;  Unity5中应换成这句  */
                        } else {
                                if(changeAudioListener){
                                        cameras.
                                        GetComponent<AudioListener>().enabled = true;
                                }
                                cameras.camera.enabled = true;/* cameras.GetComponent<Camera>().enabled = ture;  Unity5中应换成这句  */
                        }
                }
        }
}

9. Attach CameraSwitch to the Switchboard game object.
9. 将CameraSwitch脚本挂载到Switchboard游戏对象上。

10. In the Inspector view, set both Cameras and Shortcuts size to 3. Then, drag the scene cameras into the Cameras slots, and type 1 , 2 , and 3 into the Shortcuts text fields, as shown in the next screenshot.
10. (选择Switchboard游戏对象)在Inspector面板中,将Camera和Shortcuts的数量都设置为3.然后,将场景中的三个摄像机分别拖拽到Camera参数栏中的对应位置,并在Shortcuts参数栏的相应位置,分别键入1,2,3(这里的123键是主键盘位置上的123,不是小键盘),如截图:
11. Play your scene and test your cameras.
11. 运行你的场景并测试你的摄像机。
How it works (工作原理)
The script is very straightforward. All it does is capture the key pressed and enable its respective camera (and its Audio Listener, in case the Change Audio Listener option is checked).
这个脚本十分的直观简介,其功能就是让你按键盘上的对应键时,激活各自的摄像机(避免Audio Listener和Change Audio Listener选项选中)
There's more... (更多)
Here are some ideas on how you could try twisting this recipe a bit.
这里面有一些注意,让你可以试着去改变一点本节内容

Using a single-enabled camera
使用单一状态摄像机
A different approach to the problem would be keeping all the secondary cameras disabled and assigning their position and rotation to the main camera via a script (you would need to make a copy of the main camera and add it to the list, in case you wanted to save its transform settings).
解决这个问题有不同方法,整体来说就是保持所有二级摄像机不可使用,并把他们的位置和转向通过脚本赋予给主摄像机(你需要复制一个main camera的备份添加到列表中,如果你想保存他的变换设定)。

Triggering the switch from other events
从其他事件触发选控
Also, you could change your camera from other game object's scripts by using a line of code,such as the one shown here:
当然,你可以根据下列一行代码,从其他游戏对象的脚本中改变你的摄像机
GameObject.Find("Switchboard").GetComponent("CameraSwitch").
SwitchCamera(1);

本帖被以下画板推荐:

还没有设置签名!您可以在此展示你的链接,或者个人主页!
使用道具 <
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表