添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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);
              

So, I have a javascript code into my website and I need to send 4 strings together into the C# function.

My Javascript Function

<title></title> </head> 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

BatmanDeveloper:

So, I have a javascript code into my website and I need to send 4 strings together into the C# function.

My Javascript Function

<title></title> </head> 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?

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:

  • Create an empty GameObject
  • Add UnityMainThreadDispatcher.cs Component.
  • Add UnityServer.cs Component.
  • Run in unity and the server will be running.
  • Load the localhost url and it will try to find the gameobject and the function in order to execute it.
    Note that all the parameters will be passed through as a string array. (string)
  • 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&params=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:

  • http://puu.sh/xi1jr.zip
  • Sources:

  • Simple HTTP Server in C# - CodeProject
  • GitHub - PimDeWitte/UnityMainThreadDispatcher: A simple, thread-safe way of executing actions (Such as UI manipulations) on the Unity Main Thread
  • My Brain
  • @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 .