Skip to content

Game.Modding.Toolchain.Dependencies.UnityDependency

Assembly: Assembly-CSharp
Namespace: Game.Modding.Toolchain.Dependencies

Type: class

Base: BaseDependency

Summary: Manages the Unity Editor dependency used by the toolchain. This class exposes detection (registry and file-system), download checks, installer size probing, disk-space requirements, and install/uninstall flows for a fixed Unity installer URL/version. It wraps platform-specific logic (Windows registry lookup for installed Unity), handles background CLI execution of the Unity installer/uninstaller, and integrates with the toolchain's dependency lifecycle (IsInstalled, NeedDownload, Install, Uninstall, etc.). It is intended for use by the modding/toolchain UI and installer logic to automate Unity setup for modders.


Fields

  • public const string kUnityInstallerUrl This constant stores the direct download URL used by the toolchain for the Unity installer (2022.3.62f2 in this version).

  • public static readonly string sUnityVersion Holds the Unity version string pulled from Unity's runtime (Application.unityVersion). Used to locate registry keys and for display strings.

  • public static readonly string kDefaultInstallationDirectory Default base install directory (Program Files folder) used when creating an installation path.

  • public static readonly string kInstallationFolder Folder name appended to the chosen installationDirectory ("Unity").

  • private static string sUnityPath Cached path to a discovered Unity installation (internal use). Nullable — used when computing uninstaller path.

  • private long? m_DownloadSize Cached size of the installer download (nullable). Populated by GetUnityInstallerSize to avoid repeated network queries.

  • private string m_InstallationDirectory Backing field for the override installationDirectory property. Stores the full path (with kInstallationFolder appended).

Properties

  • public override string name { get; } Returns "Unity editor". Display name for the dependency in the UI.

  • public override string icon { get; } Returns the icon resource path "Media/Toolchain/Unity.svg" used in the UI.

  • public override string version { get; protected set } Exposes the Unity version (sUnityVersion). The setter is protected but is effectively read-only here.

  • public static string unityPath { get; } Attempts to determine an installed Unity root folder by querying Windows registry keys for the installed Unity version. It tries multiple registry locations (CurrentUser / LocalMachine and WOW6432Node uninstall entries), extracts an install/uninstall path, traverses parent directories as needed, and returns the path if it exists. Returns null if not found.

  • public static string unityExe { get; } If unityPath is found, returns the full path to "%unityPath%/Editor/Unity.exe". Otherwise null.

  • public static string unityUninstallerExe { get; } If the cached sUnityPath is set, returns "%sUnityPath%/Editor/Uninstall.exe". Otherwise null. Used to invoke a silent uninstall when available.

  • public string installerPath { get; } Resolved path in the toolchain's download folder for the Unity installer file name derived from kUnityInstallerUrl. Uses SharedSettings.instance.modding.downloadDirectory to build the full local path.

  • public override string installationDirectory { get; set } Override that stores the chosen base installation directory. The setter appends kInstallationFolder and normalizes with Path.GetFullPath.

  • public override bool canChangeInstallationDirectory { get; } Returns true — the UI may allow changing the installation directory.

  • public override bool confirmUninstallation { get; } Returns true — the UI should request confirmation before uninstall.

  • public override LocalizedString installDescr { get; } A localized description string used by the UI to warn the user about installing Unity. It injects UNITY_VERSION and HOST tokens for display.

  • public override LocalizedString uninstallMessage { get; } A localized message used by the UI to warn about uninstalling Unity (injects UNITY_VERSION).

Constructors

  • public UnityDependency() Initializes sUnityPath to null and sets installationDirectory to the default Program Files directory. Prepares the dependency for use by the toolchain.

Methods

  • public override Task<bool> IsInstalled(CancellationToken token) Checks whether Unity is considered installed by verifying unityExe is not null and the file exists (LongFile.Exists). Returns a completed Task. Query is synchronous but returned as Task.

  • public override Task<bool> IsUpToDate(CancellationToken token) Currently implements the same check as IsInstalled (no separate update checking for this Unity installer). Returns IsInstalled(token).

  • public override async Task<bool> NeedDownload(CancellationToken token) Determines whether the installer needs to be downloaded:

  • If the installer file doesn't exist locally, returns true.
  • Otherwise compares the local file length to the size obtained from GetUnityInstallerSize; if lengths differ, deletes the local file and returns true.
  • Otherwise returns false.

  • private async Task<long> GetUnityInstallerSize(CancellationToken token) Fetches (and caches in m_DownloadSize) the remote file size for kUnityInstallerUrl by calling IToolchainDependency.GetDownloadSizeAsync. Returns the cached value if available.

  • public override async Task<List<IToolchainDependency.DiskSpaceRequirements>> GetRequiredDiskSpace(CancellationToken token) Computes disk-space requirements:

  • If Unity is not installed, adds a requirement for installationDirectory with a hardcoded 6 GiB (6442450944L) estimate.
  • If the installer needs downloading, also adds a requirement for the installerPath sized to the installer download size. Returns a list of DiskSpaceRequirements for the caller to evaluate.

  • public override Task Download(CancellationToken token) Invokes BaseDependency.Download with the fixed URL and installerPath and "DownloadingUnity" as a localized progress key. Returns the download Task.

  • public override async Task Install(CancellationToken token) Performs a silent installation by executing the installer via CliWrap with "/S" argument (silent). Updates the dependency state to Installing and logs output and errors. Any thrown exceptions are wrapped as ToolchainException(ToolchainError.Install, ...) except for cancellation and ToolchainException which are rethrown. Finally, deletes the installer file.

  • public override async Task Uninstall(CancellationToken token) Performs uninstallation:

  • Sets state to Removing and attempts to invoke unityUninstallerExe with silent arguments and /D=installFolder to specify path.
  • If a registry-discovered unityPath exists, waits for the directory to be removed (AsyncUtils.WaitForAction checking LongDirectory.Exists).
  • Exceptions are wrapped similarly to Install (ToolchainError.Uninstall) except for cancellation and ToolchainException.

  • private static bool TryGetRegistryKeyValue(RegistryKey registry, string path, string key, out string value) Utility to open a registry key and attempt to read a value. Returns true if found. Logs errors and returns false on exceptions. Used by unityPath lookup.

  • private static bool TryGetParentPath(string path, int depth, out string parentPath) Given a path string, walks up 'depth' parent directories and verifies the resulting directory exists. If so, returns true and sets parentPath. Used to normalize paths extracted from registry values that may include executable arguments or nested paths.

Usage Example

// Example: Check/install Unity via the toolchain dependency
using System.Threading;

var dep = new Game.Modding.Toolchain.Dependencies.UnityDependency();
using var cts = new CancellationTokenSource();

// Check whether Unity is present
bool installed = await dep.IsInstalled(cts.Token);
if (!installed)
{
    // Determine if download is needed
    if (await dep.NeedDownload(cts.Token))
    {
        // Download installer (progress handled by BaseDependency infrastructure)
        await dep.Download(cts.Token);
    }

    // Install (will run the installer silently)
    await dep.Install(cts.Token);
}

// To uninstall (prompting/confirmation handled by UI)
await dep.Uninstall(cts.Token);