广播
在一个Cubes上挂载inttro_BoradCastMessate脚本
public class Inttro_BroadCastMessate : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnMouseDown() { print("123"); GameObject.Find("Cubes").BroadcastMessage("ChangeMyColor", SendMessageOptions.DontRequireReceiver);//调用Cubes的子物体或者父级物体的上脚本中的ChangeMyColor方法 } }
在Cubes的子物体上挂载ChangeColor
public class ChangeColor : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void ChangeMyColor() { gameObject.renderer.material.color = Color.red; } }
结果点击Cubes则其子物体Cube则改变颜色
时间: 2024-10-03 22:57:25