Hi
How could I send three parameters to a function into Game from Webpage?
gameInstance.SendMessage('myCube', 'myFunc', 'vroad', 0, 0, 0);
Thank You
Hi
How could I send three parameters to a function into Game from Webpage?
gameInstance.SendMessage('myCube', 'myFunc', 'vroad', 0, 0, 0);
Thank You
I’m not really sure what you’re asking for. But the SendMessage method takes an Object parameter, so you could do this:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Execute(string[] items) {
Debug.Log(items[0]); // myCube
Debug.Log(items[1]); // myFunc
Debug.Log(items[2]); // vroad
}
void Example() {
string[] parameters = new string[] { "myCube", "myFunc", "vroad" };
gameObject.SendMessage("ApplyDamage", parameters);
}
}
Just as a heads-up, adding more tags to a thread, especially ones that are not relevant, won’t magically result in more people providing answers. You’ll just end up annoying the moderator who will have to remove the irrelevant ones.
So, I have a javascript code into my website and I need to send 4 strings together into the C# function.
My Javascript Function
<html>
<head>
<title></title>
</head>
<body>
function jsFunc()
{
gameInstance.SendMessage('myCube', 'callMyC#Function', 'myStringOne', 'myStringTwo', 'myStringThree', 'myStringFour',);
}
</script>
</body>
</html>
My C# Function into Unity Game
void callMyC#Function(string wordOne, string wordTwo, string wordThree, string wordFour)
{
//to do something here
}
Thank You
Do you want to make a server, or do you want every user to have their own game running together with a webbrowser?
Wrap them into a single parameter. Then on the Unity side unwrap them.
JSON is a typical format used for this purpose.
Edit: I’ve realized that WebGL already offers this feature, but using only one paremeter.
Like @Kiwasi said, you should simply convert the multiple objects to a single one.
You could use json, alternatively for simplicity you could also seperate each variable using comma’s.
If you want a complete solution here are the steps:
I’ve put together an application that runs a http server, in order to work using javascript.
Follow these steps in order to setup the server on runtime:
In order to run code in unity change the bolded text in the url, and send a HTTP GET Request: http://localhost:80/target=myCube&method=callMyCSharpFunction¶ms=myStringOne,myStringTwo,myStringThree,myStringFour
Heres an example of a remote executable script:
using UnityEngine;
public class CubeScript : MonoBehaviour {
void callMyCSharpFunction(string[] args) {
Debug.Log(string.Join(", ", args));
}
}
![]()
Javascript example code:
function httpGetAsync(theUrl, callback) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
var url = "http://localhost:80/target=myCube&method=callMyCSharpFunction&"
+ "params=myStringOne,myStringTwo,myStringThree,myStringFour";
httpGetAsync(url, (resp) => {
if (resp == 'true') {
console.log("Executed code on UnityServer.cs!");
} else {
console.log("Error occured on UnityServer.cs!");
}
});
Scripts:
Sources:
@Magiichan
Hello i just found that i can’t send more than one parameter to a Json Function so i did array of string to send to the function parameter
object[] tempStorage = new object[2];
tempStorage[0] = ExpName;
tempStorage[1] = ExpName + pdfName;
Application.ExternalCall("OpenPDFUIButton", tempStorage);
in json file how to be able to separate those 2 values to get the value i want
function OpenPDFUIButton (expNameAndFileName)
{
var PName = “http://21.32.56/Virtual/Files/“+expName+”/”+expNameAndFileName;
}
as you can see i need to get expname and expnameAndFile name separately How can i achieve that ? , thanks .