[Unity3D]在游戏开发中Notification Center的简单使用与案例说明

来源:互联网 发布:知茵女装网上有卖吗 编辑:程序博客网 时间:2024/05/17 21:07

学习Unity脚本推荐:Unity3D官网索引


Notification Center,顾名思义,就是用来处理各种通知的信息中心。

Unity3D的官网解释如下:

Notification Center的脚本源码如下:

import System.Collections.Generic;//    NotificationCenter is used for handling messages between GameObjects.   //    GameObjects can register to receive specific notifications.  When another objects sends a notification of that type, all GameObjects that registered for it and implement the appropriate message will receive that notification.   //    Observing GameObjetcs must register to receive notifications with the AddObserver function, and pass their selves, and the name of the notification.  Observing GameObjects can also unregister themselves with the RemoveObserver function.  GameObjects must request to receive and remove notification types on a type by type basis.   //    Posting notifications is done by creating a Notification object and passing it to PostNotification.  All receiving GameObjects will accept that Notification object.  The Notification object contains the sender, the notification type name, and an option hashtable containing data.//    To use NotificationCenter, either create and manage a unique instance of it somewhere, or use the static NotificationCenter.// We need a static method for objects to be able to obtain the default notification center.// This default center is what all objects will use for most notifications.  We can of course create our own separate instances of NotificationCenter, but this is the static one used by all.private static var defaultCenter : NotificationCenter;static function DefaultCenter () {    // If the defaultCenter doesn't already exist, we need to create it    if (!defaultCenter) {        // Because the NotificationCenter is a component, we have to create a GameObject to attach it to.        var notificationObject: GameObject = new GameObject("Default Notification Center");        // Add the NotificationCenter component, and set it as the defaultCenter        defaultCenter = notificationObject.AddComponent(NotificationCenter);    }       return defaultCenter;} // Our hashtable containing all the notifications.  Each notification in the hash table is an ArrayList that contains all the observers for that notification.var notifications: Hashtable = new Hashtable();// AddObserver includes a version where the observer can request to only receive notifications from a specific object.  We haven't implemented that yet, so the sender value is ignored for now.function AddObserver (observer, name: String) { AddObserver(observer, name, null); }function AddObserver (observer, name: String, sender) {    // If the name isn't good, then throw an error and return.    if (name == null || name == "") { Debug.Log("Null name specified for notification in AddObserver."); return; }    // If this specific notification doens't exist yet, then create it.    if (!notifications[name]) {        notifications[name] = new List.<Component>();    }       var notifyList: List.<Component> = notifications[name];       // If the list of observers doesn't already contains the one that's registering, then add it.    if (!notifyList.Contains(observer)) { notifyList.Add(observer); }}// RemoveObserver removes the observer from the notification list for the specified notification typefunction RemoveObserver (observer, name: String) {    var notifyList: List.<Component> = notifications[name];       // Assuming that this is a valid notification type, remove the observer from the list.    // If the list of observers is now empty, then remove that notification type from the notifications hash.  This is for housekeeping purposes.    if (notifyList) {        if (notifyList.Contains(observer)) { notifyList.Remove(observer); }        if (notifyList.Count == 0) { notifications.Remove(name); }    }}// PostNotification sends a notification object to all objects that have requested to receive this type of notification.// A notification can either be posted with a notification object or by just sending the individual components.function PostNotification (aSender, aName: String) { PostNotification(aSender, aName, null); }function PostNotification (aSender, aName: String, aData) { PostNotification(new Notification(aSender, aName, aData)); }function PostNotification (aNotification: Notification) {    // First make sure that the name of the notification is valid.    //Debug.Log("sender: " + aNotification.name);    if (aNotification.name == null || aNotification.name == "") { Debug.Log("Null name sent to PostNotification."); return; }    // Obtain the notification list, and make sure that it is valid as well    var notifyList: List.<Component> = notifications[aNotification.name];    if (!notifyList) { Debug.Log("Notify list not found in PostNotification: " + aNotification.name); return; }       // Create an array to keep track of invalid observers that we need to remove    var observersToRemove = new List.<Component>();    // Itterate through all the objects that have signed up to be notified by this type of notification.    for (var observer in notifyList) {        // If the observer isn't valid, then keep track of it so we can remove it later.        // We can't remove it right now, or it will mess the for loop up.        if (!observer) { observersToRemove.Add(observer);        } else {            // If the observer is valid, then send it the notification.  The message that's sent is the name of the notification.            observer.SendMessage(aNotification.name, aNotification, SendMessageOptions.DontRequireReceiver);        }    }       // Remove all the invalid observers    for (observer in observersToRemove) {        notifyList.Remove(observer);    }}// The Notification class is the object that is send to receiving objects of a notification type.// This class contains the sending GameObject, the name of the notification, and optionally a hashtable containing data.class Notification {    var sender;    var name : String;    var data;    function Notification (aSender,  aName: String) { sender = aSender; name = aName; data = null; }    function Notification (aSender, aName: String, aData) { sender = aSender; name = aName; data = aData; }}

接下来就是在需要的地方声明这个通知,我们借用上一个脚本案例,传送:

[Unity3D]简单的鼠标碰撞检测与事件监听

稍作修改:

function Update () {if(Input.GetButtonDown("Fire1")){var ray = Camera.main.ScreenPointToRay(Input.mousePosition);var hit : RaycastHit;if(Physics.Raycast(ray,hit)){//hit.collider.BroadcastMessage("ApplyDamage",1,SendMessageOptions.DontRequireReceiver);NotificationCenter.DefaultCenter().PostNotification(this,"ApplyDamage");}}}

有了发送通知的事件,还需要接收通知的GameObject,在接受体的脚本中写下以下源码:

#pragma strictvar words:String;function Start(){NotificationCenter.DefaultCenter().AddObserver(this,"ApplyDamage");}function ApplyDamage(){Debug.Log(words);}

这样就能实现任意GameObject之间的通知传输和函数调用了。

当然,NotificationCenterCenter的作用远不止这么简单,它也可以用作数据传输,

查看一下官网的声明:

可见还有一个重载的函数可以传参aData。

使用起来也很方便,将脚本稍作修改即可:

function Start(){NotificationCenter.DefaultCenter().AddObserver(this,"ApplyDamage");}function ApplyDamage(note:Notification){Debug.Log("从"+note.sender+"接收一个信息内容"+note.data+"通知名称为"+note.name);}

运行效果便是:


当然很多时候我们并不仅仅需要将数据打印出来,所以数据的转换也是一个非常关键的步骤。

比如说下面这个案例:


我们要输出剩余的Health值便会发现存在数据类型转换的问题:

此时我们需要进行转换:

这样就解决数据传输中的类型转换问题了。