Search Unity

LocalizeAudioClipBehaviour Scripting syntax help needed

Discussion in 'Localization Tools' started by the_Whizzkid, May 22, 2020.

  1. the_Whizzkid

    the_Whizzkid

    Joined:
    Apr 15, 2020
    Posts:
    20
    Unity 2019.3. I'm a beginner.
    Can't find any example or help on the syntax to change the "Localized Asset Reference" (TabAssets/...) in a "Localize Audio Clip" Compontent from a script.
    (I made it to change the localisation of a text per Script, I guessed the Audio is similar.)
    Set Up:
    Asset Table is filled. I have an audio Source, added a Localize Audio Clip component to it. Wrote a script, attatched it to the Audio Source.
    Now I need to pass one of my Keys of my Asset Table as a string to that component to play the localized audio.
    My very short script:

    using UnityEngine;
    using UnityEngine.Localization.Components;

    public class AudioSubTitle : MonoBehaviour
    {
    public LocalizeAudioClipBehaviour subTitleAudio;

    public void ShowText(string audioToPlay)
    {
    // here ist where i have no clue what to enter...
    subTitleAudio. ? = audioToPlay;
    }
    }
    Any help is appreciated...
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
  3. the_Whizzkid

    the_Whizzkid

    Joined:
    Apr 15, 2020
    Posts:
    20
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
  5. the_Whizzkid

    the_Whizzkid

    Joined:
    Apr 15, 2020
    Posts:
    20
    That link works, but I'm getting this error when saving this:
    "subTitleAudio.AssetReference = audioToPlay;"
    error CS0029: Cannot implicitly convert type 'string' to 'UnityEngine.Localization.LocalizedAudioClip'
    Maybe i didn't desribe this clear enough or I did a mistake.
    I don't want to pass an Audioclip, but just the Key ("Mom_Nerv_1") i have in the Asset Tables like in the example below.

    (Like it works with Localize String with this: "public LocalizeStringBehaviour subTitel;" and "subTitel.StringReference.TableEntryReference = textToShow;" I just need to pass the key to the "String Reference", "Entry Name" and don't need to worry about the rest.)

    Here the Compontent attachted to the AudioSource:
    upload_2020-5-23_16-24-45.png

    Here are the keys in my Asset Table (Right now only the German ones do have an audio file linked):
    upload_2020-5-23_16-18-47.png
     

    Attached Files:

  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    There should be a TableEntryReference property you can assign the entry name to. It won't auto update when the value changes though. That's something we have added in the next release.

    I'm replying on my phone so I don't have source code in front of me at the moment.
     
    the_Whizzkid likes this.
  7. the_Whizzkid

    the_Whizzkid

    Joined:
    Apr 15, 2020
    Posts:
    20
    Works! :)
    Thanks for the fast answers!
    You mean that the auto Update is not working for clips already playing? Because it plays the other audio clip i'm assigning. (The last clip finnished playing before assigning the new one).

    In case someone else needs it, here is my code:
    ...
    using UnityEngine.Localization.Components;

    public class AudioSubTitle : MonoBehaviour
    {
    public LocalizeAudioClipBehaviour subTitleAudio;

    public void PlayKey(string audioToPlay)
    {
    subTitleAudio.AssetReference.TableEntryReference = audioToPlay;
    GetComponent<AudioSource>().Play();
    }
    }
     
    karl_jones likes this.