Unity 4.x游戏开发技巧集锦第2章摄像机的应用

来源:互联网 发布:windows7 apache 编辑:程序博客网 时间:2024/05/22 09:48

Unity 4.x游戏开发技巧集锦第2章摄像机的应用

作为游戏开发者,千万不要忽略了摄像机(Camera)的重要性。毕竟玩家是通过摄像机,才看到了游戏的视图。本章将介绍一些摄像机的常见应用。本文选自《Unity 4.x游戏开发技巧集锦》

Unity 4.x游戏开发技巧集锦2.1  设置双游戏视图

很多游戏里,一个游戏的视图中,经常会包含另一个游戏视图。而两个视图所呈现的,是由两个摄像机在场景的不同位置所拍摄的画面。例如,《QQ飞车》中,除了第三人称视图以外,游戏视图的右侧还有一个跑道位置预览视图,如图2-1所示。本节将模拟这种情况,然后说明生成这种视图效果的方法。本文选自《Unity 4.x游戏开发技巧集锦》

图2-1  《QQ飞车》中的双游戏视图

Unity 4.x游戏开发技巧集锦2.1.1  环境准备

首先,除了场景中默认生成的Main Camera对象外,还需要为场景添加4个游戏对象:Directional light、Camera、Cube和Sphere。改变两个摄像机的位置,使得它们看到场景中不同的游戏对象,如图2-2所示。

图2-2  为场景添加游戏对象,并调整它们的位置
Main Camera所拍摄到的视图里有球体,Camera所拍摄到的视图里有立方体,修改Camera拍摄视图的背景颜色,具体操作是:选中Camera,在查看器中修改名为Camera的组件的Background属性为其它的颜色,如图2-3所示。最终两个游戏视图合并时,背景颜色可以区分两个视图的边界。本文选自《Unity 4.x游戏开发技巧集锦》

                      
                                                                   图2-3  修改摄像机所拍摄视图的背景颜色

Unity 4.x游戏开发技巧集锦2.1.2  编写脚本

然后,在Project视图里创建一个C#脚本文件,并命名为PictureInPicture。打开脚本文件,并向其中添加如下代码:本文选自《Unity 4.x游戏开发技巧集锦》
  • 01 using UnityEngine;
  • 02
  • 03 public class PictureInPicture : MonoBehaviour
  • 04 {
  • 05 //定义枚举类型
  • 06 public enum HorizontalAlignment{left, center, right};
  • 07 public enum VerticalAlignment{top, middle, bottom};
  • 08 public enum ScreenDimensions{pixels, screen_percentage};
  • 09 //定义枚举类型的变量
  • 10 public HorizontalAlignment horizontalAlignment = HorizontalAlignment.left;
  • 11 public VerticalAlignment verticalAlignment = VerticalAlignment.top;
  • 12 public ScreenDimensions dimensionsIn = ScreenDimensions.pixels;
  • 13
  • 14 public int width = 50;
  • 15 public int height= 50;
  • 16 public float xOffset = 0f;
  • 17 public float yOffset = 0f;
  • 18 public bool update = true;
  • 19
  • 20 private int hsize, vsize, hloc, vloc;
  • 21 //游戏对象初始化时,调用此函数
  • 22 void Start ()
  • 23 {
  • 24 AdjustCamera ();
  • 25 }
  • 26 // 游戏运行时,每一帧都调用此函数
  • 27 void Update ()
  • 28 {
  • 29 if(update)
  • 30 AdjustCamera ();
  • 31 }
  • 32 void AdjustCamera()
  • 33 {
  • 34 if(dimensionsIn == ScreenDimensions.screen_percentage)//调节视图为指定百分比大小
  • 35 {   
  • 36 hsize = Mathf.RoundToInt(width * 0.01f * Screen.width);
  • 37 vsize = Mathf.RoundToInt(height * 0.01f * Screen.height);
  • 38 }
  • 39 else//调节视图为指定像素大小
  • 40 {
  • 41 hsize = width;
  • 42 vsize = height;
  • 43 }
  • 44 if(horizontalAlignment == HorizontalAlignment.left)//水平方向上是左对齐
  • 45 {
  • 46 hloc = Mathf.RoundToInt(xOffset * 0.01f * Screen.width);   
  • 47
  • 48 else if(horizontalAlignment == HorizontalAlignment.right)//水平方向上是右对齐
  • 49 {
  • 50 hloc = Mathf.RoundToInt((Screen.width - hsize) - (xOffset * 0.01f * Screen.width));
  • 51
  • 52 else //水平方向上是居中对齐
  • 53 {
  • 54 hloc = Mathf.RoundToInt(((Screen.width * 0.5f) -
  • 55  (hsize * 0.5f)) - (xOffset * 0.01f * Screen.height));
  • 56 }
  • 57 if(verticalAlignment == VerticalAlignment.top)//垂直方向上是顶端对齐
  • 58 {
  • 59 vloc = Mathf.RoundToInt((Screen.height - vsize) - (yOffset * 0.01f * Screen.height));
  • 60 }
  • 61 else if(verticalAlignment == VerticalAlignment.bottom)//垂直方向上是底端对齐
  • 62 {
  • 63 vloc = Mathf.RoundToInt(yOffset * 0.01f * Screen.height);
  • 64 }
  • 65 else//垂直方向上是居中对齐
  • 66 {
  • 67 vloc = Mathf.RoundToInt(((Screen.height * 0.5f) - 
  • 68 (vsize * 0.5f)) - (yOffset * 0.01f * Screen.height));
  • 69 }
  • 70 camera.pixelRect = new Rect(hloc,vloc,hsize,vsize);
  • 71 }
  • 72 }
脚本将依据用户自定义的设置,决定该如何摆放,以及以多大的尺寸,显示Camera所拍摄的视图。本文选自《Unity 4.x游戏开发技巧集锦》

0 0
原创粉丝点击