diff --git a/Editor/Module Management/ModuleInstaller.cs b/Editor/Module Management/ModuleInstaller.cs index 5b2d5e28..f894ea0b 100644 --- a/Editor/Module Management/ModuleInstaller.cs +++ b/Editor/Module Management/ModuleInstaller.cs @@ -22,8 +22,6 @@ public static class ModuleInstaller private const int THREAD_SLEEP_TIME = 100; private const string PROGRESS_BAR_TITLE = "Ready Player Me"; - private const string GLTFAST_SYMBOL = "GLTFAST"; - private const string READY_PLAYER_ME_SYMBOL = "READY_PLAYER_ME"; private const string GLTFAST_NAME = "com.atteneder.gltfast"; private const string WEBVIEW_NAME = "webview"; @@ -39,6 +37,8 @@ public static class ModuleInstaller static ModuleInstaller() { + Events.registeringPackages -= OnRegisteringPackages; + Events.registeringPackages += OnRegisteringPackages; # if RPM_DEVELOPMENT modulesInstalled = true; #endif @@ -47,16 +47,29 @@ static ModuleInstaller() InstallModules(); EditorApplication.delayCall += DelayCreateCoreSettings; } - + #if !GLTFAST if (IsModuleInstalled(GLTFAST_NAME)) { - AddGltfastSymbol(); - AddScriptingDefineSymbolToAllBuildTargetGroups(READY_PLAYER_ME_SYMBOL); + DefineSymbolHelper.AddSymbols(); } #endif } + + /// + /// Called when a package is about to be added, removed or changed. + /// + /// Describes the PackageInfo entries of packages currently registering. + private static void OnRegisteringPackages(PackageRegistrationEventArgs args) + { + // Core module uninstalled + if (args.removed != null && args.removed.Any(p => p.name == ModuleList.Core.name)) + { + DefineSymbolHelper.RemoveSymbols(); + ProjectPrefs.SetBool(ProjectPrefs.FIRST_TIME_SETUP_DONE, false); + } + } private static void DelayCreateCoreSettings() { @@ -158,39 +171,6 @@ public static PackageInfo[] GetPackageList() return listRequest.Result.ToArray(); } - public static void AddGltfastSymbol() - { - AddScriptingDefineSymbolToAllBuildTargetGroups(GLTFAST_SYMBOL); - } - - private static void AddScriptingDefineSymbolToAllBuildTargetGroups(string defineSymbol) - { - foreach (BuildTarget target in Enum.GetValues(typeof(BuildTarget))) - { - BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target); - - if (group == BuildTargetGroup.Unknown) - { - continue; - } - - List defineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').Select(d => d.Trim()).ToList(); - - if (defineSymbols.Contains(defineSymbol)) continue; - defineSymbols.Add(defineSymbol); - try - { - PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defineSymbols.ToArray())); - } - catch (Exception e) - { - Debug.LogWarning("Could not set RPM " + defineSymbol + " defines for build target: " + target + " group: " + group + " " + e); - } - } - - CompilationPipeline.RequestScriptCompilation(); - } - /// /// Check all modules installed successfully /// diff --git a/Editor/UI/EditorWindows/AvatarLoaderEditor/AvatarLoaderEditor.cs b/Editor/UI/EditorWindows/AvatarLoaderEditor/AvatarLoaderEditor.cs index 864cb291..25a2d483 100644 --- a/Editor/UI/EditorWindows/AvatarLoaderEditor/AvatarLoaderEditor.cs +++ b/Editor/UI/EditorWindows/AvatarLoaderEditor/AvatarLoaderEditor.cs @@ -29,7 +29,7 @@ public class AvatarLoaderEditor : EditorWindow public static void ShowWindow() { #if !GLTFAST - ModuleInstaller.AddGltfastSymbol(); + DefineSymbolHelper.AddSymbols(); #endif var window = GetWindow(); window.titleContent = new GUIContent(AVATAR_LOADER); diff --git a/Editor/Utils/DefineSymbolHelper.cs b/Editor/Utils/DefineSymbolHelper.cs new file mode 100644 index 00000000..9a0bcdd5 --- /dev/null +++ b/Editor/Utils/DefineSymbolHelper.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; +using UnityEditor; +using UnityEditor.Compilation; + +namespace ReadyPlayerMe.Core.Editor +{ + public static class DefineSymbolHelper + { + private const string GLTFAST_SYMBOL = "GLTFAST"; + private const string READY_PLAYER_ME_SYMBOL = "READY_PLAYER_ME"; + + private static void ModifyScriptingDefineSymbolInAllBuildTargetGroups(string defineSymbol, bool addSymbol) + { + foreach (BuildTarget target in Enum.GetValues(typeof(BuildTarget))) + { + BuildTargetGroup group = BuildPipeline.GetBuildTargetGroup(target); + + if (group == BuildTargetGroup.Unknown) + { + continue; + } + + List defineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';').Select(d => d.Trim()).ToList(); + + if (addSymbol && !defineSymbols.Contains(defineSymbol)) + { + defineSymbols.Add(defineSymbol); + } + else if (!addSymbol && defineSymbols.Contains(defineSymbol)) + { + defineSymbols.Remove(defineSymbol); + } + else + { + continue; + } + + try + { + PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defineSymbols.ToArray())); + } + catch (Exception e) + { + var actionWord = addSymbol ? "set" : "remove"; + Debug.LogWarning($"Could not {actionWord} {defineSymbol} defines for build target: {target} group: {group} {e}"); + } + } + + if (addSymbol) + { + CompilationPipeline.RequestScriptCompilation(); + } + } + + public static void AddSymbols() + { + ModifyScriptingDefineSymbolInAllBuildTargetGroups(GLTFAST_SYMBOL, true); + ModifyScriptingDefineSymbolInAllBuildTargetGroups(READY_PLAYER_ME_SYMBOL, true); + CompilationPipeline.RequestScriptCompilation(); + } + + public static void RemoveSymbols() + { + ModifyScriptingDefineSymbolInAllBuildTargetGroups(GLTFAST_SYMBOL, false); + ModifyScriptingDefineSymbolInAllBuildTargetGroups(READY_PLAYER_ME_SYMBOL, false); + } + + } +} diff --git a/Editor/Utils/DefineSymbolHelper.cs.meta b/Editor/Utils/DefineSymbolHelper.cs.meta new file mode 100644 index 00000000..27d0197c --- /dev/null +++ b/Editor/Utils/DefineSymbolHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f7f640bcb9cd21648a78581c77d76723 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: