Unity摄像机的使用一:双游戏地图或设置小地图

<span style="font-size: 14px; font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">今天学习了一下摄像机的运用,这也是游戏中经常用到的,比如说QQ飞车,穿越火线等                                                                                                    </span>

大体的效果如下图: —_—


下面我们就来看看怎么弄。

首先,这肯定需要两个摄像机,除了Main Camera 之外我们需要新建一个摄像机Camera,显而易见

Camera的视图在Main Camera之上,所以Camera的Depth的值要比Mian Camera 大,我们先让这两个

摄像机看到场景中不同的物体;


下面我们开始写脚本控制:

using UnityEngine;
using System.Collections;

public class PictureinPicture : MonoBehaviour {

	//定义枚举类型
	public enum HorizontalAlignment {left, center, right};
	public enum VerticalAlignment{top, middle, bottom};
	public enum ScreenDimensions{pixels, screen_percentage};

	//定义枚举类型的变量
	public HorizontalAlignment horizontalAlignment = HorizontalAlignment.left;
	public VerticalAlignment verticalAlignment = VerticalAlignment.top;
	public ScreenDimensions dimensions = ScreenDimensions.pixels;

	public int width = 50;
	public int height = 50;
	public float xOffset = 0.0f;
	public float yOffset = 0.0f;
	public bool update = true;

	private int hSize, vSize, hLoc, vLoc;

	void Start()
	{
		AdjustCamera();
	}

	//游戏运行时,每一帧都调用此函数
	void Update()
	{
		AdjustCamera();
	}

	//游戏对象初始化时,调用此函数
	void AdjustCamera()
	{
		if (dimensions == ScreenDimensions.screen_percentage)            //调节视图为指定百分比大小
		{
			hSize = (int)(width * 0.01f * Screen.width);
			vSize = (int)(height * 0.01f * Screen.height);
		}
		else       							 //调节视图为指定像素大小
		{
			hSize = height;
			vSize = width;
		}

		if (horizontalAlignment == HorizontalAlignment.left)               //水平方向上是左对齐
		{
			hLoc = (int)(xOffset * 0.01f * Screen.width);
		}
		else if(horizontalAlignment == HorizontalAlignment.right)         //水平方向上是右对齐
		{
			hLoc = (int)((Screen.width - hSize) - (xOffset * 0.01f * Screen.width));
		}
		else                                                             //水平方向上是居中
		{
			hLoc = (int)((Screen.width - hSize) * 0.5f - (xOffset * 0.01f * Screen.width));
		}

		if (verticalAlignment == VerticalAlignment.top)                     //垂直方向为顶端
		{
			vLoc = (int)((Screen.height - vSize) - (yOffset * 0.01f * Screen.height));
		}
		else if (verticalAlignment == VerticalAlignment.bottom)             //垂直方向为底端
		{
			vLoc = (int)(yOffset * 0.01f * Screen.height);
		}
		else								  //垂直方向为居中
		{
			vLoc = (int)((Screen.height - vSize) * 0.5f - (yOffset * 0.01f * Screen.height));
		}

		this.GetComponent<Camera>().pixelRect = new Rect(hLoc, vLoc, hSize, vSize);
	}
}

脚本写好之后把他挂到Camera上



最后通过设置参数达到想要的效果:


当然,如果需要你也可以设置多个视图,只要你不看花眼偷笑

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值