安卓代码:
package com.xxxx.countrycode;
import android.app.Activity;
import java.util.Locale;
public class AcquireCountryCode {
// 获取国家码
public String getCountryCode()
{
Locale locale = getActivity().getResources().getConfiguration().locale;
return locale.getCountry();
}
private Activity _unityActivity;
// 获取Unity的Activity
private Activity getActivity(){
if(null == _unityActivity) {
try {
Class<?> classtype = Class.forName("com.unity3d.player.UnityPlayer");
Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
_unityActivity = activity;
} catch (ClassNotFoundException e) {
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException e) {
}
}
return _unityActivity;
}
}
Unity 调用时的代码
AndroidJavaObject _aj = new AndroidJavaObject("com.xxxx.countrycode.AcquireCountryCode");
Debug.Log(_aj.Call<string>("getCountryCode"));
项目下载:https://download.csdn.net/download/qq_33537945/15450571
结果如图: