using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEditor.AddressableAssets; using UnityEngine; public class ChannelBuildTool { private const string ExtraCfgRoot = "../ExtraPluginCfgs/Android"; private const string TysdkPlugins = "Packages/tysdk/Plugins/Android"; private const string TysdkFiles = "Packages/tysdk/Files"; private const string AssetsPlugins = "Assets/Plugins/Android"; private const string AddressablesContentState = "AddressableAssetsData/Android/addressables_content_state.bin"; private const string AndroidAddressablesContentState = "Assets/AddressableAssetsData/Android/addressables_content_state.bin"; private const string DefaultProductName = "Fishing Travel"; private const string EnjoyPayProductName = "Fishing Travel: Hook & Explore"; // ==================== Channel Switching ==================== public static void SwitchChannel(string channel) { channel = NormalizeChannelName(channel); var cfgDir = Path.Combine(ExtraCfgRoot, channel.ToLowerInvariant()); if (!Directory.Exists(cfgDir)) { throw new DirectoryNotFoundException($"[ChannelSwitch] Channel config not found: {cfgDir}"); } Debug.Log($"[ChannelSwitch] Switching to {channel}..."); // 1. Sync root Android config files SyncRootAndroidConfigFiles(cfgDir); // 2. Sync channel SDK adapter Java files to tysdk package SyncChannelAdapterFiles(cfgDir); // 3. Sync google-services.json var srcGoogleServices = Path.Combine(cfgDir, "Files/google-services.json"); if (File.Exists(srcGoogleServices)) { var destDir = Path.Combine(TysdkFiles); if (!Directory.Exists(destDir)) Directory.CreateDirectory(destDir); var destGoogleServices = Path.Combine(destDir, "google-services.json"); File.Copy(srcGoogleServices, destGoogleServices, true); Debug.Log($"[ChannelSwitch] google-services.json -> {destGoogleServices}"); } // 4. Sync channel plugin files (.aar, .jar, etc.) to tysdk package var pluginsSrcDir = Path.Combine(cfgDir, "Plugins/Android"); var pluginsDestDir = TysdkPlugins; if (Directory.Exists(pluginsSrcDir)) { foreach (var oldPlugin in Directory.GetFiles(pluginsDestDir, "tuyoosdk_*.aar")) File.Delete(oldPlugin); foreach (var pluginFile in Directory.GetFiles(pluginsSrcDir)) { if (pluginFile.EndsWith(".meta")) continue; var fileName = Path.GetFileName(pluginFile); var dest = Path.Combine(pluginsDestDir, fileName); File.Copy(pluginFile, dest, true); Debug.Log($"[ChannelSwitch] Plugin: {pluginFile} -> {dest}"); } } // 5. Set Bundle Identifier per channel var bundleId = channel switch { "Flexion" => "com.arkgame.ft.flexion", "EnjoyPay" => "com.arkgame.ft.ep", _ => "com.arkgame.ft" }; PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, bundleId); Debug.Log($"[ChannelSwitch] Bundle Identifier -> {bundleId}"); var productName = channel == "EnjoyPay" ? EnjoyPayProductName : DefaultProductName; PlayerSettings.productName = productName; Debug.Log($"[ChannelSwitch] Product Name -> {productName}"); // 6. Set runtime channel tysdk.ChannelConfig.SetChannel(channel); // 7. Set Addressables profile and optional channel content state SetAddressablesProfile(channel); SyncAddressablesContentState(channel, cfgDir); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); Debug.Log($"[ChannelSwitch] Done. Channel: {channel}"); } // ==================== Menu ==================== [MenuItem("Tools/Channel/Switch Channel/GooglePlay", false, 10000)] public static void Switch_GooglePlay() => SwitchChannel("GooglePlay"); [MenuItem("Tools/Channel/Switch Channel/Flexion", false, 10001)] public static void Switch_Flexion() => SwitchChannel("Flexion"); [MenuItem("Tools/Channel/Switch Channel/Rustore", false, 10002)] public static void Switch_Rustore() => SwitchChannel("Rustore"); [MenuItem("Tools/Channel/Switch Channel/EnjoyPay", false, 10003)] public static void Switch_EnjoyPay() => SwitchChannel("EnjoyPay"); // ==================== Build ==================== private static void BuildAndroidForChannel(string channel, bool debug) { try { SwitchChannel(channel); var fileName = debug ? $"tysdkTest_{channel}_debug.apk" : $"tysdkTest_{channel}.apk"; var outPath = $"../Bin/{fileName}"; var buildOptions = debug ? BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.WaitForPlayerConnection : BuildOptions.None; var buildPlayerOptions = new BuildPlayerOptions { scenes = EditorBuildSettings.scenes.Select(x => x.path).ToArray(), locationPathName = outPath, target = BuildTarget.Android, options = buildOptions, }; Debug.Log($"[ChannelBuild] Building {channel} -> {outPath}"); var buildReport = BuildPipeline.BuildPlayer(buildPlayerOptions); if (buildReport?.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded) { Debug.Log($"[ChannelBuild] Success: {Path.GetFullPath(outPath)}"); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); } else { Debug.LogError($"[ChannelBuild] Failed: {buildReport?.summary}"); } } finally { SwitchChannel("GooglePlay"); AssetDatabase.SaveAssets(); } } [MenuItem("Tools/Channel/Build Android (Channel)/GooglePlay", false, 10100)] public static void Build_GooglePlay() => BuildAndroidForChannel("GooglePlay", false); [MenuItem("Tools/Channel/Build Android (Channel)/GooglePlay Debug", false, 10101)] public static void Build_GooglePlay_Debug() => BuildAndroidForChannel("GooglePlay", true); [MenuItem("Tools/Channel/Build Android (Channel)/Rustore", false, 10102)] public static void Build_Rustore() => BuildAndroidForChannel("Rustore", false); [MenuItem("Tools/Channel/Build Android (Channel)/Rustore Debug", false, 10103)] public static void Build_Rustore_Debug() => BuildAndroidForChannel("Rustore", true); [MenuItem("Tools/Channel/Build Android (Channel)/Flexion", false, 10104)] public static void Build_Flexion() => BuildAndroidForChannel("Flexion", false); [MenuItem("Tools/Channel/Build Android (Channel)/Flexion Debug", false, 10105)] public static void Build_Flexion_Debug() => BuildAndroidForChannel("Flexion", true); [MenuItem("Tools/Channel/Build Android (Channel)/EnjoyPay", false, 10106)] public static void Build_EnjoyPay() => BuildAndroidForChannel("EnjoyPay", false); [MenuItem("Tools/Channel/Build Android (Channel)/EnjoyPay Debug", false, 10107)] public static void Build_EnjoyPay_Debug() => BuildAndroidForChannel("EnjoyPay", true); private static string NormalizeChannelName(string channel) { if (string.IsNullOrEmpty(channel)) return channel; switch (channel.Trim().ToLowerInvariant()) { case "googleplay": return "GooglePlay"; case "flexion": return "Flexion"; case "enjoypay": return "EnjoyPay"; case "rustore": return "Rustore"; default: return channel.Trim(); } } private static void SyncRootAndroidConfigFiles(string cfgDir) { CopyRootConfigFiles(cfgDir, "*.gradle"); CopyRootConfigFiles(cfgDir, "*.properties"); CopyRootConfigFile(cfgDir, "AndroidManifest.xml"); } private static void SyncChannelAdapterFiles(string cfgDir) { // ChannelAdapterManager, ChannelAdapterBase, ConfigManager and SDKManager // are fixed platform files and are not copied per channel. foreach (var oldJava in Directory.GetFiles(TysdkPlugins, "*Adapter.java")) { File.Delete(oldJava); } foreach (var javaFile in Directory.GetFiles(cfgDir, "*Adapter.java")) { var fileName = Path.GetFileName(javaFile); var dest = Path.Combine(TysdkPlugins, fileName); File.Copy(javaFile, dest, true); Debug.Log($"[ChannelSwitch] Adapter: {javaFile} -> {dest}"); } } private static void CopyRootConfigFiles(string cfgDir, string searchPattern) { foreach (var src in Directory.GetFiles(cfgDir, searchPattern, SearchOption.TopDirectoryOnly)) { CopyRootConfigFile(src); } } private static void CopyRootConfigFile(string cfgDir, string fileName) { var src = Path.Combine(cfgDir, fileName); if (File.Exists(src)) { CopyRootConfigFile(src); } } private static void CopyRootConfigFile(string src) { var fileName = Path.GetFileName(src); var dest = Path.Combine(AssetsPlugins, fileName); File.Copy(src, dest, true); Debug.Log($"[ChannelSwitch] Config: {src} -> {dest}"); } private static void SetAddressablesProfile(string channel) { var settings = AddressableAssetSettingsDefaultObject.Settings; if (settings == null) { Debug.LogWarning("[ChannelSwitch] Addressables settings not found."); return; } var profileName = UsesRustoreAddressablesProfile(channel) ? "arksgameru" : "arksgamecos"; var profileId = settings.profileSettings.GetProfileId(profileName); if (string.IsNullOrEmpty(profileId)) { Debug.LogError($"[ChannelSwitch] Addressables profile not found: {profileName}"); return; } settings.activeProfileId = profileId; var remoteLoadPath = settings.RemoteCatalogLoadPath.GetValue(settings); var remoteBuildPath = settings.RemoteCatalogBuildPath.GetValue(settings); Debug.Log($"[ChannelSwitch] Addressables profile -> {profileName}, profileId={profileId}, Remote.LoadPath={remoteLoadPath}, Remote.BuildPath={remoteBuildPath}"); } private static void SyncAddressablesContentState(string channel, string cfgDir) { var src = Path.Combine(cfgDir, AddressablesContentState); if (!File.Exists(src) && UsesRustoreAddressablesContentState(channel)) { Debug.Log($"[ChannelSwitch] Channel content state not found, try legacy Rustore content state path. Channel={channel}, checked={src}"); src = Path.Combine("../ExtraPluginCfgs", AddressablesContentState); } if (!File.Exists(src)) { Debug.Log($"[ChannelSwitch] Addressables content state not copied. Channel={channel}, checked={src}"); return; } var destDir = Path.GetDirectoryName(AndroidAddressablesContentState); if (!Directory.Exists(destDir)) Directory.CreateDirectory(destDir); File.Copy(src, AndroidAddressablesContentState, true); Debug.Log($"[ChannelSwitch] Addressables content state: {src} -> {AndroidAddressablesContentState}"); } private static bool UsesRustoreAddressablesContentState(string channel) { return channel == "Rustore" || channel == "EnjoyPay"; } private static bool UsesRustoreAddressablesProfile(string channel) { return channel == "Rustore" || channel == "EnjoyPay"; } }