2.1 创建一个picture-in-picture(画中画)效果Unity
Thepoly 14575 0
实名

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

发布于 2022-3-24 17:00:40

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

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分享
游戏引擎相关内容


Chapter 2: Using Cameras
第二章 使用摄像机
目录
介绍
Introduction

2.1 创建一个picture-in-picture(画中画)效果
2.1 Creating a picture-in-picture effect

2.2 在多个摄像机间转换
2.2 Switching between multiple cameras

2.3 自定义镜头光晕(镜头眩光)效果
2.3 Customizing the lens flare effect

2.4 根据屏幕内容制作纹理图片
2.4 Making textures from screen content

2.5 伸缩镜头变焦
2.5 Zooming a telescopic camera

2.6 制作一个检视摄像机
2.6 Making an inspect camera

2.7 利用Shuriken系统制作粒子特效
2.7 Creating particle effects using Shuriken

2.8 展示小地图
2.8 Displaying a mini-map
Introduction(介绍)
As developers, we should never forget to pay attention to the cameras. After all, they are the windows from which our players see our game. In this chapter, we will take a look at ways of making them more interesting within the player experience.
作为一个开发者,我们不应忘了要对摄像机格外注意。毕竟,他们是玩家看到我们游戏的窗口。在这一章节,我们将学习让镜头更加有趣的方法,增强玩家体验。(注意,这一章节按照本书教程的话,最好使用unity4.X版本,C#库在新版本里面有更新,很可能会造成代码测试报错)

2.1 Creating a picture-in-picture effect2.1 创建一个picture-in-picture(画中画)效果
Having more than one viewport displayed can be useful in many situations. For example, you might want to show simultaneous events going on in different locations, or maybe you want to have a separate window for hot-seat multiplayer games. Although you could do it manually by adjusting the Normalized Viewport Rect parameters on your camera, this recipe includes a series of extra preferences to make it more independent from the user's display configuration.
多视口展示在很多情况下是十分有用的。例如,你想展示在不同地点、同时发生的事件,或者你想为多玩家游戏提供一个分视窗。虽然,你可以在你的摄像机上手动调节Normalized Viewport Rect(在unity5之后是Viewport Rect,矩形视口选项)参数,但是这一部分内容包含了额外的参考,让视口在用户的展示设置中变得更加独立。

Getting ready (如何去做)
For this recipe, we have prepared a package named basicLevel containing a scene. The package is in the  0423_02_01_02 folder.
为了这一章节的内容,我们需要准备一个名为basicLevel的包,加载到场景之中。这个包在0423_02_01_02文件夹之中。(这里面的这个文件夹应该是本书自带的电子资源,目前没有,不过不影响学习)。

How to do it (如何去做)
To create a picture-in-picture display, just follow these steps:
创建一个画中画的展示,需要遵循以下步骤:

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

2. In the Project view, open basicScene, inside the folder02_01_02. This is a basic scene featuring a directional light, a camera, and some geometry.
2. 在项目面板里,打开位于folder02_01_02文件夹里的basicScene文件。这是一个包含一个平行光源、一个摄像机和一些几何体的基本场景。

3. Add the Camera option to the scene through the Create dropdown menu on top of the Hierarchy view, as shown in the following screenshot:
3. 在Hierarchy面板中,点击Create,在下拉菜单中,选择Camera选项,在场景中加入一个摄像机,如下图所示:
4. Select the camera you have created and, in the Inspector view, set its Depth to 1:
4. 选中你创建的摄像机,在Inspector面板中,将Depth设置为1:
5. In the Project view, create a new C# script and rename it  PictureInPicture.
5. 在Project面板里,创建一个新的C#脚本,将它重命名为PictureInPicture(右键Create|C#Script)

6. Open your script and replace everything with the following code:
6. 打开脚本,将内部的所有内容替换为以下代码:
using UnityEngine;
public class PictureInPicture: MonoBehaviour {
public enum HorizontalAlignment{left, center, right};
public enum VerticalAlignment{top, middle, bottom};
public HorizontalAlignment horizontalAlignment =
HorizontalAlignment.left;
public VerticalAlignment verticalAlignment =
VerticalAlignment.top;
public enum ScreenDimensions{pixels, screen_percentage};
public ScreenDimensions dimensionsIn = ScreenDimensions.
pixels;
public int width = 50;
public int height= 50;
public float xOffset = 0f;
public float yOffset = 0f;
public bool update = true;
private int hsize, vsize, hloc, vloc;
void Start (){
AdjustCamera ();
}
void Update (){
if(update)
AdjustCamera ();
}
void AdjustCamera(){
if(dimensionsIn == ScreenDimensions.screen_percentage){
hsize = Mathf.RoundToInt(width * 0.01f * Screen.width);
vsize = Mathf.RoundToInt(height * 0.01f * Screen.height);
} else {
hsize = width;
vsize = height;
}
if(horizontalAlignment == HorizontalAlignment.left){
hloc = Mathf.RoundToInt(xOffset * 0.01f *Screen.width);
} else if(horizontalAlignment == HorizontalAlignment.right){
hloc = Mathf.RoundToInt((Screen.width - hsize) - (xOffset * 0.01f * Screen.width));
} else {
hloc = Mathf.RoundToInt(((Screen.width * 0.5f) - (hsize * 0.5f)) - (xOffset * 0.01f * Screen.height));
}
if(verticalAlignment == VerticalAlignment.top){
vloc = Mathf.RoundToInt((Screen.height - vsize) - (yOffset * 0.01f * Screen.height));
} else if(verticalAlignment == VerticalAlignment.bottom){
vloc = Mathf.RoundToInt(yOffset * 0.01f *Screen.height);
} else {
vloc = Mathf.RoundToInt(((Screen.height *0.5f) - (vsize * 0.5f)) - (yOffset * 0.01f * Screen.height));
}
camera.pixelRect = new Rect(hloc,vloc,hsize,vsize);##Unity4中使用此行代码##
this.GetComponent Camera ().pixelRect = new Rect(hloc,vloc,hsize,vsize);##Unity5中使用此行代码##
}
}

7. Save your script and attach it to the new camera that you created previously.
7. 保存你的脚本,将它附着到你新创建的摄像机上(Attach附着,就是“挂载”操作,即直击左键脚本按住拖拽到摄像机上)

8. Uncheck the new camera's Audio Listener component and change some of the  parameters: change Horizontal Alignment to Right, Vertical Alignment to Top, and Dimensions In to pixels. Leave XOffset and YOffset as 0,change Width to 400 and Height to 200, as shown below:
8. 不要选中新摄像机的Audio Listener组建,之后修改一些PictureInPicture的一些参数:将Horizontal Alignment(水平线校准)设置为right(右), Vertical Alignment(垂直校准)设置为Top(上), Dimensions In设置为 pixels(像素)。让 XOffset和YOffset归0,Width(宽)为400、Height(高)为200,如图所示:
9. Play your scene. The new camera's viewport should be visible on the top right of the screen:
9. 运行你的场景,一个新的摄像机视窗应该在你屏幕的右上角,如图:
How it works (工作原理)
Our script changes the camera's Normalized Viewport Rect parameters, thus resizing and positioning the viewport according to the user preferences
我们的脚本改变的是摄像机的Normalized Viewport Rect(常规视窗矩形),因此可以根据使用者的喜好,重新定位视窗的位置。
There's more... (更多)
The following are some aspects of your picture-in-picture you could change.
你可以调节picture-in-picture的以下几个方面

Making the picture-in-picture proportional to the screen's size
让picture-in-picture和屏幕尺寸等比例
If you change the Dimensions In option to screen_percentage , the viewport size will be based on the actual screen's dimensions instead of pixels.
如果你将Dimensions In选项改变为screen_percentage,视窗的尺寸就会基于你实际屏幕的大小等比例缩放,而不是固定的多少像素了。

Changing the position of the picture-in-picture
改变picture-in-picture的位置
Vertical Alignment and Horizontal Alignment can be used to change the viewport's origin. Use them to place it where you wish.
利用垂直和水平校准选项改变你的视窗位置,把它移动到你想的地方。

Preventing the picture-in-picture from updating on every frame
阻止picture-in-picture 逐帧更新
Leave the Update option unchecked if you don't plan to change the viewport position in running mode. Also, it's a good idea to leave it checked when testing and then uncheck it once the position has been decided and set up.
如果你打算在游戏运行模式下改变视窗的位置,可以取消勾选Update选项。当然,在测试的时候可以勾上Update,在位置已经决定好或是已经放置好的时候,可以将它取消勾选。

本帖被以下画板推荐:

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

本版积分规则

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