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 have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Failing to register AnalyzeRule

Discussion in 'Addressables' started by alexander-verbeek-10chambers, Sep 28, 2022.

  1. alexander-verbeek-10chambers

    alexander-verbeek-10chambers

    Joined:
    Apr 8, 2021
    Posts:
    2
    I have been attempting to write an AnalyzeRule that allows us to check for duplicate dependencies and automatically fix them by moving them to a group of our choice according to our own logic. My rule registered and functioned as expected for a while, but now I am encountering an issue where it fails to register and appear in the Analyze window.

    My rule class inherits from CheckBundleDupeDependencies and in some cases an object of that class (the default one included with addressables package) already is present in the list when registering when it fails. I have two projects. In one of them my rule successfully registers, while in the other project, it fails to register. Presumably due to some order of initialization issue and pre-existing items in the registered rules list. In both projects, my code is located in an editor directory.

    The issue appears to be partially caused by the loop and if-statement inside AnalyzeSystem.RegisterNewRule which prevent any TRule inheriting from any rule already in the list from being added to the list:
    Code (CSharp):
    1.         public static void RegisterNewRule<TRule>() where TRule : AnalyzeRule, new()
    2.         {
    3.             foreach (var rule in Rules)
    4.             {
    5.                 if (rule.GetType().IsAssignableFrom(typeof(TRule)))
    6.                     return;
    7.             }
    8.             Rules.Add(new TRule());
    9.         }
    Why is this check in there? Shouldn't that expression look more like
    (rule.GetType() == typeof(TRule))
    if you want to prevent the same rule from being added more than once?
    Is inheriting from CheckBundleDupeDependencies to re-use that dependency checking functionality a bad idea? It would be hard for me to write that myself though. And the information at the bottom of the manual page seems to recommend using these base classes: https://docs.unity3d.com/Packages/com.unity.addressables@1.20/manual/AnalyzeTool.html

    Just to be thorough, I'll also attach the .cs file containing the rule class I've cobbled together.

    I should also note that if possible I would prefer to avoid having to inherit from CheckBundleDupeDependencies. I would prefer to have dependency checking functionality available in some sort of static utility class.

    Having to make a asset bundle build merely to check asset dependencies also seems a bit overkill to me.
     

    Attached Files:

  2. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    354
    This issue has been fixed in Addressables 1.20.0:
    The new code is:
    Code (CSharp):
    1. public static void RegisterNewRule<TRule>() where TRule : AnalyzeRule, new()
    2. {
    3.     foreach (var rule in Rules)
    4.     {
    5.         if (rule.GetType().Equals(typeof(TRule)))
    6.             return;
    7.     }
    8.     Rules.Add(new TRule());
    9. }
     
  3. alexander-verbeek-10chambers

    alexander-verbeek-10chambers

    Joined:
    Apr 8, 2021
    Posts:
    2
    Thank you. This project is on addressables 1.19.19 currently. Looks like we'll need to do some project upgrading to a newer Unity editor version in the near future to benefit from that version of addressables.

    For anyone else who might be looking at this now or in the future and who is either unwilling or unable to upgrade their project, I have found a little workaround: You do not need to register your analyze rule. You can also write an editor window with a button, or a menu entry with a method that constructs an instance of your AnalyzeRule and calls RefreshAnalysis and FixIssues.

    Something like this:

    Code (CSharp):
    1. AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
    2. DuplicateDependencyRule duplicateDependencyRule = new DuplicateDependencyRule();
    3. duplicateDependencyRule.RefreshAnalysis(settings);
    4. duplicateDependencyRule.FixIssues(settings);
     
    Last edited: Oct 5, 2022