自学内容网 自学内容网

Unity WebGL发布H5接入360渠道网页SDK

涉及到的知识点包含:

1、Lua -> C# -> JavaScript 通讯链 jslib实现

2、JavaScript -> C# -> Lua 通讯链 unityInstance.SendMessage();实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChannelMessage : MonoBehaviour
{
    public delegate void LuaDelegate(string msg);
    // 添加一个字典来存储多个回调
    private static Dictionary<string, LuaDelegate> luaCallbacks = new Dictionary<string, LuaDelegate>();
    
    // 注册回调方法
    public static void RegisterCallback(string eventName, LuaDelegate callback)
    {
        luaCallbacks[eventName] = callback;
    }
    
    // 触发回调
    private void TriggerCallback(string eventName, string message)
    {
        if (luaCallbacks.ContainsKey(eventName) && luaCallbacks[eventName] != null)
        {
            luaCallbacks[eventName](message);
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        Object.DontDestroyOnLoad(gameObject);
    }
    // js回调登录结果
    public void OnLogin(string iaaq)
    {
        Debugger.Log("OnLogin iaaq " + iaaq);
        Constant.IAAQ = iaaq;
    }
    // 
    /// <summary>
    /// js回调支付结果
    /// </summary>
    /// <param name="result">success支付成功,其余失败</param>
    public void OnPaySuccess(string result)
    {
        Debugger.Log("OnPaySuccess " + result);
        TriggerCallback("pay", result);  // 触发支付回调
    }
    // js回调支付结果
    public void OnPayFail(string result)
    {
        Debugger.Log("OnPayFail " + result );
        TriggerCallback("pay", result);  // 触发支付回调
    }
    /// <summary>
    /// js回调广告播放结果
    /// </summary>
    /// <param name="result">inited:初始化完成 success:播完广告成功,其余失败</param>
    public void OnAdPlay(string result)
    {
        Debugger.Log("OnAdPlay " + result);
        TriggerCallback("ad", result);   // 触发广告回调
    }
}

-- 在Lua中注册回调
function OnPayCallback(msg)
    framework.Logger.log("支付回调: " .. msg)
    -- 处理支付结果
end

function OnAdCallback(msg)
    framework.Logger.log("广告回调: " .. msg)
    -- 处理广告结果
   
end
CS.ChannelMessage.RegisterCallback("pay", OnPayCallback)
CS.ChannelMessage.RegisterCallback("ad", OnAdCallback)


原文地址:https://blog.csdn.net/yhhwatl/article/details/144272459

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!