wangguan
2020-12-14 5ad15def0b0cd852a3aac835135af101122db7f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using LitJson;
 
public class SDKCallBack : MonoBehaviour
{
    public static SDKCallBack ins;
    public List<Action> list_Action = new List<Action>();
    public List<string> list_callBackName = new List<string>();
    public Dictionary<string, Action> dic = new Dictionary<string, Action>();
 
    public void Awake()
    {
        if (ins)
        {
            Destroy(gameObject);
        }
        else
        {
            ins = this;
            DontDestroyOnLoad(this);
            if (KTGMGemClient.GameConfig.useSDK)
            {
                Debug.Log("安卓SDK初始化");
                SDKManager.ins.sdk.setCallBackClazz("SDKCallBack", "AndroidCallBack");
                // 在这里开始初始化SDK
                SDKManager.ins.sdk.initSdk();
            }
 
        }
 
    }
 
 
    public void setNextAction(string act, Action func)
    {
        if (!dic.ContainsKey(act))
        {
            dic.Add(act, func);
        }
    }
 
    // 安卓所有的回调方法都通过这里通知Unity
    public void AndroidCallBack(string arg)
    {
        Debug.Log("安卓返回值===>" + arg);
        JsonData data = JsonMapper.ToObject(arg);
        //loginData = JsonMapper.ToObject(value);
 
        if (data.ContainsKey("action"))
        {
            // data.data.openid
            // { "code":200,"msg":"success","data":{ "is_certify":0,"age":0,"openid":"1000484"} }
 
            //{ "action":"login", "data":{ "code":200,"msg":"success","data":{ "is_certify":0,"age":0,"openid":"1000485"} } }
            string act = data["action"].ToString();
            Debug.Log(act);
            if (act == "login")
            {
                JsonData loginData = data["data"];
                if ((int)loginData["code"] == 200)
                {
                    if (loginData.ContainsKey("msg"))
                    {
                        string result = loginData["msg"].ToString();
                        if (result.Equals("success"))
                        {
                            Debug.Log("成功了");
                        }
                        else
                        {
                            Debug.Log("失败了");
 
                        }
                    }
 
                    string uid = loginData["data"]["openid"].ToString();
                    SDKManager.ins.sdk.uid = (string)uid;
                    LoginResult(act);
                }
                else
                {
                    // 登录失败则直接重新拉起
                    SDKManager.ins.sdk.login();
                }
 
            }
        }
 
    }
 
    void LoginResult(string act)
    {
        Action func = dic[act];
        func?.Invoke();
    }
 
    void Chongzhi()
    {
 
    }
 
    // Start is called before the first frame update
    void Start()
    {
    }
 
    // Update is called once per frame
    void Update()
    {
 
    }
}