Commit 8a58d58a authored by gaorui's avatar gaorui
Browse files

feat(app): Merge LogViewer.cs

parents 4925bb19 d95ddb07
{
"name": "HoolaiDemo",
"rootNamespace": "HoolaiDemo",
"references": [
"HoolaiSDK",
"Unity.TextMeshPro"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
fileFormatVersion: 2
guid: 4ad29b91a96fd4dd0a7c1c680da6e981
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEngine.Scripting;
using UnityEngine.UI;
[Preserve]
public class ExitConfirmationDialog : MonoBehaviour
{
public GameObject dialog; // Reference to the dialog panel
......
......@@ -2,13 +2,16 @@ using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Scripting;
[Preserve]
public class GameManager : MonoBehaviour
{
public ExitConfirmationDialog exitDialog; // 在 Inspector 中设置
private bool isExiting = false; // 防止重复调用退出
[Preserve]
private void Awake()
{
// 触发单例构造,内部会调用 PlatformCallback.SetListener(this)
......@@ -18,6 +21,7 @@ public class GameManager : MonoBehaviour
//Android 需监听返回事件,如果当前场景没有游戏业务处理了(比如拦截返回上一个场景,此时无需调用,但返回到顶级场景后继续返回需处理此逻辑)需调用 SDK 的退出接口
[Preserve]
void Update()
{
if (Input.GetKey(KeyCode.Escape))
......@@ -26,6 +30,8 @@ public class GameManager : MonoBehaviour
HandleExit();
}
}
[Preserve]
private void HandleExit()
{
// 防止重复调用
......@@ -39,11 +45,13 @@ public class GameManager : MonoBehaviour
Invoke("ResetExitFlag", 0.5f);
}
[Preserve]
private void ResetExitFlag()
{
isExiting = false;
}
[Preserve]
public void OnLoginBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked login");
......@@ -51,12 +59,14 @@ public class GameManager : MonoBehaviour
HoolaiSdkManager.Login();
}
[Preserve]
public void OnLogoutBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked logout");
HoolaiSdkManager.Logout();
}
[Preserve]
public void OnEnterServerBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked EnterServer");
......@@ -67,7 +77,7 @@ public class GameManager : MonoBehaviour
playerInfo.ZoneId = "1";
playerInfo.ZoneName = "傲视九重天";
playerInfo.ServerId = "1";
playerInfo.ServerName = "牛逼格拉斯";
playerInfo.ServerName = "傲视九重天";
playerInfo.Balance = "9999";
playerInfo.Vip = "99";
playerInfo.PartyName = "少林寺";
......@@ -78,11 +88,11 @@ public class GameManager : MonoBehaviour
playerInfo.Phylum = "100";
playerInfo.ClassField = "滴滴滴答";
//Game Extra sample Data(optional)
playerInfo.AddExtra("intKey", 55);
playerInfo.AddExtra("longLongKey", 66786658787676);
playerInfo.AddExtra("stringKey", "Hello111");
HoolaiSdkManager.SendEvent(EventType.EnterServer, playerInfo);
NativeLogger.I("Game_Unity_Log", "clicked EnterServer");
HoolaiSdkManager.SendEvent(EventType.CreateRole, playerInfo);
......@@ -94,6 +104,7 @@ public class GameManager : MonoBehaviour
}
[Preserve]
public void OnPayBtnClicked1()
{
NativeLogger.I("Game_Unity_Log", "clicked Pay");
......@@ -117,6 +128,7 @@ public class GameManager : MonoBehaviour
}
[Preserve]
public void OnPayBtnClicked6()
{
NativeLogger.I("Game_Unity_Log", "clicked Pay");
......@@ -139,6 +151,7 @@ public class GameManager : MonoBehaviour
HoolaiSdkManager.StartPay(payParams);
}
[Preserve]
public void OnPayBtnClicked68()
{
NativeLogger.I("Game_Unity_Log", "clicked Pay");
......@@ -161,6 +174,7 @@ public class GameManager : MonoBehaviour
HoolaiSdkManager.StartPay(payParams);
}
[Preserve]
public void OnPayBtnClicked128()
{
NativeLogger.I("Game_Unity_Log", "clicked Pay");
......@@ -183,12 +197,14 @@ public class GameManager : MonoBehaviour
HoolaiSdkManager.StartPay(payParams);
}
[Preserve]
public void OnExitBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked Exit");
HoolaiSdkManager.ExitGame();
}
[Preserve]
public void OnGetProductInfoBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked QueryGoodsInfo");
......@@ -197,6 +213,7 @@ public class GameManager : MonoBehaviour
// 添加方法用于根据索引获取单个商品信息
[Preserve]
public static GoodsInfo GetGoodsInfoByIndex(List<GoodsInfo> goodsList, int index)
{
if (goodsList != null &&
......@@ -210,6 +227,7 @@ public class GameManager : MonoBehaviour
// 新增方法:使用指定索引的商品数据进行支付
[Preserve]
public void OnPayWithSpecificGoodsBtnClicked(int goodsIndex)
{
NativeLogger.I("Game_Unity_Log", "clicked Pay With Random Goods");
......@@ -250,6 +268,7 @@ public class GameManager : MonoBehaviour
// 添加方法用于将GoodsInfo转换为PayParams
[Preserve]
public PayParams ConvertGoodsInfoToPayParams(GoodsInfo goodsInfo, int count = 1)
{
if (goodsInfo == null) return null;
......@@ -270,6 +289,7 @@ public class GameManager : MonoBehaviour
return payParams;
}
[Preserve]
public void OnCDKBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked CDKey");
......@@ -277,18 +297,21 @@ public class GameManager : MonoBehaviour
}
[Preserve]
public void OnShareBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked shareData");
HoolaiSdkManager.ShareData(1, 1, "test");
}
[Preserve]
public void OnOpenServiceBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked openService");
HoolaiSdkManager.OpenService();
}
[Preserve]
public void OnOpenAccountBtnClicked()
{
NativeLogger.I("Game_Unity_Log", "clicked openAccount");
......
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scripting;
[Preserve]
public class HoolaiListener : IPlatformCallbackListener
{
private static HoolaiListener _instance;
......@@ -25,52 +28,60 @@ public class HoolaiListener : IPlatformCallbackListener
private HoolaiListener()
{
// 自动设置监听器
PlatformCallback.SetListener(this);
PlatformCallback.SetListener(this);;
}
// 初始化退出标准警告
[Preserve]
public void SetExitConfirmationDialog(ExitConfirmationDialog dialog)
{
exitDialog = dialog; // 保存对话框实例
}
[Preserve]
public void OnInitSuccess(InitResult result)
{
NativeLogger.I("Game_Unity_Callback", $"OnInitSuccess GameId:{result.gameId} channel:{result.channel} channelId:{result.channelId}");
}
[Preserve]
public void OnInitFailed(string reason)
{
{;
NativeLogger.I("Game_Unity_Callback", $"OnInitFailed reason:{reason}");
}
[Preserve]
public void OnLoginFailed(string reason)
{
NativeLogger.I("Game_Unity_Callback", $"OnLoginFailed reason:{reason}");
}
[Preserve]
public void OnLoginSuccess(LoginResult result)
{
NativeLogger.I("Game_Unity_Callback", $"OnLoginSuccess uid:{result.uid} channel:{result.channel} channelId:{result.accessToken}");
}
[Preserve]
public void OnRefreshUser(LoginResult result)
{
NativeLogger.I("Game_Unity_Callback", $"OnRefreshUser uid:{result.uid} channel:{result.channel} channelId:{result.accessToken}");
}
[Preserve]
public void OnLogout(string message)
{
NativeLogger.I("Game_Unity_Callback", $"OnLogout reason:{message}");
}
[Preserve]
public void OnPayFailed(string reason)
{
NativeLogger.I("Game_Unity_Callback", $"OnPayFailed reason:{reason}");
}
[Preserve]
public void OnPayGoodsList(List<GoodsInfo> goodsList)
{
_cachedGoodsList = goodsList;
......@@ -78,32 +89,37 @@ public class HoolaiListener : IPlatformCallbackListener
NativeLogger.I("Game_Unity_Callback", $"OnPayGoodsList count:{goodsList?.Count ?? 0}");
}
// 获取缓存的商品列表
[Preserve]
public List<GoodsInfo> GetCachedGoodsList()
{
return _cachedGoodsList;
}
[Preserve]
public void OnPaySuccess(string message)
{
NativeLogger.I("Game_Unity_Callback", $"OnPaySuccess message:{message}");
}
[Preserve]
public void OnShareFailed(string reason)
{
NativeLogger.I("Game_Unity_Callback", $"OnShareFailed reason:{reason}");
}
[Preserve]
public void OnShareSuccess(string reason)
{
NativeLogger.I("Game_Unity_Callback", $"OnShareSuccess reason:{reason}");
}
[Preserve]
public void OnUpdate(string data)
{
NativeLogger.I("Game_Unity_Callback", $"OnUpdate data:{data}");
}
[Preserve]
public void OnCustomExit()
{
NativeLogger.I("Game_Unity_Callback", $"OnCustomExit show Game Exit Dialog!");
......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scripting;
using UnityEngine.UI;
[Preserve]
public class LogViewer : MonoBehaviour
{
[Header("UI References")]
......@@ -13,19 +16,25 @@ public class LogViewer : MonoBehaviour
public bool autoScroll = true;
public int lineHeight = 33;
[Preserve]
private Queue<string> logQueue = new Queue<string>();
[Preserve]
private bool isDirty = false;
[Preserve]
void OnEnable()
{
Application.logMessageReceived += HandleLog;
}
[Preserve]
void OnDisable()
{
Application.logMessageReceived -= HandleLog;
}
[Preserve]
void HandleLog(string logString, string stackTrace, LogType type)
{
string time = System.DateTime.Now.ToString("HH:mm:ss");
......@@ -34,6 +43,7 @@ public class LogViewer : MonoBehaviour
isDirty = true;
}
[Preserve]
void Update()
{
if (isDirty)
......@@ -44,6 +54,7 @@ public class LogViewer : MonoBehaviour
}
[Preserve]
void UpdateLogText()
{
if (logText == null || scrollRect == null)
......@@ -97,8 +108,8 @@ public class LogViewer : MonoBehaviour
}
}
// 计算文本内容的实际高度
[Preserve]
float CalculateTextHeight(string text, Text textComponent, float width)
{
if (string.IsNullOrEmpty(text))
......@@ -113,13 +124,14 @@ public class LogViewer : MonoBehaviour
return height / textComponent.pixelsPerUnit;
}
[Preserve]
void ScrollToBottomNow()
{
// 滚动到底部
scrollRect.verticalNormalizedPosition = 0f;
}
[Preserve]
public void ClearLogs()
{
logQueue.Clear();
......
{
"name": "NewAssembly",
"rootNamespace": "",
"name": "HoolaiSDK",
"rootNamespace": "HoolaiSDK",
"references": [
"Unity.TextMeshPro"
"Newtonsoft.Json"
],
"includePlatforms": [],
"excludePlatforms": [],
......@@ -14,8 +14,8 @@
"versionDefines": [
{
"name": "com.unity.nuget.newtonsoft-json",
"expression": "",
"define": ""
"expression": "3.0.0",
"define": "NEWTONSOFT_JSON_AVAILABLE"
}
],
"noEngineReferences": false
......
using UnityEngine;
using UnityEngine.Scripting;
[Preserve]
public class AndroidSdkInterface : MonoBehaviour
{
private static AndroidJavaClass _serviceClass;
......@@ -105,7 +107,7 @@ public class AndroidSdkInterface : MonoBehaviour
}
catch (System.Exception ex)
{
NativeLogger.I("fastsdk_btn_unity", $"调用 SdkManager.enterServer 异常: {ex.Message}");
NativeLogger.I("fastsdk_btn_unity", $"调用 SdkManager.sendEvent 异常: {ex.Message}");
}
}
......
......@@ -2,7 +2,9 @@
using UnityEngine;
using System.Runtime.InteropServices;
using System;
using UnityEngine.Scripting;
[Preserve]
public class IosSdkInterface
{
// ================== DllImport 声明 ==================
......
......@@ -2,7 +2,9 @@ using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Scripting;
[Preserve]
public class UnityMainThreadDispatcher : MonoBehaviour
{
private static readonly Queue<Action> _executionQueue = new Queue<Action>();
......
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Scripting;
[Preserve]
public static class HoolaiSdkManager
{
// ==================== 登录 ====================
......@@ -43,7 +45,11 @@ public static class HoolaiSdkManager
// ==================== 数据上报 ====================
public static void SendEvent(EventType eventType, PlayerInfo playerInfo)
{
string jsonStr = JsonConvert.SerializeObject(playerInfo);
string jsonStr = JsonConvert.SerializeObject(playerInfo, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Include,
DefaultValueHandling = DefaultValueHandling.Include
});
#if UNITY_ANDROID && !UNITY_EDITOR
AndroidSdkInterface.CallSdkSendEvent(eventType, jsonStr);
#elif UNITY_IOS && !UNITY_EDITOR
......
using System.Collections.Generic;
using UnityEngine.Scripting;
[Preserve]
public interface IPlatformCallbackListener
{
void OnInitSuccess(InitResult result);
......
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Scripting;
[Preserve]
[System.Serializable]
public class InitResult
{
[JsonProperty("channel")]
public string channel;
[JsonProperty("gameId")]
public int gameId;
[JsonProperty("channelId")]
public int channelId;
}
[Preserve]
[System.Serializable]
public class LoginResult
{
[JsonProperty("uid")]
public long uid;
[JsonProperty("accessToken")]
public string accessToken;
[JsonProperty("channel")]
public string channel;
public string channelUid; // 已弃用,默认为0
[JsonProperty("channelUid")]
public string channelUid; // deprecated,default value is 0
[JsonProperty("serverArea")]
public string serverArea;
public string extendInfo; // 扩展参数转 JSON 字符串再传过来
[JsonProperty("extendInfo")]
public string extendInfo;
}
[Preserve]
[System.Serializable]
public class GoodsInfo
{
[JsonProperty("currency")]
public string currency;
[JsonProperty("itemId")]
public string itemId;
[JsonProperty("itemName")]
public string itemName;
[JsonProperty("itemCount")]
public string itemCount;
[JsonProperty("itemPrice")]
public string itemPrice;
[JsonProperty("showTag")]
public string showTag;
}
[Preserve]
public class PlatformCallback : MonoBehaviour
{
private static IPlatformCallbackListener _listener;
......@@ -64,6 +96,7 @@ public class PlatformCallback : MonoBehaviour
NativeLogger.I("PlatformCallback", $"[PlatformCallback] Initialized with GameObject name: {gameObject.name}");
}
[Preserve]
public static void SetListener(IPlatformCallbackListener listener)
{
_listener = listener;
......

using UnityEngine.Scripting;
[Preserve]
public enum AccessActivityType
{
CD_KEY = 1 //CD_KEY
}
\ No newline at end of file
using UnityEngine.Scripting;
[Preserve]
public enum EventType
{
EnterServer = 1, //进服
LevelUp = 2, //升级
CreateRole = 3, //创角
CustomerAction = 4 //自定义报送
EnterServer = 1,
LevelUp = 2,
CreateRole = 3,
CustomerAction = 4
}
using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine.Scripting;
[Preserve]
public class PayParams
{
[JsonProperty("amount")]
public int Amount { get; set; }
public int Amount;
[JsonProperty("itemId")]
public string ItemId { get; set; }
public string ItemId;
[JsonProperty("itemName")]
public string ItemName { get; set; }
public string ItemName;
[JsonProperty("count")]
public int Count { get; set; }
public int Count;
[JsonProperty("callbackInfo")]
public string CallbackInfo { get; set; }
public string CallbackInfo;
[JsonProperty("notifyUrl")]
public string NotifyUrl { get; set; }
public string NotifyUrl;
[JsonProperty("currency")]
public string Currency { get; set; }
public string Currency;
[JsonProperty("opt")]
public Dictionary<string, string> Opt { get; private set; }
public Dictionary<string, string> Opt;
public PayParams()
{
......
using Newtonsoft.Json;
using UnityEngine.Scripting;
[Preserve]
public class PlayerInfo
{
[JsonProperty("roleId")]
public string RoleId { get; set; }
public string RoleId;
[JsonProperty("roleName")]
public string RoleName { get; set; }
public string RoleName;
[JsonProperty("roleLevel")]
public string RoleLevel { get; set; }
public string RoleLevel;
[JsonProperty("zoneId")]
public string ZoneId { get; set; }
public string ZoneId;
[JsonProperty("zoneName")]
public string ZoneName { get; set; }
public string ZoneName;
[JsonProperty("serverId")]
public string ServerId { get; set; }
public string ServerId;
[JsonProperty("serverName")]
public string ServerName { get; set; }
public string ServerName;
[JsonProperty("balance")]
public string Balance { get; set; }
public string Balance;
[JsonProperty("vip")]
public string Vip { get; set; }
public string Vip;
[JsonProperty("partyName")]
public string PartyName { get; set; }
public string PartyName;
[JsonProperty("appVersion")]
public string AppVersion { get; set; }
public string AppVersion;
[JsonProperty("appResVersion")]
public string AppResVersion { get; set; }
public string AppResVersion;
[JsonProperty("extendAction")]
public string ExtendAction { get; set; }
public string ExtendAction;
[JsonProperty("roleCreateTime")]
public string RoleCreateTime { get; set; }
public string RoleCreateTime;
[JsonProperty("phylum")]
public string Phylum { get; set; }
public string Phylum;
[JsonProperty("classField")]
public string ClassField { get; set; }
public string ClassField;
[Preserve]
[JsonProperty("extra")]
public string Extra { get; private set; }
private string _extra;
/// <summary>
/// 获取 Extra 字段的值(只读)
/// </summary>
[JsonIgnore]
public string Extra => _extra;
public void AddExtra(string key, int value)
{
if (string.IsNullOrEmpty(key)) return;
Extra += key+":"+CrateValue(value)+",";
_extra += key + ":" + CreateValue(value) + ",";
}
public void AddExtra(string key, double value)
{
if (string.IsNullOrEmpty(key)) return;
Extra += key + ":" + CrateValue(value) + ",";
_extra += key + ":" + CreateValue(value) + ",";
}
public void AddExtra(string key, string value)
{
if (string.IsNullOrEmpty(key) || value == null) return;
Extra += key + ":" + CrateValue(value) + ",";
_extra += key + ":" + CreateValue(value) + ",";
}
private string CrateValue(int val)
private string CreateValue(int val)
{
return "INT|||" + val;
}
private string CrateValue(double val)
private string CreateValue(double val)
{
return "DOUBLE|||" + val;
}
private string CrateValue(string val)
private string CreateValue(string val)
{
return val;
}
......
using UnityEngine;
using UnityEngine.Scripting;
/// <summary>
/// 原生日志记录器。
/// 该类直接调用 Android 或 iOS 的系统级日志API,可以绕过 Unity 的 Debug.Log 剥离机制。
/// 这意味着即使在 Release Build (非 Development Build) 中,日志也能被输出到 Logcat (Android) 或 Console (iOS)。
/// </summary>
[Preserve]
public static class NativeLogger
{
#if UNITY_ANDROID && !UNITY_EDITOR
// 为 Android 平台缓存 Java Log Class,避免重复查找
private static readonly AndroidJavaClass _logClass = new AndroidJavaClass("android.util.Log");
#endif
......@@ -20,16 +21,9 @@ public static class NativeLogger
public static void I(string tag, object message)
{
Debug.Log($"[{tag}] {message}");
#if UNITY_EDITOR
// 在编辑器中,我们仍然使用 Debug.Log,因为它功能更丰富(例如可以点击跳转)
Debug.Log($"[{tag}] {message}");
#elif UNITY_ANDROID
// 在 Android 设备上,直接调用 android.util.Log.i(tag, message)
#if UNITY_ANDROID && !UNITY_EDITOR
// 在 Android 设备上,额外调用 android.util.Log.i 输出到 Logcat
_logClass.CallStatic<int>("i", tag, message.ToString());
#elif UNITY_IOS
// 在 iOS 设备上,调用自定义的 NSLog 封装
// (需要在 Xcode 项目中添加一个 .mm 文件来实现 _logToConsole)
//_logToConsole($"[I] [{tag}] {message}");
#endif
}
......@@ -40,12 +34,9 @@ public static class NativeLogger
/// <param name="message">日志内容</param>
public static void W(string tag, object message)
{
#if UNITY_EDITOR
Debug.LogWarning($"[{tag}] {message}");
#elif UNITY_ANDROID
#if UNITY_ANDROID && !UNITY_EDITOR
_logClass.CallStatic<int>("w", tag, message.ToString());
#elif UNITY_IOS
// _logToConsole($"[W] [{tag}] {message}");
#endif
}
......@@ -56,12 +47,9 @@ public static class NativeLogger
/// <param name="message">日志内容</param>
public static void E(string tag, object message)
{
#if UNITY_EDITOR
Debug.LogError($"[{tag}] {message}");
#elif UNITY_ANDROID
#if UNITY_ANDROID && !UNITY_EDITOR
_logClass.CallStatic<int>("e", tag, message.ToString());
#elif UNITY_IOS
// _logToConsole($"[E] [{tag}] {message}");
#endif
}
}
<?xml version="1.0" encoding="utf-8"?>
<linker>
<assembly fullname="HoolaiSDK">
<!-- core config -->
<type fullname="HoolaiSdkManager" preserve="all"/>
<type fullname="PlatformCallback" preserve="all"/>
<type fullname="IPlatformCallbackListener" preserve="all"/>
<!-- data config -->
<type fullname="PayParams" preserve="all"/>
<type fullname="PlayerInfo" preserve="all"/>
<type fullname="InitResult" preserve="all"/>
<type fullname="LoginResult" preserve="all"/>
<type fullname="GoodsInfo" preserve="all"/>
<!-- enum config -->
<type fullname="EventType" preserve="all"/>
<type fullname="AccessActivityType" preserve="all"/>
<!-- platform interface config -->
<type fullname="AndroidSdkInterface" preserve="all"/>
<type fullname="IosSdkInterface" preserve="all"/>
<type fullname="UnityMainThreadDispatcher" preserve="all"/>
<!-- util config -->
<type fullname="NativeLogger" preserve="all"/>
<!-- Demo config -->
<!-- <type fullname="GameManager" preserve="all"/>
<type fullname="HoolaiListener" preserve="all"/>
<type fullname="LogViewer" preserve="all"/> -->
</assembly>
<!-- Newtonsoft.Json config-->
<assembly fullname="Newtonsoft.Json" preserve="all"/>
<!-- mscorlib config -->
<!-- <assembly fullname="mscorlib">
<type fullname="System.Collections.Generic.List`1" preserve="all"/>
<type fullname="System.Collections.Generic.Dictionary`2" preserve="all"/>
</assembly> -->
</linker>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment