162 lines
4.5 KiB
C#
162 lines
4.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.InteropServices;
|
|
using System.Threading.Tasks;
|
|
using AOT;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class WalletPanel : MonoBehaviour
|
|
{
|
|
private static ILogger logger = Debug.unityLogger;
|
|
private float update = 0.0f;
|
|
private float tickRate = 1/30;
|
|
private static bool walletInited = false;
|
|
private static bool reqBegin = false;
|
|
|
|
// 用于保存调用js方法的callback
|
|
private static readonly Dictionary<string, Action<string>>
|
|
requestCallback = new Dictionary<string, Action<string>>();
|
|
|
|
private static string TAG = "JCWallet";
|
|
|
|
// 钱包env初始化结束的事件名
|
|
private static string WALLET_INIT_EVENT = "wallet_init_event";
|
|
|
|
|
|
#if UNITY_IOS || UNITY_IPHONE
|
|
private const string DLL_NAME = "__Internal";
|
|
#else
|
|
private const string DLL_NAME = "cocos2djs";
|
|
#endif
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
// AndroidJNIHelper.debug = true;
|
|
Application.runInBackground = true;
|
|
registWalletEventDelegate (walletEvent);
|
|
SetAndrodCallback _setAndrodCallback = new SetAndrodCallback();
|
|
AndroidJavaClass jc = new AndroidJavaClass("com.jc.jcfw.JcSDK");
|
|
jc.CallStatic("initCommonCB", _setAndrodCallback);
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate(){
|
|
//Time.deltaTime
|
|
if (walletInited && reqBegin) {
|
|
tick(Time.deltaTime);
|
|
}
|
|
// update += Time.deltaTime;
|
|
// if (walletInited && update > tickRate) {
|
|
// update = 0.0f;
|
|
// tick();
|
|
// }
|
|
|
|
}
|
|
|
|
public class SetAndrodCallback : AndroidJavaProxy
|
|
{
|
|
public SetAndrodCallback() :
|
|
base("com.jc.jcfw.UnityCallback")
|
|
{
|
|
}
|
|
|
|
public void nativeCallback(string funId, string msg, int type)
|
|
{
|
|
Debug.Log("From Android: funId: " + funId + " msg: " + msg + " type: " + type);
|
|
if (type == 0 && requestCallback.ContainsKey(funId)){
|
|
requestCallback[funId](msg);
|
|
} else if (type == 1) {
|
|
// TODO::TEST
|
|
string[] paramList = new [] { msg };
|
|
int result = runWalletMethod(funId, "nativeCallback", 1, paramList);
|
|
}
|
|
}
|
|
}
|
|
|
|
public delegate void WalletCallback(string jsonStr);
|
|
|
|
/**
|
|
* 初始换钱包环境
|
|
* 该方法只需调用一次
|
|
* 钱包任何操作之前须保证该方法已正常调用
|
|
*/
|
|
public static void initWalletEnv(WalletCallback callback){
|
|
requestCallback[WALLET_INIT_EVENT] = (data) =>{
|
|
Debug.Log("wallet env inited: ");
|
|
walletInited = true;
|
|
callback (data);
|
|
};
|
|
initEnv();
|
|
}
|
|
|
|
/**
|
|
* 调用main.js中定义好的js方法
|
|
* @param methodName: 调用的方法名
|
|
* @param paramList: 所有参数都放在string数组中, 没参数的话就传new string[0]
|
|
* @param callback: unity进程收到js执行结果事件后, 会调用callback方法
|
|
*/
|
|
public static void callWalletMethod(
|
|
string methodName,
|
|
string[] paramList,
|
|
WalletCallback callback
|
|
){
|
|
if (methodName == "initWallet") {
|
|
reqBegin = true;
|
|
}
|
|
string funId = System.Guid.NewGuid().ToString();
|
|
logger.Log(TAG, "wallet method : " + methodName + " ID: " + funId);
|
|
requestCallback[funId] = (data) =>{
|
|
callback (data);
|
|
};
|
|
int result = runWalletMethod(funId, methodName, paramList.Length, paramList);
|
|
if (result == 0){
|
|
requestCallback.Remove (funId);
|
|
}
|
|
}
|
|
|
|
// begin of wallet event
|
|
// 用于接收js端返回的数据
|
|
[MonoPInvokeCallback(typeof (WalletEventDelegate))]
|
|
static void walletEvent(IntPtr name, IntPtr message)
|
|
{
|
|
string funId = Marshal.PtrToStringAnsi(name);
|
|
string msg = Marshal.PtrToStringAnsi(message);
|
|
string s_msg = "Wallet Event::";
|
|
s_msg += "ID: " + funId;
|
|
s_msg += " MSG: " + msg;
|
|
logger.Log (TAG, s_msg);
|
|
if (requestCallback.ContainsKey(funId)){
|
|
requestCallback[funId](msg);
|
|
// 这里有些情况下会导致crash, 待想到好的办法后再处理
|
|
// requestCallback.Remove (funId);
|
|
}
|
|
}
|
|
|
|
public delegate void WalletEventDelegate(IntPtr name, IntPtr message);
|
|
|
|
// 注册wallet event事件回调
|
|
[DllImport(DLL_NAME)]
|
|
public static extern void registWalletEventDelegate(WalletEventDelegate bt);
|
|
|
|
// end of wallet event
|
|
[DllImport(DLL_NAME)]
|
|
public static extern void initEnv();
|
|
|
|
[DllImport(DLL_NAME)]
|
|
public static extern void tick(float dt);
|
|
|
|
// 通用的wallet方法调用,
|
|
[DllImport(DLL_NAME)]
|
|
public static extern int
|
|
runWalletMethod(
|
|
string funId,
|
|
string methodName,
|
|
int paramCount,
|
|
string[] paramList
|
|
);
|
|
}
|