先修复一下,错误的场景 删除不必要的钓场资产 修复into场景 update:更新meta文件,修复报错 update:修复资源 修复第一章节建造 修复建造第三章 update:删除多余内容 update:更新README.md update:还原图标 update:更新README update:更新配置
121 lines
3.8 KiB
C#
121 lines
3.8 KiB
C#
// using AppsFlyerSDK;
|
||
using System;
|
||
using UnityEngine;
|
||
|
||
/// <summary>
|
||
/// 渠道类型枚举 - 区分谷歌商店/俄罗斯渠道
|
||
/// </summary>
|
||
[Flags]
|
||
public enum ChannelType
|
||
{
|
||
/// <summary>
|
||
/// 未初始化(默认值,需在游戏启动时设置)
|
||
/// </summary>
|
||
Unknown = 0,
|
||
/// <summary>
|
||
/// 谷歌商店渠道
|
||
/// </summary>
|
||
GooglePlay = 1,
|
||
/// <summary>
|
||
/// 俄罗斯渠道(如Yandex Games/本地商店等)
|
||
/// </summary>
|
||
Rustore = 1 << 1,
|
||
}
|
||
|
||
/// <summary>
|
||
/// 全局渠道管理类 - 单例模式+静态字段,全局唯一访问
|
||
/// </summary>
|
||
public static class ChannelManager
|
||
{
|
||
// 全局静态渠道字段 - 私有存储,通过公共方法访问/设置
|
||
private static ChannelType _currentChannel = ChannelType.Unknown;
|
||
|
||
/// <summary>
|
||
/// 获取当前渠道(全局只读访问)
|
||
/// </summary>
|
||
public static ChannelType CurrentChannel
|
||
{
|
||
get
|
||
{
|
||
Debug.Log($"CurrentChannel => {_currentChannel}");
|
||
return _currentChannel;
|
||
}
|
||
}
|
||
#if UNITY_EDITOR
|
||
public static void SetChannel()
|
||
{
|
||
_currentChannel = ChannelType.GooglePlay;
|
||
}
|
||
#elif UNITY_ANDROID
|
||
public static void SetChannel()
|
||
{
|
||
string channelStr = ReadMetaData();
|
||
if (string.IsNullOrEmpty(channelStr))
|
||
{
|
||
_currentChannel = ChannelType.GooglePlay;
|
||
return;
|
||
}
|
||
bool isSuccess = Enum.TryParse<ChannelType>(channelStr, ignoreCase: true, out ChannelType resultChannel);
|
||
if (isSuccess)
|
||
{
|
||
_currentChannel = resultChannel;
|
||
Debug.Log($"[ChannelManager] 渠道初始化成功,当前渠道:{_currentChannel}");
|
||
}
|
||
else
|
||
{
|
||
_currentChannel = ChannelType.GooglePlay;
|
||
Debug.LogError($"[ChannelManager] 渠道初始化失败,渠道:{channelStr}");
|
||
}
|
||
}
|
||
static string ReadMetaData()
|
||
{
|
||
string metaName = "CHANNEL";
|
||
try
|
||
{
|
||
// 1. 获取Unity的当前Activity实例(Android原生上下文)
|
||
using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
|
||
{
|
||
using (AndroidJavaObject currentActivity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
|
||
{
|
||
// 创建MetaDataReader实例
|
||
AndroidJavaObject metaDataReader = new AndroidJavaObject("com.unity.plugin.MetaDataReader", currentActivity);
|
||
if (metaDataReader != null)
|
||
{
|
||
Debug.Log("[ChannelManager] MetaDataReader Get");
|
||
string value = metaDataReader.Call<string>("getAppMetaDataString", metaName);
|
||
Debug.Log($"[ChannelManager] MetaDataReader Get Value == {value}");
|
||
return value;
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"[ChannelManager] MetaDataReader == null");
|
||
return string.Empty;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (System.Exception e)
|
||
{
|
||
Debug.LogError("读取meta-data失败:" + e.Message + "\n" + e.StackTrace);
|
||
return string.Empty;
|
||
}
|
||
}
|
||
|
||
#else
|
||
public static void SetChannel()
|
||
{
|
||
_currentChannel = ChannelType.Unknown;
|
||
}
|
||
#endif
|
||
/// <summary>
|
||
/// 快速判断是否为谷歌商店渠道(语法糖,简化业务代码判断)
|
||
/// </summary>
|
||
public static bool IsGooglePlay => _currentChannel == ChannelType.GooglePlay;
|
||
|
||
/// <summary>
|
||
/// 快速判断是否为俄罗斯渠道(语法糖,简化业务代码判断)
|
||
/// </summary>
|
||
public static bool IsRustoreStore => _currentChannel == ChannelType.Rustore;
|
||
|
||
}
|