Forum rules - please read before posting.

Addressables - Loading Dependencies

Unity: 2019.4.18
AC: 1.74.2
Addressables system is installed and updated for AC use.


Not quite an AC related issue, but it might help others using the system.

After opening a Scene using Addressables + AC, I would like to load the scenes dependencies. Nothing fancy, just load everything/all Adressables associated with that scene using a custom script. Has anyone figured this out yet? I found a script online, but I'm getting an error.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement;
using UnityEngine.SceneManagement;

public class LoadAdressableScene : MonoBehaviour
{

    public AssetReference AssetToLoad;
    // Use this for initialization
    void Start()
    {
        //AssetToLoad.LoadAsset<scene>().Completed += OnAssetReferenceLoaded;
        Addressables.PreloadDependencies(AssetToLoad, null);
        Addressables.LoadScene(AssetToLoad).Completed += OnCompleted;
    }

    private void OnCompleted(AsyncOperationHandle<Scene> obj)
    {
        Debug.Log("Scene Load Complete");
    }

    // Update is called once per frame
    void Update () {

    }
}

(21,30): error CS0246: The type or namespace name 'AsyncOperationHandle<>' could not be found (are you missing a using directive or an assembly reference?)

Not a scripter, but from what I understand, AsyncOperationHandle is referenced in the: Packages -> Addressables -> Runtime -> ResourceManager -> ResourceManager script. The namespace is UnityEngine.ResourceManagement .

Is this script similar to what anyone else is using, or is it outdated?

Comments

  • Here's one I found from the Unity Youtube Channel. Doesn't seem to do what I need though. It loads a Scene, a Prefab and a Sprite.

    using UnityEngine;
    using UnityEngine.UI;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    
    public class AddressablesManager : MonoBehaviour
    {
        [SerializeField] Image image;
        [SerializeField] AssetReference assetReference;
        [SerializeField] AssetReferenceGameObject assetReferenceGameObject;
        [SerializeField] AssetReferenceSprite assetReferenceSprite;
    
        public void AddressablesPrefab()
        {
            Addressables.InstantiateAsync(assetReferenceGameObject);
    
        }
    
        public void AddressablesScene()
        {
            assetReference.LoadSceneAsync(UnityEngine.SceneManagement.LoadSceneMode.Additive);
        }
    
        public void AddressableSprite()
        {
            assetReferenceSprite.LoadAssetAsync().Completed += OnSpriteLoaded;
        }
    
    
        void OnSpriteLoaded(AsyncOperationHandle<Sprite> handle)
        {
            image.sprite = handle.Result;
        }
    }
    
  • Just to clarify, AC works fine when using the normal Unity build system, but when switching to the Addressable System, things get crazy. I'm pretty sure it's an Addressable dependency issue.

    I have one build in the build settings, which transfers to the Start/Menu scene (An Addressable Bundle). While hovering/clicking the buttons, they work, but they are no longer bound to the audio mixer/AC volume control. They're max volume. But, when I open the options (Unity Ui) Canvas, and hover over the buttons in that canvas, the audio works just fine.

    When I switch scenes, the music system breaks, even when I return to the title screen, the music stays off. SFX (Other) audio works, but is also max volume. Speech audio doesn't seem to work. When I load a saved game, the scene doesn't load from the beginning.

    Action Lists work as intended, and timelines. Seems to be mostly an audio problem on my end.

  • You're using Audio Mixer Groups for volume control?

    It may be a case of having to make the Mixers addressables as well. Unity may have to create copies of certain assets referenced by addressables if they aren't also addressable.

    The script in your first post will switch the scene in addition to loading dependencies. Unless that's your intent, you can otherwise rely on AC's Scene: Switch Action as normal - since this will also use Addressables.LoadScene if you supply an addressable scene key.

  • I changed an actionlist which fixed the Loading issue I was having. Everything seems to load fine now. I'm actually using AC's Scene: Switch Action to load each Scene. Seems to be working properly. I was searching for Addressable scripts because I wasn't sure if everything was loading properly when opening a scene. Maybe I won't need the scripts after-all.

    Looks like the audio is still an issue though. I am using an Audio Mixer to control the volume.
    https://adventurecreator.org/tutorials/working-audio-mixers

    I also added 2 more audio sliders using the Variables/Menu system. (Total of 5 audio sources: Speech, Ambience, Music, System and SFX). All work/save on reset in a normal build setting. But, they stop working properly when calling Scenes through the Addressable System.

    To test, I swapped: Audio settings -> Volume controlled by: 'Audio Mixer Groups' back to 'Audio Sources', and it solved the issue (minus the 2 modified audio sources). It seems my issue might be the Audio Mixer Groups setting when building with the Addressable System. Is this happening on your end? Maybe I need to tweak something in my project.

  • Also, the Audio Mixer is Addressable. My first build, I added all dependencies to the Addressable System. I tried adding them to Resources and to the actual Build Settings too. Didn't seem to take. The mixers are attached to prefab audio source objects. I'll keep testing.

  • Mixer groups shouldn't normally be referenced by scenes - AC will assign them from the Settings Manager directly.

    You may need to make the Settings Manager addressable, and/or your UI prefabs, if your menus rely on them.

  • For the most part, this is an update. Still trying to figure this out.

    From what I've read, this Audio Mixer issue has been around for a while with other Unity users. If an Audio Mixer is called in the Build Settings and as an Addressable, it creates a duplicate Mixer, just like it would any other object. But, with a Mixer, the duplicates confuse the player, causing audio problems. It seems to help when users remove any trace of the Mixer from the Build Settings, and add it only as an Addressable. I'm trying to find a workaround with the AC References file.


    I created a blank scene detached from Adventure Creator + any other asset. I added this script to an empty game object to open the first Addressable Scene. Simplest Addressable scene loader I've found so far, outside of AC.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    using UnityEngine.SceneManagement;
    
    public class SceneLoader : MonoBehaviour
    {
        public AssetReference scene;
        // Start is called before the first frame update
        void Start()
        {
          Addressables.LoadSceneAsync(scene,UnityEngine.SceneManagement.LoadSceneMode.Single).Completed += SceneLoadComplete; 
        }
    
        private void SceneLoadComplete(AsyncOperationHandle<UnityEngine.ResourceManagement.ResourceProviders.SceneInstance> obj)
        {
            if (obj.Status == AsyncOperationStatus.Succeeded)
            {
                // Set our reference to the AsyncOperationHandle (see next section)
                Debug.Log(obj.Result.Scene.name + " successfully loaded.");
                // (optional) do more stuff
            }
        }
    }
    

    I have 1 Audio Mixer in my entire project. I moved all Setting Managers/Ui/Anything using the Audio Mixer out of Resources, into Addressables. Assets -> Adventure Creator -> Resources were also moved out of Resources, into Addressables.

    When I build the game, the start up (Build Settings) scene transitions to the next Scene (Addressable), but nothing in the scene loads. It seems the 'References' file is required to be in Resources for the scene transition to work properly. But, when I move the References file back into Resources, it calls the 'AC Manager' files, one of which is the Settings Manager, which calls the Audio Mixer Groups.

    At the moment, I'm wondering if there's a way for me to load the References file as an Addressable. Currently, the scene turns black. I can see the cursor icon, so something is working there.

    I found another script which is supposed to find and default the original Mixer, getting some errors though. Not quite sure what I need to tweak here, but maybe that's all it would take, just default the default Mixer after the Player is loaded.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AddressableAssets;
    using UnityEngine.ResourceManagement.AsyncOperations;
    using UnityEngine.SceneManagement;
    
    public static class SceneAudioMixerInitializer 
    {
      public static void FixMixerOnAllAudioSourcesInScene() {
        // Get all of the scene's audio sources
        AudioSource[] audioSources = Object.FindObjectsOfType<AudioSource>();
    
        // Loop through them
        foreach (AudioSource audioSource in audioSources)
          FixMixerOnAudioSource(audioSource);
      }
    
      private static void FixMixerOnAudioSource(AudioSource audioSource) {
        // Ignore audio sources without a mixer group
        if (audioSource.outputAudioMixerGroup) {
          // Extract the ghost group's name
          AudioMixerGroup outputAudioMixerGroup = audioSource.outputAudioMixerGroup;
          string groupName = outputAudioMixerGroup.name;
          string mixerName = outputAudioMixerGroup.audioMixer.name;
    
          // Use group and mixer names to find the real group among the bundled ones
          audioSource.outputAudioMixerGroup =
            AssetsManager.AudioMixers[mixerName].FindMatchingGroups(groupName)[0];
        }
      }
    }
    

    cs(23,7): error CS0246: The type or namespace name 'AudioMixerGroup' could not be found (are you missing a using directive or an assembly reference?)
    cs(29,9): error CS0103: The name 'AssetsManager' does not exist in the current context
    https://forum.unity.com/threads/audio-mixer-and-asset-bundle.338077/

  • If a script involves mixer groups, you need to make use of Unity's Audio namespace:

    using UnityEngine.Audio;
    
  • Modified fix for the audio issue.

    Tags: Audio Mixer Groups, Addressables
    Reference: https://adventurecreator.org/tutorials/working-audio-mixers


    If you're having issues adjusting your audio volume (while using audio mixers) at runtime after building your project, this is how the issue was solved on my side.

    1) Create a new canvas prefab. Add your sliders to the new canvas.

    2) Add the script (bottom of the post) to the slider. -> In the script, add your Audio Mixer volume parameter (Tutorials online to set up parameters) to "Your Audio Mixer Parameter Goes Here". -> Drag Audio Mixer into script 'Mixer' slot on slider. -> Change the Min volume of the slider to = .0001 / Max volume to = 1. -> Add event to 'On Value Changed (Single) -> Drag slider into slot and call the script using the first 'SetLevel'. Don't use SetLevel (float).

    3) Create an Adventure Creator variable. -> Type: Float -> Set initial value -> Link to: Options Data

    4) Create a new Adventure Creator Menu -> Unity UI Prefab or Scene -> Check ON: Ignore Input (so it doesn't block hotspots) -> Check ON: Enabled on start? (It should be on at all times so it affects ALL assets/canvas') -> Add Slider dropdown to the Menu -> Drag the Canvas and Slider you created into the Menu. -> Slider affects: Float Variable -> Global float var: Add the volume variable you created. -> Min. value = .0001 / Max. value = 1.

    5) Test volume, make sure they all move together. -> Hide the new Sliders however you like. I simply dragged them down out of view.


    (( Notes / Tips:))

    Make sure you're using .0001 for Min volume to avoid bugs when using the script.

    I currently have two volume control Menus: 1 in my Settings Menu and another using the description above. The volume is connected through the variable, not just AC's settings. The setting saves when you reload the game.

    When you close the Settings Menu or transition from it, the volume controls may not connect with the rest of your assets. By using the above Canvas Menu, you'll have an adjustable Volume Control Menu in every Scene, for every asset.

    The Volume controls will be connected by both the variable and AC. Even though AC sets up Speech, Ambience SFX and Music, I also added the slider script/variable to the Menu. Settings Menu - (Slider -> Slider affects: Speech) + ANOTHER slider in the same Menu - (Slider -> Slider affects: Float Variable -> Global float var: Speech). 2 Sliders affecting Speech Volume in different ways, yet they are connected.

    Any new Audio Mixer Group (Not Speech, Ambience SFX or Music) only requires 1 slider in the AC Menu.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Audio;
    
    public class SpeechVolume : MonoBehaviour
    
    {
    
        public AudioMixer mixer;
    
        public void SetLevel (float sliderValue)
    
        {
            mixer.SetFloat ("Your Audio Mixer Parameter Goes Here", Mathf.Log10 (sliderValue) * 20 );
        }
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.