123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using UnityEngine;
- /// <summary>
- /// 安卓原生交互类
- /// </summary>
- public class AndroidInteractive : MonoBehaviour
- {
- /// <summary>
- /// 单键
- /// </summary>
- private static AndroidInteractive pInit = null;
- public static Action<string> LoginSuccessCall = null;
- public static Action<string> LoginFailCall = null;
- public static Action<string> LogoutCall = null;
- public static Action<string> PaySuccessCall = null;
- public static Action<string> PayFailCall = null;
- /// <summary>
- /// 当前版本
- /// </summary>
- public static string curVersion = "";
- /// <summary>
- /// 当前年龄
- /// </summary>
- public static int curAge = -1;
- /// <summary>
- /// 获取单键
- /// </summary>
- /// <returns>单键实例</returns>
- public static AndroidInteractive Instance()
- {
- return pInit;
- }
- /// <summary>
- /// 初始化
- /// </summary>
- private void Awake()
- {
- // 初始化
- pInit = this;
- }
- /// <summary>
- /// SDK登录成功回调
- /// </summary>
- /// <param name="uid"></param>
- private void SDKLoginSuccess(string uid)
- {
- Debug.Log("SDK 登录成功!");
- if (LoginSuccessCall != null)
- {
- LoginSuccessCall(uid);
- }
- }
- /// <summary>
- /// SDK登录失败回调
- /// </summary>
- /// <param name="uid"></param>
- private void SDKLoginFail(string msg)
- {
- Debug.Log("SDK 登录失败!");
- if (LoginFailCall != null)
- {
- LoginFailCall(msg);
- }
- }
- /// <summary>
- /// SDK登出回调
- /// </summary>
- /// <param name="msg"></param>
- private void SDKLogout(string msg)
- {
- Debug.Log("SDK 登出成功!");
- if (LogoutCall != null)
- {
- LogoutCall(msg);
- }
- }
- /// <summary>
- /// SDK支付成功回调
- /// </summary>
- /// <param name="oderID"></param>
- private void SDKPaySuccess(string oderID)
- {
- if (PaySuccessCall != null)
- {
- PaySuccessCall(oderID);
- }
- }
- /// <summary>
- /// SDK支付失败回调
- /// </summary>
- /// <param name="oderID"></param>
- private void SDKPayFail(string msg)
- {
- if (PayFailCall != null)
- {
- PayFailCall(msg);
- }
- }
- /// <summary>
- /// 查询年龄段
- /// </summary>
- public int GetUserAge()
- {
- if(curAge != -1)
- {
- return curAge;
- }
- try
- {
- using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- //// 获取平台名字
- //string PlatformName = ajo.Call<string>("GetPlatformName");
- // 获取当前版本
- string strAge = ajo.Call<string>("GetUserAge");
- Debug.Log("获取当前年龄__________________________" + strAge);
- curAge = int.Parse(strAge);
- UserProxy.Instance.player.ageRange = curAge;
- return curAge;
- }
- }
- }
- catch (Exception exc)
- {
- curAge = 99;
- return curAge;
- }
- }
- // 获取Version Name
- public String GetVersionName()
- {
- if (curVersion != "")
- {
- return curVersion;
- }
- try
- {
- using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- //// 获取平台名字
- //string PlatformName = ajo.Call<string>("GetPlatformName");
- // 获取当前版本
- curVersion = ajo.Call<string>("GetVersionName");
- Debug.Log("获取当前版本__________________________" + curVersion);
- return curVersion;
- }
- }
- }
- catch (Exception exc)
- {
- curVersion = "unity";
- return curVersion;
- }
- }
- /// <summary>
- /// 调用Java层函数
- /// </summary>
- public static bool CallJavaFunction(string className)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- try
- {
- using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- ajo.Call(className);
- return true;
- }
- }
- }
- catch (Exception exc)
- {
- Debug.LogError("调用《" + className + "》异常: " + exc);
- }
- }
- return false;
- }
- /// <summary>
- /// 调用Java层函数
- /// </summary>
- public static bool CallJavaFunction(string funName, string Parameter1)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- try
- {
- using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- ajo.Call(funName, Parameter1);
- return true;
- }
- }
- }
- catch (Exception exc)
- {
- Debug.LogError("调用《" + funName + "》异常: " + exc);
- }
- }
- return false;
- }
- /// <summary>
- /// 调用Java层函数
- /// </summary>
- public static bool CallJavaFunction(string funName, string Parameter1, string Parameter2)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- try
- {
- using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- ajo.Call(funName, Parameter1, Parameter2);
- return true;
- }
- }
- }
- catch (Exception exc)
- {
- Debug.LogError("调用《" + funName + "》异常: " + exc);
- }
- }
- return false;
- }
- /// <summary>
- /// 调用Java层函数
- /// </summary>
- public static bool CallJavaFunction(string funName, string Parameter1, string Parameter2, string Parameter3)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- try
- {
- using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- ajo.Call(funName, Parameter1, Parameter2, Parameter3);
- return true;
- }
- }
- }
- catch (Exception exc)
- {
- Debug.LogError("调用《" + funName + "》异常: " + exc);
- }
- }
- return false;
- }
- /// <summary>
- /// 调用Java层函数
- /// </summary>
- public static bool CallJavaFunction(string funName, string Parameter1, string Parameter2, string Parameter3, string Parameter4)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- try
- {
- using (AndroidJavaClass ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
- {
- using (AndroidJavaObject ajo = ajc.GetStatic<AndroidJavaObject>("currentActivity"))
- {
- ajo.Call(funName, Parameter1, Parameter2, Parameter3, Parameter4);
- return true;
- }
- }
- }
- catch (Exception exc)
- {
- Debug.LogError("调用《" + funName + "》异常: " + exc);
- }
- }
- return false;
- }
- }
|