Unity-Released WebGL uses SendMessage to pass multiple parameters

Source: Internet
Author: User

If you want to achieve unity and browser data interaction in general will be in two ways

Method One:

Application.externalcall ("SayHello", "HelloWorld");

This way you can call the JS function in the Earl puzzle in unity.

Method Two:

SendMessage ("Main Camera", "functionname", "parameters");

This way you can use the JS code in the Web page to pass parameters to unity, call the corresponding function

But I used in the process of the law idle a problem is that SendMessage only accept a parameter, which is very embarrassing, in case we reserved the function needs more than one parameter, this question puzzled me for several days

No matter how you try to pass parameters, this error will occur.

I've tried this.

SendMessage ("Main Camera", "Setwendu", 33,22);

And there's this.

SendMessage ("Main Camera", "Setwendu", "33" + "22");

But it all comes down to this mistake.

Until I saw a foreign blog one day.

Respect the copyright I pasted the original link

http://www.feedingedge.co.uk/blog/2011/03/09/browser-to-unity3d-communication-and-back-again/

I found out that unity's sendmessage was supposed to support only one parameter, no wonder I never succeeded.

A custom delimiter is used in the blog to deliver multiple parameters

In Unity:

void inboundfunction(string indata)
{

string[] Words = indata. Split (' ~ ');

Data1 = Words[0];
MOREDATA2 = words[1];
Anotherpiece3 = words[2];

}

JS in:

<script type= "Text/javascript" >
<!–
function Callbackunity (ARG)
{
Alert (ARG);
var unity = Unityobject.getobjectbyid ("Unityplayer");
Unity. SendMessage ("Rezzer", "Inboundfunction", "item1~item2~item3");
}
–>
</script>

This method combines multiple parameters into a single parameter in C # using split bar parameters to be separated so that the transfer of multiple parameters can be implemented skillfully

Unity-Released WebGL uses SendMessage to pass multiple parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.