172 lines
5.3 KiB
C#
172 lines
5.3 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
public abstract class SdkAndroidBase : SdkBase
|
|||
|
{
|
|||
|
private AndroidJavaClass _javaClass;
|
|||
|
private bool _useGm;
|
|||
|
|
|||
|
protected AndroidJavaObject javaObject { get; private set; }
|
|||
|
|
|||
|
public override bool useGm
|
|||
|
{
|
|||
|
get { return false; }
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 根据不同渠道获得不同的Sdk实例
|
|||
|
/// </summary>
|
|||
|
// ReSharper disable once UnusedMember.Global
|
|||
|
public static SdkBase CreateSdkLink()
|
|||
|
{
|
|||
|
SdkBase result;
|
|||
|
var channel = AssetUpdateManager.channel;
|
|||
|
// 模拟器内网测试流程
|
|||
|
if (channel == "Editor")
|
|||
|
result = new SdkEditor();
|
|||
|
else if (channel.StartsWith("Test"))
|
|||
|
result = new SdkEmpty();
|
|||
|
else if (channel.StartsWith("YuCheng"))
|
|||
|
result = new SdkAndroidYuCheng();
|
|||
|
else if (channel.StartsWith("Traceless01"))
|
|||
|
result = new SdkAndroidTraceless01();
|
|||
|
else if (channel.StartsWith("Traceless"))
|
|||
|
result = new SdkAndroidTraceless();
|
|||
|
else
|
|||
|
{
|
|||
|
Debug.LogError(string.Format("Unable to handle channel {0}! Controller falls into Default!", channel));
|
|||
|
result = null;
|
|||
|
}
|
|||
|
var sdkAndroid = result as SdkAndroidBase;
|
|||
|
if (sdkAndroid != null)
|
|||
|
{
|
|||
|
AndroidJavaClass javaClass;
|
|||
|
AndroidJavaObject javaObject;
|
|||
|
try
|
|||
|
{
|
|||
|
javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
|||
|
javaObject = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Debug.LogError(ex);
|
|||
|
javaClass = null;
|
|||
|
javaObject = null;
|
|||
|
}
|
|||
|
|
|||
|
if (javaObject == null)
|
|||
|
{
|
|||
|
Debug.LogError("Unable to attach to JavaObject! Sdk falls into Default!");
|
|||
|
result = null;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
sdkAndroid.javaObject = javaObject;
|
|||
|
sdkAndroid._javaClass = javaClass;
|
|||
|
sdkAndroid.HuaWeiResolutionFix();
|
|||
|
}
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
protected AndroidJavaObject GetRoleInfoMap(RoleDataSubmit submitType)
|
|||
|
{
|
|||
|
var result = GetRoleInfoDict(submitType);
|
|||
|
return ConvertToJavaMap(result);
|
|||
|
}
|
|||
|
|
|||
|
public override void Dispose()
|
|||
|
{
|
|||
|
if (javaObject != null)
|
|||
|
{
|
|||
|
javaObject.Dispose();
|
|||
|
javaObject = null;
|
|||
|
}
|
|||
|
|
|||
|
if (_javaClass != null)
|
|||
|
{
|
|||
|
_javaClass.Dispose();
|
|||
|
_javaClass = null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#region Call Sdk Function
|
|||
|
public override void LoginSdk(bool isAuto)
|
|||
|
{
|
|||
|
javaObject.Call("Login", isAuto);
|
|||
|
}
|
|||
|
|
|||
|
public override void Logout()
|
|||
|
{
|
|||
|
javaObject.Call("Logout");
|
|||
|
}
|
|||
|
|
|||
|
public override void SwitchAccount()
|
|||
|
{
|
|||
|
javaObject.Call("SwitchAccount");
|
|||
|
}
|
|||
|
|
|||
|
public override void CloseGame()
|
|||
|
{
|
|||
|
javaObject.Call("CloseGame");
|
|||
|
}
|
|||
|
|
|||
|
public override void ShakeDevice()
|
|||
|
{
|
|||
|
Debug.LogError("Shake Device is not implemented in Android!");
|
|||
|
}
|
|||
|
|
|||
|
public override void SubmitData(RoleDataSubmit submitType)
|
|||
|
{
|
|||
|
base.SubmitData(submitType);
|
|||
|
var roleInfo = GetRoleInfoMap(submitType);
|
|||
|
javaObject.Call("SubmitRole", roleInfo);
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 华为机器专用分辨率校正工具
|
|||
|
/// </summary>
|
|||
|
private void HuaWeiResolutionFix()
|
|||
|
{
|
|||
|
var deviceName = SystemInfo.deviceName;
|
|||
|
var deviceModule = SystemInfo.deviceModel;
|
|||
|
var regex = new Regex("Hua\\s*Wei", RegexOptions.Singleline | RegexOptions.IgnoreCase);
|
|||
|
var isHuaWei = regex.Match(deviceName).Success ||
|
|||
|
regex.Match(deviceModule).Success;
|
|||
|
var realMetrics = javaObject.Call<float>("GetRealMetrics");
|
|||
|
Debug.Log(string.Format("Device Name: {0}, Module {1}, is HuaWei {2}! Real Metrics {3}!", deviceName,
|
|||
|
deviceModule, isHuaWei, realMetrics));
|
|||
|
if (isHuaWei && realMetrics >= 2f)
|
|||
|
{
|
|||
|
// 资料里面显示的是提高到1.5倍,但是渠道反馈仍然有问题,因此提高到匹配realMetrics的下限。
|
|||
|
var res = new Vector2(Screen.width, Screen.height) * 2f;
|
|||
|
Screen.SetResolution(Mathf.CeilToInt(res.x), Mathf.CeilToInt(res.y), true);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static AndroidJavaObject ConvertToJavaMap(IDictionary<string, string> parameters)
|
|||
|
{
|
|||
|
var javaMap = new AndroidJavaObject("java.util.HashMap");
|
|||
|
var putMethod = AndroidJNIHelper.GetMethodID(
|
|||
|
javaMap.GetRawClass(), "put",
|
|||
|
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
|
|||
|
var args = new object[2];
|
|||
|
foreach (var kvp in parameters)
|
|||
|
using (var k = new AndroidJavaObject(
|
|||
|
"java.lang.String", kvp.Key))
|
|||
|
{
|
|||
|
using (var v = new AndroidJavaObject(
|
|||
|
"java.lang.String", kvp.Value))
|
|||
|
{
|
|||
|
args[0] = k;
|
|||
|
args[1] = v;
|
|||
|
AndroidJNI.CallObjectMethod(javaMap.GetRawObject(),
|
|||
|
putMethod, AndroidJNIHelper.CreateJNIArgArray(args));
|
|||
|
}
|
|||
|
}
|
|||
|
return javaMap;
|
|||
|
}
|
|||
|
}
|