wangguan
2020-10-27 1cc976d33bfbe7488c85df9d35f28aa6e4360294
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using UnityEngine;
#if UNITY_IPHONE
using System.Runtime.InteropServices;
#endif
 
 
public enum AccountType
{
    ANONYMOUS = 0,
    REGISTERED = 1,
    SINA_WEIBO = 2,
    QQ = 3,
    QQ_WEIBO = 4,
    ND91 = 5,
    WEIXIN = 6,
    TYPE1 = 11,
    TYPE2 = 12,
    TYPE3 = 13,
    TYPE4 = 14,
    TYPE5 = 15,
    TYPE6 = 16,
    TYPE7 = 17,
    TYPE8 = 18,
    TYPE9 = 19,
    TYPE10 = 20
}
 
public enum Gender
{
    UNKNOW = 0,
    MALE = 1,
    FEMALE = 2
}
 
 
public class TDGAAccount
{
    private static TDGAAccount account;
 
#if UNITY_ANDROID
    private static readonly string ACCOUNT_CLASS = "com.tendcloud.tenddata.TDGAAccount";
    private static AndroidJavaClass accountClass;
    private AndroidJavaObject mAccount;
#endif
 
#if UNITY_IPHONE
    [DllImport("__Internal")]
    private static extern void TDGASetAccount(string accountId);
 
    [DllImport("__Internal")]
    private static extern void TDGASetAccountName(string accountName);
 
    [DllImport("__Internal")]
    private static extern void TDGASetAccountType(int accountType);
 
    [DllImport("__Internal")]
    private static extern void TDGASetLevel(int level);
 
    [DllImport("__Internal")]
    private static extern void TDGASetGender(int gender);
 
    [DllImport("__Internal")]
    private static extern void TDGASetAge(int age);
 
    [DllImport("__Internal")]
    private static extern void TDGASetGameServer(string gameServer);
#endif
 
    public static TDGAAccount SetAccount(string accountId)
    {
        if (account == null)
        {
            account = new TDGAAccount();
        }
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (accountClass == null)
            {
                accountClass = new AndroidJavaClass(ACCOUNT_CLASS);
            }
            account.mAccount = accountClass.CallStatic<AndroidJavaObject>("setAccount", accountId);
#endif
#if UNITY_IPHONE
            TDGASetAccount(accountId);
#endif
        }
        return account;
    }
 
    public void SetAccountName(string accountName)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (mAccount != null)
            {
                mAccount.Call("setAccountName", accountName);
            }
#endif
#if UNITY_IPHONE
            TDGASetAccountName(accountName);
#endif
        }
    }
 
    public void SetAccountType(AccountType type)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (mAccount != null)
            {
                AndroidJavaClass enumClass = new AndroidJavaClass("com.tendcloud.tenddata.TDGAAccount$AccountType");
                AndroidJavaObject obj = enumClass.CallStatic<AndroidJavaObject>("valueOf", type.ToString());
                mAccount.Call("setAccountType", obj);
                enumClass.Dispose();
            }
#endif
#if UNITY_IPHONE
            TDGASetAccountType((int)type);
#endif
        }
    }
 
    public void SetLevel(int level)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (mAccount != null)
            {
                mAccount.Call("setLevel", level);
            }
#endif
#if UNITY_IPHONE
            TDGASetLevel(level);
#endif
        }
    }
 
    public void SetAge(int age)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (mAccount != null)
            {
                mAccount.Call("setAge", age);
            }
#endif
#if UNITY_IPHONE
            TDGASetAge(age);
#endif
        }
    }
 
    public void SetGender(Gender type)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (mAccount != null)
            {
                AndroidJavaClass enumClass = new AndroidJavaClass("com.tendcloud.tenddata.TDGAAccount$Gender");
                AndroidJavaObject obj = enumClass.CallStatic<AndroidJavaObject>("valueOf", type.ToString());
                mAccount.Call("setGender", obj);
                enumClass.Dispose();
            }
#endif
#if UNITY_IPHONE
            TDGASetGender((int)type);
#endif
        }
    }
 
    public void SetGameServer(string gameServer)
    {
        if (Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.WindowsEditor)
        {
#if UNITY_ANDROID
            if (mAccount != null)
            {
                mAccount.Call("setGameServer", gameServer);
            }
#endif
#if UNITY_IPHONE
            TDGASetGameServer(gameServer);
#endif
        }
    }
}