using DefaultNamespace;
using MotionFramework;
using UnityEngine;
///
/// Unity 调用 JavaScript 方法的脚本
/// 用于在 Unity WebGL 中调用网页中的 JavaScript 函数
///
public class JavaScriptCaller : ModuleSingleton, IModule
{
#region 页面路径常量
///
/// 背包页面路径
///
private const string PAGE_BAG = "/pages/index/bag";
///
/// 打卡页面路径
///
private const string PAGE_DAKA = "/pages/index/daka";
///
/// 成就页面路径
///
private const string PAGE_ACHIEVE = "/pages/index/achieve";
///
/// 排行页面路径
///
private const string PAGE_RANK = "/pages/index/rank";
///
/// 拍卖行页面路径
///
private const string PAGE_AUCTION = "/pages/index/auction";
///
/// 动态页面路径
///
private const string PAGE_DYNAMICS = "/pages/index/dynamics";
///
/// 邮件页面路径
///
private const string PAGE_MAIL = "/pages/index/mail";
#endregion
///
/// 解析URL中的code参数
/// 调用JavaScript函数来解析URL,然后JavaScript会将结果传回Unity
///
public void ParseUrlCode()
{
Debug.Log("[JavaScriptCaller] 准备解析URL中的code参数");
#if UNITY_WEBGL && !UNITY_EDITOR
// 在 WebGL 平台调用 JavaScript 函数
Application.ExternalCall("ParseUrlCode");
#else
// 在编辑器或其他平台,只打印日志
Debug.LogWarning("[JavaScriptCaller] 当前不在 WebGL 平台,无法调用 JavaScript 函数");
Debug.Log("[JavaScriptCaller] 模拟调用: ParseUrlCode()");
#endif
}
///
/// 跳转到小程序页面(当前小程序内的页面跳转)
///
/// 目标页面路径,例如:"/pages/index/bag"
private void JumpToMiniProgramPage(string targetPath)
{
Debug.Log($"[JavaScriptCaller] 准备跳转到小程序页面: {targetPath}");
string newtargetPath = targetPath + $"?token={Apis.GetToken()}";
#if UNITY_WEBGL && !UNITY_EDITOR
// 在 WebGL 平台调用 JavaScript 函数
Application.ExternalCall("JumpToMiniProgramPage", newtargetPath);
#else
// 在编辑器或其他平台,只打印日志
Debug.LogWarning("[JavaScriptCaller] 当前不在 WebGL 平台,无法调用 JavaScript 函数");
Debug.Log($"[JavaScriptCaller] 模拟调用: JumpToMiniProgramPage('{newtargetPath}')");
#endif
}
///
/// 跳转到其他小程序
///
/// 目标小程序的 appId
/// 目标页面路径
private void JumpToOtherMiniProgram(string appId, string targetPath)
{
Debug.Log($"[JavaScriptCaller] 准备跳转到其他小程序 - appId: {appId}, path: {targetPath}");
#if UNITY_WEBGL && !UNITY_EDITOR
// 在 WebGL 平台调用 JavaScript 函数
Application.ExternalCall("JumpToOtherMiniProgram", appId, targetPath);
#else
// 在编辑器或其他平台,只打印日志
Debug.LogWarning("[JavaScriptCaller] 当前不在 WebGL 平台,无法调用 JavaScript 函数");
Debug.Log($"[JavaScriptCaller] 模拟调用: JumpToOtherMiniProgram('{appId}', '{targetPath}')");
#endif
}
///
/// 调用自定义 JavaScript 函数(通用方法)
///
/// JavaScript 函数名
/// 函数参数(可变参数)
private void CallJavaScriptFunction(string functionName, params object[] args)
{
Debug.Log($"[JavaScriptCaller] 准备调用 JavaScript 函数: {functionName}");
#if UNITY_WEBGL && !UNITY_EDITOR
// 在 WebGL 平台调用 JavaScript 函数
if (args != null && args.Length > 0)
{
Application.ExternalCall(functionName, args);
}
else
{
Application.ExternalCall(functionName);
}
#else
// 在编辑器或其他平台,只打印日志
Debug.LogWarning("[JavaScriptCaller] 当前不在 WebGL 平台,无法调用 JavaScript 函数");
Debug.Log($"[JavaScriptCaller] 模拟调用: {functionName}({string.Join(", ", args)})");
#endif
}
#region 便捷跳转方法
///
/// 跳转到背包页面
///
public void JumpToBag()
{
JumpToMiniProgramPage(PAGE_BAG);
}
///
/// 跳转到打卡页面
///
public void JumpToDaka()
{
JumpToMiniProgramPage(PAGE_DAKA);
}
///
/// 跳转到成就页面
///
public void JumpToAchieve()
{
JumpToMiniProgramPage(PAGE_ACHIEVE);
}
///
/// 跳转到排行页面
///
public void JumpToRank()
{
JumpToMiniProgramPage(PAGE_RANK);
}
///
/// 跳转到拍卖行页面
///
public void JumpToAuction()
{
JumpToMiniProgramPage(PAGE_AUCTION);
}
///
/// 跳转到动态页面
///
public void JumpToDynamics()
{
JumpToMiniProgramPage(PAGE_DYNAMICS);
}
///
/// 跳转到邮件页面
///
public void JumpToMail()
{
JumpToMiniProgramPage(PAGE_MAIL);
}
#endregion
public void OnCreate(object createParam)
{
}
public void OnUpdate()
{
}
public void OnDestroy()
{
}
public void OnGUI()
{
}
}