Unity一NGUI学习笔记

本篇文章主要介绍NGUI中的UIRoot、UICamera、以及UIAnchor等三个类,基本上使用NGUI这个插件都肯定会用到这三个类,其中UIRoot类主要是为了确保你的GUI对像缩放倍数为 2/(Screen.height);而UICamera则是NGUI插件的基础核心类,这个类中定义了一系列的鼠标、键盘、触屏等动作的响应操作,场景中允许有多个UICamera存在也可以和Unity的主Camera共存;UIAnchor这个类是为了固定你的对象在屏幕中的位置,例如你将UIAnchor的属性Side设置为Center,则无论屏幕规格是多少UIAnchor里的对象在游戏中都会出现在屏幕的正中间。
一 UIRoot
首先是如何用NGUI进行开发,创建一个新的场景,然后把场景里的主Camera删除掉,打开菜单栏中NGUI的“Create a new UI”创建一个新的UI。
Unity一NGUI学习笔记(一)
在NGUI中Layer是一个很重要的概念,在后续的开发过程都会涉及到,建议添加一个Layer专门用来存放GUI对象,创建成功后可以看到有个UIRoot对象,UIRoot最主要的属性是 manualHeight,这个组件会确保它的所有子对象的缩放倍数是2/ manualHeight,如果你勾选了Automatic则缩放倍数会2/屏幕高度。要注意的是这个插件不能和UIOrthoCamera这个组件一起使用。

UICamera
这个类是这篇文章的重点,它主要的功能是对场景中UICamera的管理以及定义场景中的事件类型及其相应操作。凡是所有用到UI对象的Camera都要加入UICamera这个组件,在UICameraTool中定义了许多有用的函数方便了UICamera的使用,不过这个以后再说。
//这是用于管理场景中所有UICamera的链表
static List<UICamera> mList = new List<UICamera>();
在游戏场景初始化阶段,每个UICamera都会根据平台义useMouse、useTouch、useKeyboard和useController 这些属性,分别对应的是能否在场景使用鼠标、触摸屏、键盘以及摇杆。UICamera中有一个很重要的类
public class MouseOrTouch
{
public Vector2 pos; // Current position of the mouse or touch event
public Vector2 delta; // Delta since last update
public Vector2 totalDelta; // Delta since the event started being tracked

public Camera pressedCam; // Camera that the OnPress(true) was fired with

public GameObject current; // The current game object under the touch or mouse
public GameObject pressed; // The last game object to receive OnPress

public ClickNotification clickNotification = ClickNotification.Always;
}
这个类就是相机能监测到的事件类型,然后UICamera定义了几个静态的事件
// Mouse events,分别代表左键、右键和中键的事件
static MouseOrTouch[] mMouse = new MouseOrTouch[] { new MouseOrTouch(), new MouseOrTouch(), new MouseOrTouch() };

// The last object to receive OnHover
static GameObject mHover;

// Joystick/controller/keyboard event
static MouseOrTouch mController = new MouseOrTouch();

// List of currently active touches(触摸屏的事件)
Dictionary<int, MouseOrTouch> mTouches = new Dictionary<int, MouseOrTouch>();

然后用一个static public MouseOrTouch currentTouch来定义当前要处理的事件,UICamera在初始化的时候会被加入 mList这个链表中,然后对链表进行排序,根据相机的深度,深度值越小的相机排位靠前,最靠前的相机为场景的主UICamera,然后只有只有主UICamera才会去监测场景中的事件,其他的UICamera并不执行监测任务。UICamera利用Unity的Raycast去监测事件发生的对象,因为发射出去的Ray对象必须碰撞到Collider才会有反应,所以NGUI中所有需要响应事件的控件均需要添加Collider,同时Ray只会碰撞到深度最小的Collider,Ray射线的最大深度为rangeDistance,当这个值为-1时则发射深度和相机深度一样,主UICamera每一帧都会主动去发射Ray检测鼠标此时触碰到的对象并将其记录在对应的鼠标按键事件中,这是能监测到OnHover这个动作的关键(当然只有在useMouse为true时才会有此操作)。currentTouch这个变量是整个UICamera中控制事件监测的关键所在,记录了当前事件的触发对象和一些其他诸如position位置、dealta时间、totaldealta总时间等属性,然后用currentTouchID记录当前事件的类型,这些类型包括鼠标事件、键盘控制器事件以及触摸屏事件。三个处理函数ProcessMouse(),ProcessTouches ()和ProcessOrthers ()根据currentTouch来定义具体的动作以及当前需要响应的操作,这些动作有:
OnHover (bool isOver) //is sent when the mouse hovers over a collider or moves away.
OnPress ( bool isDown) //is sent when a mouse button gets pressed on the collider.
OnSelect ( bool selected) //is sent when a mouse button is released on the same object as it was pressed on.
OnClick (int button) //is sent with the same conditions as OnSelect, with the added check to see if the mouse has not moved much.
OnDrag (Vector2 delta) //is sent when a mouse or touch gets pressed on a collider and starts dragging it.
OnDrop (GameObject gameObject)// is sent when the mouse or touch get released on a different collider than the one that was being dragged.
OnInput (string text) //is sent when typing (after selecting a collider by clicking on it).
OnTooltip (bool show)// is sent when the mouse hovers over a collider for some time without moving.
OnScroll (float delta) //is sent out when the mouse scroll wheel is moved.
OnKey (KeyCode key)// is sent when keyboard or controller input is used.
至于具体的事件和操作定义这里就不详细解释,有兴趣的同学可以找源码来看。凡是场景中的对象都可以触发到这些事件,所以具体的做法是在对象上添加相应的Collider,并在组件中定义以上函数即可,一旦相应的事件触发则会调用对象相应的函数。值得注意的是UICamera只会监测到同一Layer的对象,所以建议场景中的对象和GUI对象分不同的Layer存放。
PS: 目前尚未弄懂的问题是UICamera中用于处理Input(即文字输入)的对象static GameObject mSel的初始化问题及其键盘响应OnSelect事件问题。


三 UIAnchor
这个类可以确保你的对象在游戏屏幕中的位置,这样你就可以不用为不同的游戏屏幕规格而写不同的代码了
public enum Side
{
BottomLeft,
Left,
TopLeft,
Top,
TopRight,
Right,
BottomRight,
Bottom,
Center,
}
这是Side的取值范围,你还可以通过 relativeOffset这个属性来设置偏移量,这个偏移量是指屏幕大小的百分比。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值