Audiokinetic's Community Q&A is the forum where users can ask and answer questions within the Wwise and Strata communities. If you would like to get an answer from Audiokinetic's Technical support team, make sure you use the Support Tickets page.

How do I set a Wwise-type switch by name from a Unity script?

0 votes

This is a question sent to Audiokinetic on email and posted here to benefit everyone. 

To set a Wwise-type Switch you need to make a Wwise-Type property and assign it from Unity's Inspector, like this: 
public AK.Wwise.Switch MySwitch;
MySwitch.SetValue(gameObject);

Alternatively, you can set a Switch by name from code by using: 
AkSoundEngine.SetSwitch();But what if you'd like to have a Wwise-type Switch that can be set by name from script?

asked Jun 7, 2022 in General Discussion by Mads Maretty S. (Audiokinetic) (38,280 points)

1 Answer

0 votes
 
Best answer

As Wwise-type Switches need a property for each Switch element, it's not super convenient to redirect your code based on the name (string), and you will most like have to modify the code as you add more Switches.
But here's one way to do it with a list so you can add more Switches from the editor without having to revisit your code.

public class MySwitchScript : MonoBehaviour
{
  
// List of Wwise-type Switches assigned in Unity's Inspector.
  public List<AK.Wwise.Switch> MySwitches = new List<AK.Wwise.Switch>();
  private void Awake()
  {
    // Finds the Switch with the name "Dirt" and sets the Switch on this game object.
    MySwitches.Find(x => x.ToString() == "Dirt").SetValue(gameObject);
  }
}

And to make it even more flexible for the rest of your game code, you can make the Awake() function into a custom function. This way, your other scripts (e.g. footstep script) can simply call this function with the name of the surface the player is standing on and if the surface name corresponds to a Switch in your list, the correct Switch will be found and set automatically. 

public class MySwitchScript : MonoBehaviour
{
  // List of Wwise-type Switches assigned in Unity's Inspector.
  public List<AK.Wwise.Switch> MySwitches = new List<AK.Wwise.Switch>();
  private void SetSwitchByName(string nameOfSwitch)
  {
    // Finds the Switch with the text (ToString) name ("Dirt) and sets the Switch on this game object.
    MySwitches.Find(x => x.ToString() == nameOfSwitch).SetValue(gameObject);
  }
}

answered Jun 7, 2022 by Mads Maretty S. (Audiokinetic) (38,280 points)
selected Jun 7, 2022 by Mads Maretty S. (Audiokinetic)
...