107 lines
2.8 KiB
C#
107 lines
2.8 KiB
C#
|
|
#if UNITY_WEBGL
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Runtime.InteropServices;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace HC
|
|||
|
|
{
|
|||
|
|
public class HCWxTools : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
private static HCWxTools Cw { get; set; }
|
|||
|
|
|
|||
|
|
public Action<string, string> WxCallUnityAction;
|
|||
|
|
|
|||
|
|
public static HCWxTools Instance
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (Cw != null) return Cw;
|
|||
|
|
if (!Application.isPlaying)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Cw = new GameObject(nameof(HCWxTools)).AddComponent<HCWxTools>();
|
|||
|
|
DontDestroyOnLoad(Cw.gameObject);
|
|||
|
|
|
|||
|
|
return Cw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
[DllImport("__Internal", EntryPoint = "JsUnityToWxMiniGame")]
|
|||
|
|
public static extern string UnityToWxMiniGame([In] string obj0);
|
|||
|
|
|
|||
|
|
|
|||
|
|
public string UnityToWxMiniGame(Dictionary<string, object> args)
|
|||
|
|
{
|
|||
|
|
#if !UNITY_EDITOR
|
|||
|
|
var jsonArgs = MiniJSON.Json.Serialize(args);
|
|||
|
|
var returnStr = UnityToWxMiniGame(jsonArgs);
|
|||
|
|
HCDebugger.LogWarning($"[UnityToWxMiniGame] {jsonArgs} returnStr = {returnStr}");
|
|||
|
|
return returnStr;
|
|||
|
|
#else
|
|||
|
|
HCDebugger.LogWarning($"[UnityToWxMiniGame] {MiniJSON.Json.Serialize(args)}");
|
|||
|
|
return "";
|
|||
|
|
#endif
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void OnWxCallUnity(string content)
|
|||
|
|
{
|
|||
|
|
HCDebugger.LogDebug($"[HCWxTools] [OnWxCallUnity] content : {content}");
|
|||
|
|
|
|||
|
|
var strings = content.Split(new[] { "#@#" }, StringSplitOptions.None);
|
|||
|
|
var cmd = strings[0];
|
|||
|
|
content = strings[1];
|
|||
|
|
if (cmd.Equals("getDistinctId"))
|
|||
|
|
{
|
|||
|
|
HCTools.SavePlayerPrefsString(HCTools.SS_DISTINCT_ID, content);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
WxCallUnityAction?.Invoke(cmd, content);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
[Serializable]
|
|||
|
|
public class WxNativeAdEntity
|
|||
|
|
{
|
|||
|
|
public string cmd;
|
|||
|
|
public string adPostion;
|
|||
|
|
public string adUnit;
|
|||
|
|
public int width;
|
|||
|
|
public int height;
|
|||
|
|
public int left;
|
|||
|
|
public int top;
|
|||
|
|
public int _fixed;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
[Serializable]
|
|||
|
|
public class WxNativeCallback
|
|||
|
|
{
|
|||
|
|
public string cmd;
|
|||
|
|
public string adPostion;
|
|||
|
|
public string adUnit;
|
|||
|
|
public int code;
|
|||
|
|
public string msg;
|
|||
|
|
|
|||
|
|
public string GetAdUnit(Dictionary<string, string> showNativeAdUnit)
|
|||
|
|
{
|
|||
|
|
var adShowFailPosition = adPostion;
|
|||
|
|
if (string.IsNullOrEmpty(adShowFailPosition))
|
|||
|
|
{
|
|||
|
|
foreach (var (key, value) in showNativeAdUnit)
|
|||
|
|
{
|
|||
|
|
if (value.Equals(adUnit))
|
|||
|
|
{
|
|||
|
|
return key;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endif
|