我正在制作一个应用程序,需要能够检查以下设置是否已在 Android 手机上激活:蓝牙/手机旋转/手电筒/平面模式/GPS/手机亮度/静音模式。
我还没有找到使用 C# 在 Unity 中执行此操作的任何方法。我找到了使用 Xamarin 来完成它的方法,但它们都不能与 Unity 一起使用(或者也许我没有做对),我找到的唯一方法是使用 Java 并将其制作成插件并在 C# 脚本中调用它。但我找不到一个明确的方法来完成这项工作。如果这是唯一的解决方案,您能否解释一下如何操作,我找到的所有文档都来自 2014 年的旧版本。
我认为对此有一个简单的解决方案,但我就是找不到。 list 部分没有问题,我会添加所需的权限。
最佳答案
在 Java 中,您要调用的方法应该是公共(public)的或静态的,您必须将 java 源代码构建为库(在 build.gradle 中:apply plugin: 'com.android. library'
),并将 .aar 添加到 Unity 的 Assets/Plugins/Android/
文件夹中。
然后你可以像这样在 Unity 中实例化你的插件:
// this class string is the package at the top of your Java class extended with the class name, e.g.:
// package com.yourcompany.you.package;
string classString = "com.yourcompany.you.package.className";
// Get the class
var tempAjc = new AndroidJavaClass(classString);
// Here you can call a static method on the class that returns an instance of the class if you want to pass some parameters upon creation
_androidObject = tempAjc.CallStatic<AndroidJavaObject>("CreateInstance",
new object[] {arg1, arg2});
// non static call on your new instance
_androidObject.Call("PassingMoreStuff", initParam);
// if you want to return something from Java to Unity:
int javaVal = _androidObject.Call<int>(methodName, parameters);
关于c# - 如何在 C# 中使用 Unity 访问 android 系统设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57474794/