|
7 | 7 | using UnityEngine; |
8 | 8 |
|
9 | 9 | /// <summary> |
10 | | -/// 获取渠道名称 |
11 | | -/// Android: |
12 | | -/// 需要在主启动的Activity 中添加 |
| 10 | +/// 渠道信息获取工具类 |
| 11 | +/// </summary> |
| 12 | +/// <remarks> |
| 13 | +/// 用于在Unity应用中获取当前运行平台的渠道信息。 |
| 14 | +/// |
| 15 | +/// <para><b>Android 平台配置:</b></para> |
| 16 | +/// 需要在主启动的 Activity 中添加以下 meta-data 标签: |
| 17 | +/// <code><![CDATA[ |
13 | 18 | /// <meta-data android:name="channel" android:value="android_cn_taptap" /> |
| 19 | +/// ]]></code> |
14 | 20 | /// |
15 | | -/// iOS : |
16 | | -/// 需要在Info.plist中添加 |
17 | | -/// Key ==> value String 类型 |
18 | | -/// channel ==> ios_cn_xxx |
19 | | -/// |
20 | | -/// </summary> |
| 21 | +/// <para><b>iOS 平台配置:</b></para> |
| 22 | +/// 需要在 Info.plist 中添加以下键值对(String 类型): |
| 23 | +/// <code><![CDATA[ |
| 24 | +/// <key>channel</key> |
| 25 | +/// <string>ios_cn_xxx</string> |
| 26 | +/// ]]></code> |
| 27 | +/// |
| 28 | +/// <para><b>Editor/PC 平台配置:</b></para> |
| 29 | +/// 在 StreamingAssets 文件夹下创建 channel.txt 文件,格式如下: |
| 30 | +/// <code><![CDATA[ |
| 31 | +/// channel=editor_cn_test |
| 32 | +/// ]]></code> |
| 33 | +/// </remarks> |
21 | 34 | public sealed class BlankGetChannel |
22 | 35 | { |
23 | 36 | #if UNITY_IOS |
| 37 | + /// <summary> |
| 38 | + /// iOS 平台原生方法:从 Info.plist 中获取渠道名称 |
| 39 | + /// </summary> |
| 40 | + /// <param name="channelKey">渠道键名,默认为 "channel"</param> |
| 41 | + /// <returns>渠道名称</returns> |
24 | 42 | [DllImport("__Internal")] |
25 | 43 | private static extern string getChannelName(string channelKey); |
26 | 44 |
|
27 | 45 | #endif |
| 46 | + /// <summary> |
| 47 | + /// 渠道信息缓存字典,用于避免重复读取配置 |
| 48 | + /// </summary> |
28 | 49 | private static readonly Dictionary<string, string> ChannelCache = new Dictionary<string, string>(); |
29 | 50 |
|
30 | 51 | /// <summary> |
31 | | - /// 获取渠道值 |
| 52 | + /// 获取指定渠道键对应的渠道名称 |
32 | 53 | /// </summary> |
33 | | - public static string GetChannelName(string channelKey = "channel") |
| 54 | + /// <remarks> |
| 55 | + /// 该方法会根据当前运行平台从相应的配置源读取渠道信息,并使用缓存避免重复读取。 |
| 56 | + /// <list type="table"> |
| 57 | + /// <listheader> |
| 58 | + /// <term>平台</term> |
| 59 | + /// <description>配置源</description> |
| 60 | + /// </listheader> |
| 61 | + /// <item> |
| 62 | + /// <term>Android</term> |
| 63 | + /// <description>AndroidManifest.xml 中的 meta-data</description> |
| 64 | + /// </item> |
| 65 | + /// <item> |
| 66 | + /// <term>iOS</term> |
| 67 | + /// <description>Info.plist 中的键值对</description> |
| 68 | + /// </item> |
| 69 | + /// <item> |
| 70 | + /// <term>Editor/PC</term> |
| 71 | + /// <description>StreamingAssets/channel.txt 文件</description> |
| 72 | + /// </item> |
| 73 | + /// </list> |
| 74 | + /// </remarks> |
| 75 | + /// <param name="channelKey">渠道键名,默认为 "channel"</param> |
| 76 | + /// <param name="defaultValue">当未找到渠道配置时返回的默认值,默认为 "default"</param> |
| 77 | + /// <returns>渠道名称,如果未配置则返回 <paramref name="defaultValue"/></returns> |
| 78 | + /// <example> |
| 79 | + /// 以下示例展示如何获取渠道名称: |
| 80 | + /// <code><![CDATA[ |
| 81 | + /// // 使用默认参数获取主渠道 |
| 82 | + /// string channel = BlankGetChannel.GetChannelName(); |
| 83 | + /// |
| 84 | + /// // 获取指定键的渠道 |
| 85 | + /// string subChannel = BlankGetChannel.GetChannelName("sub_channel", "unknown"); |
| 86 | + /// ]]></code> |
| 87 | + /// </example> |
| 88 | + public static string GetChannelName(string channelKey = "channel", string defaultValue = "default") |
34 | 89 | { |
35 | 90 | if (ChannelCache.TryGetValue(channelKey, out var value)) |
36 | 91 | { |
37 | 92 | return value; |
38 | 93 | } |
39 | 94 |
|
40 | | - string channelName = "default"; |
| 95 | + string channelName = defaultValue; |
41 | 96 | #if UNITY_STANDALONE || UNITY_EDITOR |
42 | 97 | string path = Application.streamingAssetsPath + "/channel.txt"; |
43 | 98 | if (File.Exists(path)) |
|
0 commit comments