miduo_client/Assets/Plugins/TapDBScripts/TapDB.cs

400 lines
12 KiB
C#
Raw Normal View History

2020-07-10 09:59:56 +08:00
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using TapDBMiniJSON;
using System;
// version 2.1.4
// 帐户类型
public enum TGTUserType{
TGTTypeAnonymous = 0, // 匿名用户
TGTTypeRegistered = 1,// 注册用户
}
// 用户性别
public enum TGTUserSex{
TGTSexMale = 0, // 男性
TGTSexFemale = 1, // 女性
TGTSexUnknown = 2, // 性别未知
}
public class TapDB
{
#if UNITY_IOS
[DllImport ("__Internal")]
public static extern void TapDB_nativeSetHost(string host);
[DllImport ("__Internal")]
public static extern void TapDB_nativeSetCustomEventHost(string host);
[DllImport ("__Internal")]
public static extern void TapDB_nativeOnStart(string appId, string channel, string gameVersion);
[DllImport ("__Internal")]
public static extern void TapDB_nativeSetUser(string userId);
[DllImport ("__Internal")]
public static extern void TapDB_nativeSetLevel(int level);
[DllImport ("__Internal")]
public static extern void TapDB_nativeSetName(string name);
[DllImport ("__Internal")]
public static extern void TapDB_nativeSetServer(string server);
[DllImport ("__Internal")]
public static extern void TapDB_nativeOnChargeRequest(string orderId, string product, Int32 amount, string currencyType, string payment);
[DllImport ("__Internal")]
public static extern void TapDB_nativeOnChargeSuccess(string orderId, string product, Int32 amount, string currencyType, string payment);
[DllImport ("__Internal")]
public static extern void TapDB_nativeDeprecatedOnChargeSuccess(string orderId);
[DllImport ("__Internal")]
public static extern void TapDB_nativeOnChargeFail(string orderId, string reason);
[DllImport ("__Internal")]
public static extern void TapDB_nativeOnChargeOnlySuccess(string orderId, string product, Int32 amount, string currencyType, Int32 virtualCurrencyAmount, string payment);
[DllImport ("__Internal")]
public static extern void TapDB_nativeOnEvent(string eventCode, string properties);
#elif UNITY_ANDROID
public static string JAVA_CLASS = "com.tapdb.sdk.TapDB";
private static string UNTIFY_CLASS = "com.unity3d.player.UnityPlayer";
private static AndroidJavaClass agent = null;
private static AndroidJavaClass unityClass = null;
private static AndroidJavaClass getAgent() {
if (agent == null) {
agent = new AndroidJavaClass(JAVA_CLASS);
}
return agent;
}
private static AndroidJavaClass getUnityClass(){
if (unityClass == null) {
unityClass = new AndroidJavaClass(UNTIFY_CLASS);
}
return unityClass;
}
private static void TapDB_nativeInit(string appId, string channel, string gameVersion){
AndroidJavaObject activity = getUnityClass().GetStatic<AndroidJavaObject>("currentActivity");
getAgent().CallStatic("init", activity, appId, channel, gameVersion);
}
private static void TapDB_nativeOnResume(){
Debug.Log ("TapDB_nativeOnResume");
AndroidJavaObject activity = getUnityClass().GetStatic<AndroidJavaObject>("currentActivity");
getAgent().CallStatic("onResume", activity);
}
private static void TapDB_nativeOnStop(){
AndroidJavaObject activity = getUnityClass().GetStatic<AndroidJavaObject>("currentActivity");
getAgent().CallStatic("onStop", activity);
}
#elif UNITY_STANDALONE_WIN
[DllImport ("TapDB")]
public static extern void UnitySetHost(string host);
[DllImport ("TapDB")]
public static extern void UnityOnStart(string appId, string channel, string version);
[DllImport ("TapDB")]
public static extern void UnitySetUser(string userId, string userName);
[DllImport ("TapDB")]
public static extern void UnitySetLevel(int level);
[DllImport ("TapDB")]
public static extern void UnitySetServer(string server);
[DllImport ("TapDB")]
public static extern void UnityOnCharge(string orderId, string product, int amount, string currencyType, int virtualCurrencyAmount, string payment);
#endif
/**
*
* https://abc.example.com/的格式,不能为空
*/
public static void setHost(string host){
if (host == null) {
host = "";
}
#if UNITY_IOS
TapDB_nativeSetHost(host);
#elif UNITY_ANDROID
getAgent().CallStatic("setHost", host);
#elif UNITY_STANDALONE_WIN
UnitySetHost(host);
#endif
}
/**
*
* https://abc.example.com/的格式,不能为空
*/
public static void setCustomEventHost(string host){
if (host == null) {
host = "";
}
#if UNITY_IOS
TapDB_nativeSetCustomEventHost(host);
#elif UNITY_ANDROID
getAgent().CallStatic("setCustomEventHost", host);
#endif
}
/**
*
* appId: TapDBappId
* channel:
* gameVersion:
*/
public static void onStart(string appId, string channel, string gameVersion){
if (appId == null) {
appId = "";
}
if (channel == null) {
channel = "";
}
if (gameVersion == null) {
gameVersion = "";
}
#if UNITY_IOS
TapDB_nativeOnStart(appId, channel, gameVersion);
#elif UNITY_ANDROID
TapDB_nativeInit(appId, channel, gameVersion);
// TapDB_nativeOnResume();
#elif UNITY_STANDALONE_WIN
UnityOnStart(appId, channel, gameVersion);
#endif
}
public static void onResume(){
#if UNITY_ANDROID
TapDB_nativeOnResume();
#endif
}
public static void onStop(){
#if UNITY_ANDROID
TapDB_nativeOnStop();
#endif
}
/**
*
* userId: IDIDID
* userType:
* userSex:
* userAge: 0
*/
[Obsolete("接口已弃用,调用setUser(string userId)")]
public static void setUser(string userId, TGTUserType userType, TGTUserSex userSex, int userAge, string userName){
if (userId == null) {
userId = "";
}
if (userName == null) {
userName = "";
}
#if UNITY_IOS
TapDB_nativeSetUser(userId);
TapDB_nativeSetName(userName);
#elif UNITY_ANDROID
getAgent().CallStatic("setUser", userId);
getAgent().CallStatic("setName", userName);
#elif UNITY_STANDALONE_WIN
UnitySetUser(userId, userName);
#endif
}
/**
*
* userId: IDIDID
*/
public static void setUser(string userId){
if (userId == null) {
userId = "";
}
#if UNITY_IOS
TapDB_nativeSetUser(userId);
#elif UNITY_ANDROID
getAgent().CallStatic("setUser", userId);
#elif UNITY_STANDALONE_WIN
UnitySetUser(userId, "");
#endif
}
/**
*
* level:
*/
public static void setLevel(int level){
#if UNITY_IOS
TapDB_nativeSetLevel(level);
#elif UNITY_ANDROID
getAgent().CallStatic("setLevel", level);
#elif UNITY_STANDALONE_WIN
UnitySetLevel(level);
#endif
}
/**
*
* name:
*/
public static void setName(string name){
#if UNITY_IOS
TapDB_nativeSetName(name);
#elif UNITY_ANDROID
getAgent().CallStatic("setName", name);
#elif UNITY_STANDALONE_WIN
#endif
}
/**
*
* server:
*/
public static void setServer(string server){
if (server == null) {
server = "";
}
#if UNITY_IOS
TapDB_nativeSetServer(server);
#elif UNITY_ANDROID
getAgent().CallStatic("setServer", server);
#elif UNITY_STANDALONE_WIN
UnitySetServer(server);
#endif
}
/**
*
* orderId: ID
* product:
* amount:
* currencyType: CNY USD EUR
* virtualCurrencyAmount:
* payment:
*/
[Obsolete("接口已弃用")]
public static void onChargeRequest(string orderId, string product, Int32 amount, string currencyType, Int32 virtualCurrencyAmount, string payment){
#if UNITY_IOS
TapDB_nativeOnChargeRequest(orderId, product, amount, currencyType, payment);
#elif UNITY_ANDROID
getAgent().CallStatic("onChargeRequest", orderId, product, (long)amount, currencyType, (long)virtualCurrencyAmount, payment);
#endif
}
/**
*
* orderId: IDorderId
*/
[Obsolete("已弃用,请调用onChargeSuccess(string orderId, string product, Int32 amount, string currencyType, string payment)")]
public static void onChargeSuccess(string orderId){
#if UNITY_IOS
TapDB_nativeDeprecatedOnChargeSuccess(orderId);
#elif UNITY_ANDROID
getAgent().CallStatic("onChargeSuccess", orderId);
#endif
}
/**
*
* orderId: IDorderId
* product:
* amount: 100
* currencyType: CNY USD EUR
* payment:
*/
public static void onChargeSuccess(string orderId, string product, Int32 amount, string currencyType, string payment){
#if UNITY_IOS
TapDB_nativeOnChargeSuccess(orderId,product,amount,currencyType,payment);
#elif UNITY_ANDROID
getAgent().CallStatic("onCharge", orderId,product,(long)amount,currencyType,payment);
#endif
}
/**
*
* orderId: IDorderId
* reason:
*/
[Obsolete("接口已弃用")]
public static void onChargeFail(string orderId, string reason){
#if UNITY_IOS
TapDB_nativeOnChargeFail(orderId, reason);
#elif UNITY_ANDROID
getAgent().CallStatic("onChargeFail", orderId, reason);
#endif
}
/**
*
* orderId: ID
* product:
* amount: 100
* currencyType: CNY USD EUR
* virtualCurrencyAmount:
* payment:
*/
[Obsolete("已弃用,请调用onChargeSuccess:product:amount:currencyType:payment")]
public static void onChargeOnlySuccess(string orderId, string product, Int32 amount, string currencyType, Int32 virtualCurrencyAmount, string payment){
if (orderId == null) {
orderId = "";
}
if (product == null) {
product = "";
}
if (currencyType == null) {
currencyType = "";
}
if (payment == null) {
payment = "";
}
#if UNITY_IOS
TapDB_nativeOnChargeOnlySuccess(orderId, product, amount, currencyType, virtualCurrencyAmount, payment);
#elif UNITY_ANDROID
getAgent().CallStatic("onChargeOnlySuccess", orderId, product, (long)amount, currencyType, (long)virtualCurrencyAmount, payment);
#elif UNITY_STANDALONE_WIN
UnityOnCharge(orderId, product, amount, currencyType, virtualCurrencyAmount, payment);
#endif
}
/**
*
* eventCode:
* properties:
*/
public static void onEvent(string eventCode, Dictionary<string, object> properties) {
if (eventCode == null) {
eventCode = "";
}
if (properties == null) {
properties = new Dictionary<string, object>();
}
string stringProperties = Json.Serialize(properties);
#if UNITY_IOS
TapDB_nativeOnEvent(eventCode, stringProperties);
#elif UNITY_ANDROID
getAgent().CallStatic("onEventDeprecated", eventCode, stringProperties);
#endif
}
}