Search Unity

Trigger installer script on load despite other assemblies throwing errors?

Discussion in 'Scripting' started by QFSW, Apr 25, 2020.

  1. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    I'm trying to make an installer script for my plugin that will give a prompt to the user to install the missing dependencies if detected

    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. namespace QFSW.BA.External
    4. {
    5.     public static class PackageInstaller
    6.     {
    7.         private static readonly IPackage[] _packages = new IPackage[]
    8.         {
    9.             new UnityPackage("Newtonsoft.Json", "NewtonsoftJson", "Json.NET")
    10.         };
    11.  
    12.         [InitializeOnLoadMethod]
    13.         private static void InstallMissingPackages()
    14.         {
    15.             foreach (IPackage package in _packages)
    16.             {
    17.                 if (!package.IsInstalled())
    18.                 {
    19.                     DisplayInstallDialog(package);
    20.                 }
    21.             }
    22.         }
    23.  
    24.         private static bool DisplayInstallDialog(IPackage package)
    25.         {
    26.             string message = $"Build Automator 2 requires the library {package.PackageName} which cannot be found. Would you like to install {package.PackageName}?";
    27.             bool installAllowed = EditorUtility.DisplayDialog($"Missing {package.PackageName}", message, "Install", "Cancel");
    28.             if (installAllowed)
    29.             {
    30.                 package.Install();
    31.             }
    32.  
    33.             return installAllowed;
    34.         }
    35.     }
    36. }
    37.  

    This would work, however it seems the script never runs (or even compiles). I have it in it's own independent assembly, yet when another assembly is not compiling my script never runs

    What does happen is that my script will trigger when booting up unity, but not if the package is imported whilst Unity is running

    Any ideas on what I could change about either my script or my asmdef/code structure to solve this?
     
  2. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Put it all into a precompiled DLL to get around potential compilation order issues, still no luck :(

    It it completely impossible? If so what would be a better way to approach the fundamental issue?
     
  3. momo_the_monster

    momo_the_monster

    Joined:
    Sep 3, 2014
    Posts:
    16
    @QFSW - I'm running into this same issue, did you ever find a workaround?
     
  4. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    I'm afraid not
     
  5. momo_the_monster

    momo_the_monster

    Joined:
    Sep 3, 2014
    Posts:
    16
    @QFSW - I've got a solution that's working reliably, but it's not pretty - basically, move your importer to its own UPM package so it's installed to the Packages folder, then include the rest of your content in a .unitypackage in the packages folder which gets installed by your script after you've imported your dependencies.
     
  6. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    If you have the main package as a .unitypackage I don't think there's any need to use UPM
     
  7. Lars-Blaabjerg

    Lars-Blaabjerg

    Joined:
    Apr 5, 2011
    Posts:
    54
    I have the same issue :-(
    I want the install script to copy required dlls to the Asset folder if they are not already present. Obviously, the fact that they are not present causes compile errors elsewhere in the package.