Unity sendmessage发送多个参数


SendMessage是一个比较强大的功能,我来告诉大家如何传递多个参数。
首先创建一个接受信息的脚本,并赋给一个物体Obj,
代码如下:

using UnityEngine;  
using System.Collections;  
  
public class DJH_String : MonoBehaviour {  
    void PrintMessage(object[] obj)  
    {  
        Debug.Log(obj[0]+""+obj[1]);  
    }  
}  

为什么参数是object呢,因为SendMessage传递的参数本身就是object类型的,有兴趣的朋友可以去了解一下。

接下来,来传递信息,代码如下:

using UnityEngine;  
using System.Collections;  
  
public class DJH_SendMessage : MonoBehaviour {  
    private string msgValue1 = "Jeremy";  
    private string msgValue2 = "小代";  
    public GameObject obj;  
    void Start () {  
        object[] message = new object[2];  
        message[0] = msgValue1;  
        message[1] = msgValue2;  
        obj.SendMessage("PrintMessage", message, SendMessageOptions.DontRequireReceiver);  
    }  
}  

运行结果如下


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM