Search Unity

Addressables Play Mode editor scripting

Discussion in 'Addressables' started by MoctezumaDev, Aug 2, 2019.

  1. MoctezumaDev

    MoctezumaDev

    Joined:
    Aug 14, 2013
    Posts:
    53
    Hi

    Is there a way to change the Play Mode through an editor script? I'm trying to come with a method that would be called from the command line to build my assets and I want to make sure that my script is building the right content.

    I also want to provide to my fellow team mates with a faster way to switch between play modes in the editor without to navigate all this menus. It would be nit if the package had some kind of multiple state toggle button in the Unity3d toolbar to switch between the different modes.

    I found a way to create my custom buttons for now but I need to first find a way to change the play mode script by code.
     
  2. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    The way play modes works is this (honestly, we don't love it. it worked well with our initial design for builders, but our reality has drifted)...
    we have one list of build scripts in the AddressableAssetSettings object.

    /// <summary>
    /// List of ScriptableObjects that implement the IDataBuilder interface. These are used to create data for editor play mode and for player builds.
    /// </summary>
    public List<ScriptableObject> DataBuilders { get { return m_DataBuilders; } }


    Each builder implements CanBuildData<T>. For play mode, the T is AddressablesPlayModeBuildResult, for building for player, T is AddressablesPlayerBuildResult. As you'll notice, this does mean a builder could build for both modes, if you happened to create a script to handle that. Alternatively, it could build for neither, if you were extending addressables in some way that needed that.

    The way Addressables saves the "current" script is as an index into this array, in the members ActivePlayModeDataBuilder and ActivePlayerDataBuilderIndex.

    So, for you to set the current builder from code, you'd have to loop the array of builders, find the one you want (by name, or based on selection in a custom dropdown you've written), and use that index.

    Again, we don't love this pattern. Some of our early motivations for this pattern still stand, but some do not. (such as we still expect most medium to large studios to create many custom build scripts to suit their needs, thus the dynamic nature)

    One additional note, the package is open source, so a lot of these questions are most easily answered just by looking at our code. For example AddressableAssetsSettingsGroupEditor.TopToolbar() implements our dropdown logic.
     
  3. MoctezumaDev

    MoctezumaDev

    Joined:
    Aug 14, 2013
    Posts:
    53
    @unity_bill hi, thanks for the quick reply, I'll try to implement it and see how it goes.

    All this time I have been thinking this would be a black box and I would have to figure a lot of things by asking it here.

    I really like that it will be open source that would help a lot to understand and troubleshoot some of the features. Could someone point me to the right repository? I've been following the New Input System but I didn't find the Addressable repository neither in GitHub nor in Bitbucket Unity repository.
     
  4. MoctezumaDev

    MoctezumaDev

    Joined:
    Aug 14, 2013
    Posts:
    53
  5. MoctezumaDev

    MoctezumaDev

    Joined:
    Aug 14, 2013
    Posts:
    53
    Just for anyone looking for a quick answer to this same issue:

    Code (CSharp):
    1.  
    2. static void SetDataBuilder(string name) {
    3.         AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
    4.         for (int i = 0; i < settings.DataBuilders.Count; i++)
    5.         {
    6.             var dataBuilder = settings.GetDataBuilder(i);
    7.             if (name == dataBuilder.Name)
    8.             {
    9.                 AddressableAssetSettingsDefaultObject.Settings.ActivePlayModeDataBuilderIndex = i;
    10.                 return;
    11.             }
    12.         }
    13. }
    14.  
     
  6. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    It's something we're looking into. The internally hosted git setup we have comes with a lot of nice features (not to mention, the company is already paying for it), so moving to a public github has some cost & feature implications, as well as the overhead of making our CI work on it. All that to say, we are interested in the idea but haven't locked in a plan yet.