Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

104 řádky
2.7 KiB

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