平台 | 支持状态 |
Windows (Standalone) | ✅ 支持 |
Android | ✅ 支持 |
iOS | ✅ 支持 |
Unity Editor | ✅ 支持(使用 Windows DLL) |
Assets/└── Plugins/└── Unity_CLS/├── Unity_CLS.cs # C# 统一接口层├── Android/│ └── libs/│ ├── all-dependencies-1.0.0-shaded.jar #安卓所需java依赖│ ├── network-diagnosis-release.aar #网络探测依赖│ ├── producer-release.aar #日志上报依赖│ └── unity-release.aar #unity交互依赖├── Windows/│ └── cls_network_detect.dll #windows网络探测+日志上报动态库└── iOS/├── UnityApi.h #与ios交互的桥接代码├── UnityApi.m├── TencentCloudLogProducer.framework #cls ios sdk包├── FMDB.framework #cls ios sdk所需的依赖├── Protobuf.framework└── Reachability.framework
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application> 标签中添加明文流量支持(用于 HTTP 探测):<application android:usesCleartextTraffic="true">
using System.Collections.Generic;using UnityEngine;using TencentCloudCLS;public class NetworkDiagnosisDemo : MonoBehaviour{void Start(){// 1. 初始化 SDKCredential credential = new Credential();credential.endpoint = "ap-guangzhou.cls.tencentcs.com";credential.secretId = "您的 SecretId";credential.secretKey = "您的 SecretKey";credential.geoToken = "您的 GeoToken";Unity_CLS.Initialize(credential);// 2. 设置用户自定义扩展字段(可选)Dictionary<string, string> userCustomField = new Dictionary<string, string>();userCustomField.Add("app_version", "1.0.0");userCustomField.Add("user_id", "12345");Unity_CLS.SetUserCustom(userCustomField);// 3. 执行 TCP Ping 探测TcpPingRequest tcpRequest = new TcpPingRequest();tcpRequest.target = "www.baidu.com";tcpRequest.port = 80;tcpRequest.callback = (response) =>{Debug.Log("TCP Ping 结果: " + response);};Unity_CLS.TcpPing(tcpRequest);}}
Unity_CLS.Initialize(Credential credential)
Credential credential = new Credential();credential.endpoint = "ap-guangzhou.cls.tencentcs.com";credential.secretId = "您的 SecretId";credential.secretKey = "您的 SecretKey";credential.geoToken = "您的 GeoToken";Unity_CLS.Initialize(credential);
Unity_CLS.SetUserCustom(Dictionary<string, string> userCustomField)
Dictionary<string, string> userCustomField = new Dictionary<string, string>();userCustomField.Add("app_version", "1.0.0");userCustomField.Add("user_id", "12345");userCustomField.Add("channel", "official");Unity_CLS.SetUserCustom(userCustomField);
Unity_CLS.TcpPing(TcpPingRequest request)
字段 | 类型 | 默认值 | 平台支持 | 说明 |
target | string | "" | 全平台 | 目标主机名或 IP 地址 |
port | int | -1 | 全平台 | 目标端口号 |
count | int | 1 | 全平台 | 探测次数 |
timeout | int | 1000 | 全平台 | 超时时间(毫秒) |
size | int | 0 | Windows/Android | 发送数据包大小(字节),0表示不发送数据 |
payload | string | null | Windows/Android | 发送到目标端口的测试数据内容 |
interval | int | 300 | Windows | 探测间隔时间(毫秒) |
enableVerification | int | 1 | Windows | 是否启用连接验证 1:启用 0:禁用 |
prefer | int | 0 | Windows/iOS | IP 版本偏好 0:IPv4优先 1:IPv6优先 2:IPv4 only 3:IPv6 only |
netId | int | 0 | Windows | 网卡 ID |
traceId | string | null | 全平台 | 跟踪 ID(可为 null,自动生成) |
src | string | null | Windows | 来源标识 |
multiplePortsDetect | bool | true | Android | 是否多端口探测 |
enableMultiplePortsDetect | bool | false | iOS | 是否启用多端口探测 |
detectCustomField | Dictionary | 空字典 | 全平台 | 检测扩展字段 |
callback | callback_response | null | 全平台 | 结果回调函数 |
TcpPingRequest request = new TcpPingRequest();request.target = "www.baidu.com";request.port = 80;request.count = 3;request.timeout = 2000;request.detectCustomField = new Dictionary<string, string>(){{ "scene", "login_page" }};request.callback = (response) =>{Debug.Log("TCP Ping 结果: " + response);};Unity_CLS.TcpPing(request);
Unity_CLS.Ping(PingRequest request)
字段 | 类型 | 默认值 | 平台支持 | 说明 |
target | string | "" | 全平台 | 目标主机名或 IP 地址 |
count | int | 1 | 全平台 | 探测次数 |
timeout | int | 1000 | 全平台 | 超时时间(毫秒) |
packetSize | int | 100 | Windows/Android | 数据包大小(字节,不包含 ICMP 头) |
interval | int | 300 | Windows/iOS | 探测间隔时间(毫秒) |
ttl | int | 64 | Windows | TTL 值 |
prefer | int | 0 | Windows/iOS | IP 版本偏好 |
netId | int | 0 | Windows | 网卡 ID |
traceId | string | null | 全平台 | 跟踪 ID |
src | string | null | Windows | 来源标识 |
multiplePortsDetect | bool | true | Android | 是否多端口探测 |
enableMultiplePortsDetect | bool | false | iOS | 是否启用多端口探测 |
detectCustomField | Dictionary | 空字典 | 全平台 | 检测扩展字段 |
callback | callback_response | null | 全平台 | 结果回调函数 |
PingRequest request = new PingRequest();request.target = "www.baidu.com";request.count = 5;request.timeout = 3000;request.callback = (response) =>{Debug.Log("Ping 结果: " + response);};Unity_CLS.Ping(request);
Unity_CLS.Http(HttpRequest request)
字段 | 类型 | 默认值 | 平台支持 | 说明 |
url | string | "" | 全平台 | 目标 URL(需包含协议头,如 http:// 或 https://) |
count | int | 1 | Android/iOS | 探测次数 |
timeout | int | 1000 | 全平台 | 超时时间(毫秒) |
interval | int | 300 | Windows | 探测间隔时间(毫秒) |
ip | string | null | Android | 指定 IP 地址(为 null 时使用 DNS 解析) |
downloadBytesLimit | int | 0 | Windows | 下载字节限制,0表示不限制 |
downloadHeaderOnly | int | 0 | Windows | 是否仅下载头部 1:仅头部 0:完整内容 |
verifySslCert | int | 0 | Windows/iOS | 是否验证 SSL 证书 1:验证 0:不验证 |
size | int | 0 | Android | 发送数据包大小 |
prefer | int | 0 | Windows/iOS | IP 版本偏好 |
netId | int | 0 | Windows | 网卡 ID |
traceId | string | null | 全平台 | 跟踪 ID |
src | string | null | Windows | 来源标识 |
multiplePortsDetect | bool | true | Android | 是否多端口探测 |
enableMultiplePortsDetect | bool | false | iOS | 是否启用多端口探测 |
detectCustomField | Dictionary | 空字典 | 全平台 | 检测扩展字段 |
callback | callback_response | null | 全平台 | 结果回调函数 |
HttpRequest request = new HttpRequest();request.url = "https://www.baidu.com";request.timeout = 5000;request.callback = (response) =>{Debug.Log("HTTP 探测结果: " + response);};Unity_CLS.Http(request);
Unity_CLS.Dns(DnsRequest request)
字段 | 类型 | 默认值 | 平台支持 | 说明 |
domain | string | "" | 全平台 | 目标域名 |
type | string | null | Windows/Android | DNS 查询类型(如 "A"、"AAAA",null 默认为 A) |
servers | string | null | 全平台 | DNS 服务器列表(逗号分隔,如 "8.8.8.8,8.8.4.4",null 使用系统默认) |
count | int | 1 | Android | 探测次数 |
timeout | int | 1000 | 全平台 | 超时时间(毫秒) |
size | int | 0 | Android | 发送数据包大小 |
prefer | int | 0 | Windows/iOS | IP 版本偏好 |
traceId | string | null | 全平台 | 跟踪 ID |
src | string | null | Windows | 来源标识 |
multiplePortsDetect | bool | true | Android | 是否多端口探测 |
enableMultiplePortsDetect | bool | false | iOS | 是否启用多端口探测 |
detectCustomField | Dictionary | 空字典 | 全平台 | 检测扩展字段 |
callback | callback_response | null | 全平台 | 结果回调函数 |
DnsRequest request = new DnsRequest();request.domain = "www.baidu.com";request.servers = "8.8.8.8";request.callback = (response) =>{Debug.Log("DNS 解析结果: " + response);};Unity_CLS.Dns(request);
Unity_CLS.Mtr(MtrRequest request)
字段 | 类型 | 默认值 | 平台支持 | 说明 |
target | string | "" | 全平台 | 目标主机名或 IP 地址 |
maxTtl | int | 30 | 全平台 | 最大 TTL 值(最大跳数,1 - 255) |
times | int | 10 | 全平台 | 每跳探测次数 |
timeout | int | 1000 | 全平台 | 超时时间(毫秒) |
interval | int | 300 | Windows | 检测间隔时间(毫秒) |
protocol | string | "icmp" | Windows/Android | 协议类型("icmp"、"tcp"、"udp") |
destPort | int | 0 | Windows | 目标端口(TCP/UDP 协议使用) |
maxPaths | int | 1 | Windows/Android | 最大路径数(1 - 10) |
size | int | 0 | Android | 发送数据包大小 |
prefer | int | 0 | Windows/iOS | IP 版本偏好 |
netId | int | 0 | Windows | 网卡 ID |
traceId | string | null | 全平台 | 跟踪 ID |
src | string | null | Windows | 来源标识 |
multiplePortsDetect | bool | true | Android | 是否多端口探测 |
enableMultiplePortsDetect | bool | false | iOS | 是否启用多端口探测 |
detectCustomField | Dictionary | 空字典 | 全平台 | 检测扩展字段 |
callback | callback_response | null | 全平台 | 结果回调函数 |
MtrRequest request = new MtrRequest();request.target = "www.baidu.com";request.maxTtl = 30;request.times = 5;request.protocol = "icmp";request.callback = (response) =>{Debug.Log("MTR 结果: " + response);};Unity_CLS.Mtr(request);
using System;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using TencentCloudCLS;public class NetworkDiagnosisManager : MonoBehaviour{void Start(){Debug.Log("[NetworkDiagnosis] 开始初始化...");}/// <summary>/// 初始化 SDK(需在所有探测操作前调用)/// </summary>public void Init(){Credential credential = new Credential();credential.endpoint = "ap-guangzhou.cls.tencentcs.com";credential.secretId = "您的 SecretId";credential.secretKey = "您的 SecretKey";credential.geoToken = "您的 GeoToken";Unity_CLS.Initialize(credential);// 设置用户自定义扩展字段Dictionary<string, string> userCustomField = new Dictionary<string, string>();userCustomField.Add("app_version", "1.0.0");userCustomField.Add("scene", "main_menu");Unity_CLS.SetUserCustom(userCustomField);Debug.Log("[NetworkDiagnosis] 初始化完成");}/// <summary>/// 执行 TCP Ping 探测/// </summary>public void DoTcpPing(){TcpPingRequest request = new TcpPingRequest();request.target = "www.baidu.com";request.port = 80;request.count = 3;request.detectCustomField = new Dictionary<string, string>(){{ "test_type", "tcp_ping" }};request.callback = (response) =>{Debug.Log("[TCP Ping] 结果: " + response);};Unity_CLS.TcpPing(request);}/// <summary>/// 执行 ICMP Ping 探测/// </summary>public void DoPing(){PingRequest request = new PingRequest();request.target = "www.baidu.com";request.count = 5;request.detectCustomField = new Dictionary<string, string>(){{ "test_type", "icmp_ping" }};request.callback = (response) =>{Debug.Log("[Ping] 结果: " + response);};Unity_CLS.Ping(request);}/// <summary>/// 执行 HTTP 探测/// </summary>public void DoHttp(){HttpRequest request = new HttpRequest();request.url = "http://www.baidu.com";request.timeout = 5000;request.detectCustomField = new Dictionary<string, string>(){{ "test_type", "http" }};request.callback = (response) =>{Debug.Log("[HTTP] 结果: " + response);};Unity_CLS.Http(request);}/// <summary>/// 执行 DNS 解析探测/// </summary>public void DoDns(){DnsRequest request = new DnsRequest();request.domain = "www.baidu.com";request.detectCustomField = new Dictionary<string, string>(){{ "test_type", "dns" }};request.callback = (response) =>{Debug.Log("[DNS] 结果: " + response);};Unity_CLS.Dns(request);}/// <summary>/// 执行 MTR 路由追踪/// </summary>public void DoMtr(){MtrRequest request = new MtrRequest();request.target = "www.baidu.com";request.maxTtl = 30;request.times = 3;request.detectCustomField = new Dictionary<string, string>(){{ "test_type", "mtr" }};request.callback = (response) =>{Debug.Log("[MTR] 结果: " + response);};Unity_CLS.Mtr(request);}}
public delegate void callback_response(string response);
Dictionary<string, string> userCustomField = new Dictionary<string, string>();userCustomField.Add("user_id", "12345");userCustomField.Add("app_version", "2.0.0");Unity_CLS.SetUserCustom(userCustomField);
request.detectCustomField = new Dictionary<string, string>(){{ "scene", "game_loading" },{ "server_region", "ap-shanghai" }};
参数 | Windows | Android | iOS |
interval(探测间隔) | ✅ | ❌ | 部分支持 |
prefer(IP 版本偏好) | ✅ | ❌ | ✅ |
netId(网卡 ID) | ✅ | ❌ | ❌ |
src(来源标识) | ✅ | ❌ | ❌ |
multiplePortsDetect | ❌ | ✅ | ❌ |
enableMultiplePortsDetect | ❌ | ❌ | ✅ |
enableVerification(TCP) | ✅ | ❌ | ❌ |
payload(TCP) | ✅ | ✅ | ❌ |
protocol(MTR) | ✅ | ✅ | ✅ |
平台 | 实现方式 |
Windows / Editor | 通过 DLL(cls_network_detect.dll)P/Invoke 调用 C 原生库 |
Android | 通过 AndroidJavaClass 调用 Java 层 SDK(AAR 包) |
iOS | 通过 DllImport __Internal 调用 Objective-C 桥接层(UnityApi.m → TencentCloudLogProducer.framework) |
文档反馈