|
- using UnityEngine;
- using System.Collections;
- using ETHotfix.com.commsdk.unity3d;
- using System;
-
-
-
- /// <summary>
- /// 原生code 与Unity通信
- /// </summary>
- public class NativeBridgeUtil : MonoBehaviour
- {
-
- // 单例模式
- public static NativeBridgeUtil Instance { get; private set; }
-
- CommSDKComponent csdk;
- public void Awake()
- {
- // 实现单例模式
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- else
- {
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- gameObject.name = "NativeReceiver";
- csdk = gameObject.AddComponent<CommSDKComponent>();
- csdk.Init();
- csdk.responseHandler = OnWechatResponse;
- }
-
- public void Start()
- {
-
- #if UNITY_ANDROID //android系统
- AndroidJNI.AttachCurrentThread();
- #endif
- }
-
- public Action<string> loginCallback;
-
- #region 通用SDK
- //0 1 微信 2 闲聊 3多聊
- public void WeChatLogin(int type)
- {
- #if UNITY_ANDROID || UNITY_IOS || UNITY_IPHONE
- csdk.Authorize(type);
- #endif
- }
-
- void OnWechatResponse(int reqID, ActionType type, RespState state, Hashtable data)
- {
- Debug.Log("OnWechatResponse: reqid=" + reqID + ",action=" + type + ",state=" + state);
- switch (type)
- {
- case ActionType.Authorize:
- OnAuthorizedHandler(reqID, state, data);
- break;
- case ActionType.Share:
- break;
- case ActionType.Pay:
- break;
- case ActionType.Location:
- break;
- }
- }
-
- //微信申请授权后回调
- void OnAuthorizedHandler(int reqID, RespState state, Hashtable result)
- {
- if (state == RespState.Success)
- {
- //UILoginCtrl.Self.OnWechatLogin(result);
- Debug.Log("OnAuthorizedHandler->Success:result=" + result);
- string unionid = result["unionid"].ToString();
- Debug.Log("OnAuthorizedHandler->Success:unionid=" + unionid);
- if (loginCallback!= null)
- {
- loginCallback(unionid);
- }
- }
- else if (state == RespState.Fail)
- {
- string errorMsg = result["error_msg"].ToString();
- string statck = result["stack"].ToString();
- Debug.Log("OnAuthorizedHandler->Fail:errorMsg=" + errorMsg + ",statck=" + statck);
- }
- else if (state == RespState.Cancel)
- {
- Debug.Log("OnAuthorizedHandler: Cancel");
- }
- }
-
- #endregion
- }
-
-
-
|