Unity 4.x游戏开发技巧集锦第2章摄像机的应用
Unity 4.x游戏开发技巧集锦第2章摄像机的应用
Unity 4.x游戏开发技巧集锦2.1 设置双游戏视图
Unity 4.x游戏开发技巧集锦2.1.1 环境准备
Unity 4.x游戏开发技巧集锦2.1.2 编写脚本
- 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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。