WX-Game1/Assets/Scripts/JavaScriptCaller.cs

208 lines
5.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DefaultNamespace;
using MotionFramework;
using UnityEngine;
/// <summary>
/// Unity 调用 JavaScript 方法的脚本
/// 用于在 Unity WebGL 中调用网页中的 JavaScript 函数
/// </summary>
public class JavaScriptCaller : ModuleSingleton<JavaScriptCaller>, IModule
{
#region
/// <summary>
/// 背包页面路径
/// </summary>
private const string PAGE_BAG = "/pages/index/bag";
/// <summary>
/// 打卡页面路径
/// </summary>
private const string PAGE_DAKA = "/pages/index/daka";
/// <summary>
/// 成就页面路径
/// </summary>
private const string PAGE_ACHIEVE = "/pages/index/achieve";
/// <summary>
/// 排行页面路径
/// </summary>
private const string PAGE_RANK = "/pages/index/rank";
/// <summary>
/// 拍卖行页面路径
/// </summary>
private const string PAGE_AUCTION = "/pages/index/auction";
/// <summary>
/// 动态页面路径
/// </summary>
private const string PAGE_DYNAMICS = "/pages/index/dynamics";
/// <summary>
/// 邮件页面路径
/// </summary>
private const string PAGE_MAIL = "/pages/index/mail";
#endregion
/// <summary>
/// 解析URL中的code参数
/// 调用JavaScript函数来解析URL然后JavaScript会将结果传回Unity
/// </summary>
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
}
/// <summary>
/// 跳转到小程序页面(当前小程序内的页面跳转)
/// </summary>
/// <param name="targetPath">目标页面路径,例如:"/pages/index/bag"</param>
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
}
/// <summary>
/// 跳转到其他小程序
/// </summary>
/// <param name="appId">目标小程序的 appId</param>
/// <param name="targetPath">目标页面路径</param>
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
}
/// <summary>
/// 调用自定义 JavaScript 函数(通用方法)
/// </summary>
/// <param name="functionName">JavaScript 函数名</param>
/// <param name="args">函数参数(可变参数)</param>
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 便
/// <summary>
/// 跳转到背包页面
/// </summary>
public void JumpToBag()
{
JumpToMiniProgramPage(PAGE_BAG);
}
/// <summary>
/// 跳转到打卡页面
/// </summary>
public void JumpToDaka()
{
JumpToMiniProgramPage(PAGE_DAKA);
}
/// <summary>
/// 跳转到成就页面
/// </summary>
public void JumpToAchieve()
{
JumpToMiniProgramPage(PAGE_ACHIEVE);
}
/// <summary>
/// 跳转到排行页面
/// </summary>
public void JumpToRank()
{
JumpToMiniProgramPage(PAGE_RANK);
}
/// <summary>
/// 跳转到拍卖行页面
/// </summary>
public void JumpToAuction()
{
JumpToMiniProgramPage(PAGE_AUCTION);
}
/// <summary>
/// 跳转到动态页面
/// </summary>
public void JumpToDynamics()
{
JumpToMiniProgramPage(PAGE_DYNAMICS);
}
/// <summary>
/// 跳转到邮件页面
/// </summary>
public void JumpToMail()
{
JumpToMiniProgramPage(PAGE_MAIL);
}
#endregion
public void OnCreate(object createParam)
{
}
public void OnUpdate()
{
}
public void OnDestroy()
{
}
public void OnGUI()
{
}
}