Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    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:
    44
    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:
    231
    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:
    44
    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:
    44
    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:
    544
    If you want to send the message which serializing with your format, you need to customize the component "WebBrowserInputChannelReceiver" yourself.