Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Is there a way to call Unity function when clicking a button in Html?

Discussion in 'Unity Render Streaming' started by chealin, Jul 22, 2022.

  1. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    76
    hello

    I want to pass a value to the streaming exe file when a button is pressed in Html

    Is there any way ?

    thank you

    upload_2022-7-22_16-41-42.png
     
  2. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    286
    There are somo possible methods, but one possible method is
    You can exchange messages by embedding WebSocket Server in streaming exe and connecting to WebSocket from a browser.
     
  3. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    76
    I found the scene in Samples-Unity RenderStreaming - 3.1.0-exp.3-Example-WebBrowserInput

    In this scene, when a button created on the web is clicked, the corresponding set function in Unity is called.

    However, this process does not pass any parameters.

    I need to pass parameters.

    Is there a way to pass a String parameter?

    upload_2022-7-25_13-33-18.png
     
  4. chealin

    chealin

    Joined:
    Sep 10, 2017
    Posts:
    76
    I am trying to send and receive messages with WebSocket.

    Do I have to send and receive in Json format?

    -Code for sending and receiving messages
    Code (CSharp):
    1. using UnityEngine;
    2. using WebSocketSharp;
    3. public class WsClient : MonoBehaviour
    4. {
    5.     WebSocket ws;
    6.     private void Start()
    7.     {
    8.         Debug.Log("Start");
    9.         string url = "ws://192.168.0.40:80";
    10.  
    11.         ws = new WebSocket(url);
    12.         ws.Connect();
    13.         ws.OnMessage += (sender, e) =>
    14.         {
    15.             Debug.Log("Message Received from " + ((WebSocket)sender).Url + ", Data : " + e.Data);
    16.             Controller.Inst.requestManager.ReceiveData(e.Data);
    17.         };
    18.     }
    19.     private void Update()
    20.     {
    21.         if (ws == null)
    22.         {
    23.             return;
    24.         }
    25.         if (Input.GetKeyDown(KeyCode.Space))
    26.         {
    27.             ws.Send("Hello");
    28.             //ws.Send("SetColor|Bottle|#FFBC42");
    29.         }
    30.  
    31.     }
    32. }
     
    Last edited: Sep 14, 2022
  5. kazuki_unity729

    kazuki_unity729

    Unity Technologies

    Joined:
    Aug 2, 2018
    Posts:
    803
    If you want to send the message which serializing with your format, you need to customize the component "WebBrowserInputChannelReceiver" yourself.