SendMessage效率不高,因爲每次調用的時候都會去遍歷檢測自身或者子節點上要調用的方法。
一、方法
GameObject自身的Script
SendMessage("函數名", 參數,SendMessageOptions)
SendMessageUpwards("函數名", 參數,SendMessageOptions)
二、第三個參數使用
SendMessageOptions.RequireReceiver
using UnityEngine;
using System.Collections;
public class TestSendMassage : MonoBehaviour {
void Start () {
//this.gameObject.BroadcastMessage("sendMsg", SendMessageOptions.DontRequireReceiver);
//this.gameObject.SendMessageUpwards("sendMsg", SendMessageOptions.DontRequireReceiver);
this.gameObject.SendMessage("sendMsg", SendMessageOptions.DontRequireReceiver);
}
// Update is called once per frame
void sendMsg() {
Debug.Log("sendMsg");
}
}
using UnityEngine;
using System.Collections;
//該腳本依次掛載在TestSendMassage腳本所掛節點的父節點或者子節點上
public class TestMsg : MonoBehaviour {
void sendMsg()
{
Debug.Log("sendMsg1");
}
}