unity中消息传递函数例子

//消息传递函数例子

//GameObject.SendMessage:向自身的脚本中发送消息

//GameObject.BroadcastMessage:向自身及物体的脚本中发送消息

//GameObject.SendMessageUpwards:向自身及父级物体中发送消息

#pragma strict

function Start(){

   gameObject.SendMessageUpwards(“ApplyDamage”,5.0);

}

function Update(){

}

function ApplyDamage(damage:float){

     print(“sphere ‘s:damage”+damage);

}

////////////

#pragma strict

function Start(){

}

function Update(){

}

function ApplyDamage(damage:float){

    print(“Cube ‘s:damage”+damage);

}

/////////////////////////////

Using UnityEngine;

public class EventDispatcher:MonBehaviour{

     public delegate void EventHandler(Gameobject e);

     public event EventHandler MouseOver;

     void onMouseOver(){

          mouseOver(this.gameobject);

     }

     void Start (){

      }

       void Update (){

        }

}

////////////////////

using UnityEngine;

using System.Collections;

public class Listen : MonBehavior{

     void Start(){

           EventDispatcher ev =Gameobject.Find(“Cube”).GetComponent<EventDispatcher>();

          ev.MouseOver += ListeningFunction;

     }

     void ListeningFunction (Gameobject e){

          this .transform.Rotate(20,0,0);

          Debug.Log(“mouse is over : ”+e);

     }     

      void Update(){

       }

}

 

猜你喜欢

转载自blog.csdn.net/qq_15119013065/article/details/81356049