针对android包,采用adb设置变量用以控制unity功能的开放与关闭
设置
adb shell setprop debug.com.xxx.xxx.[key] [value]
校验
adb shell getprop
【android层】java代码获取adb shell 设置的系统属性
public String getProperty(String propName) {
Class<?> classType = null;
String buildVersion = null;
try {
classType = Class.forName("android.os.SystemProperties");
Method getMethod = classType.getDeclaredMethod("get", new Class<?>[]{String.class});
buildVersion = (String) getMethod.invoke(classType, new Object[]{propName});
} catch (Exception e) {
e.printStackTrace();
}
return buildVersion;
}
【unity层】C#代码调用,首先获取环境变量,获取不到继续从android系统属性获取
通过Environment类进行桥接
public static class Environment
{
public static string CurrentDirectory => System.Environment.CurrentDirectory;
public static void SetEnvironmentVariable(string variable, string value) => System.Environment.SetEnvironmentVariable(variable, value);
public static string GetEnvironmentVariable(string variable)
{
#if UNITY_ANDROID
var result = System.Environment.GetEnvironmentVariable(variable);
if (result != null) return result;
result = Application.Android.callSdkApiObject<string>("getProperty", string.Format("debug.{0}.{1}", Application.identifier, variable));
return string.IsNullOrEmpty(result) ? null : result;
#else
return System.Environment.GetEnvironmentVariable(variable);
#endif
}
public static string[] GetCommandLineArgs() => System.Environment.GetCommandLineArgs();
public static System.Collections.IDictionary GetEnvironmentVariables() => System.Environment.GetEnvironmentVariables();
}
实际使用1
void ImpInit.OnFinishInit()
{
try
{
Params["ProxyHostTEST"] = new System.Net.WebProxy(Environment.GetEnvironmentVariable("ProxyHostTEST"), false);
}
catch (Exception)
{
Params["ProxyHostTEST"] = "";
}
}
实际使用2
if (bool.TryParse(Environment.GetEnvironmentVariable("DisableEncryptKey"), out bool isDisable) && isDisable)
{
EncryptKey = null;
}