您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

102 行
2.7 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. using ETHotfix.com.commsdk.unity3d;
  4. using System.Text.RegularExpressions;
  5. using System;
  6. using System.Collections.Generic;
  7. using ETModel;
  8. using ETModel.com.commsdk.unity3d;
  9. /// <summary>
  10. /// 原生code 与Unity通信
  11. /// </summary>
  12. public class NativeBridgeUtil : MonoBehaviour
  13. {
  14. // 单例模式
  15. public static NativeBridgeUtil Instance { get; private set; }
  16. CommSDKComponent csdk;
  17. public void Awake()
  18. {
  19. // 实现单例模式
  20. if (Instance != null && Instance != this)
  21. {
  22. Destroy(gameObject);
  23. return;
  24. }
  25. else
  26. {
  27. Instance = this;
  28. DontDestroyOnLoad(gameObject);
  29. }
  30. gameObject.name = "NativeReceiver";
  31. csdk = gameObject.AddComponent<CommSDKComponent>();
  32. csdk.Init();
  33. csdk.responseHandler = OnWechatResponse;
  34. }
  35. public void Start()
  36. {
  37. #if UNITY_ANDROID //android系统
  38. AndroidJNI.AttachCurrentThread();
  39. #endif
  40. }
  41. #region 通用SDK
  42. //0 1 微信 2 闲聊 3多聊
  43. public void WeChatLogin(int type)
  44. {
  45. #if UNITY_ANDROID || UNITY_IOS || UNITY_IPHONE
  46. csdk.Authorize(type);
  47. #endif
  48. }
  49. void OnWechatResponse(int reqID, ActionType type, RespState state, Hashtable data)
  50. {
  51. Debug.Log("OnWechatResponse: reqid=" + reqID + ",action=" + type + ",state=" + state);
  52. switch (type)
  53. {
  54. case ActionType.Authorize:
  55. OnAuthorizedHandler(reqID, state, data);
  56. break;
  57. case ActionType.Share:
  58. break;
  59. case ActionType.Pay:
  60. break;
  61. case ActionType.Location:
  62. break;
  63. }
  64. }
  65. //微信申请授权后回调
  66. void OnAuthorizedHandler(int reqID, RespState state, Hashtable result)
  67. {
  68. if (state == RespState.Success)
  69. {
  70. //UILoginCtrl.Self.OnWechatLogin(result);
  71. Debug.Log("OnAuthorizedHandler->Success:result=" + result);
  72. string unionid = result["unionid"].ToString();
  73. Debug.Log("OnAuthorizedHandler->Success:unionid=" + unionid);
  74. }
  75. else if (state == RespState.Fail)
  76. {
  77. string errorMsg = result["error_msg"].ToString();
  78. string statck = result["stack"].ToString();
  79. Debug.Log("OnAuthorizedHandler->Fail:errorMsg=" + errorMsg + ",statck=" + statck);
  80. }
  81. else if (state == RespState.Cancel)
  82. {
  83. Debug.Log("OnAuthorizedHandler: Cancel");
  84. }
  85. }
  86. #endregion
  87. }