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. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question I want to execute JavaSctipt code defined outside of Canvas from the Unity side.

Discussion in 'WebGL' started by K_Kuriyama, Jun 2, 2023.

  1. K_Kuriyama

    K_Kuriyama

    Joined:
    Jul 4, 2020
    Posts:
    65
    I want to use Web Speech API, but on Window, MacOS and Android, I succeeded to speak with Web Speech API by writing SpeechSynthesisUtterance in .jslib file. However, only iOS did not output.
    After some tests, I found that SpeechSynthesisUtterance written in HTML JavaScript (non-Emscripten, e.g. main.js) works correctly.

    Is there any way to execute the function in the HTML-side JavaScript (***.js) from the Unity side?
    Also, I would like to know if there is any other way to dynamically read out the speech.
     
  2. K_Kuriyama

    K_Kuriyama

    Joined:
    Jul 4, 2020
    Posts:
    65
    Self resolved.
    Adding the following code to the HTML side seems to enable speech on iOS.



    Code (CSharp):
    1. canvas.addEventListener('touchend', function() {
    2.         // ダミー音声合成を実行
    3.         var speechSynthesis = window.speechSynthesis;
    4.         var utterance = new SpeechSynthesisUtterance('');
    5.         utterance.volume = 0; // 音量を0に設定
    6.         speechSynthesis.speak(utterance);
    7.       });

    I used this as a reference.

    http://fogbugz.unity3d.com/default.asp?1259405_68ph0a2cmn0aupuj
     
  3. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    233
  4. K_Kuriyama

    K_Kuriyama

    Joined:
    Jul 4, 2020
    Posts:
    65
    @unityruba

    Only the usual jslib description is given there, but it was necessary to understand the iOS Safari specifications when using the voice function on iOS.