【针对android包】采用adb设置变量来控制unity功能的开放与关闭

针对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;
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值