Android Studio:
1.新建一个Module -> android library ;
2.在该Module下有个src/main/java/xxx包下新建两个Java class
3.写入设置手机 亮度 和 音量 的脚本:
package erlangshensystem.datanetwork.com.mylibrary;
import android.app.Activity;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowManager;
/**
* Created by C on 2015/7/15.
*/
public class Brightness {
private Activity mActivity;
public Brightness(Activity currentActivity) {
Log.i("JavaClass", "Constructor called with currentActivity = " + currentActivity);
mActivity = currentActivity;
//Settings.System.putInt(mActivity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
public void SetBrightness(int value) {
float tmp = value / 100f;
Settings.System.putInt(mActivity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) (tmp * 255));
/*
WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
if (tmp == -1)
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
else
lp.screenBrightness = tmp <= 0 ? 1 : tmp;
mActivity.getWindow().setAttributes(lp);
*/
}
public float GetBrightness()
{
int current = Settings.System.getInt(mActivity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,0);
float tmp = current/255f;
return tmp;
}
}
package erlangshensystem.datanetwork.com.mylibrary;
import android.app.Activity;
import android.app.Service;
import android.media.AudioManager;
import android.util.Log;
/**
* Created by C on 2015/7/16.
*/
public class SoundVolume
{
private Activity mActivity;
private AudioManager am;
public SoundVolume(Activity currentActivity) {
Log.i("JavaClass", "Constructor called with currentActivity = " + currentActivity);
mActivity = currentActivity;
am = (AudioManager)mActivity.getApplication().getSystemService(Service.AUDIO_SERVICE);
}
public void SetSoundVolume(int value)
{
int max = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float tmp = value /100f;
am.setStreamVolume(AudioManager.STREAM_MUSIC,(int)(tmp*max), 0);
}
public float GetSoundVolume()
{
int current = am.getStreamVolume(AudioManager.STREAM_MUSIC);
float tmp = current/(float)am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
return tmp;
}
}
4.选中该module右击选择 make module ‘xxx’;
5.然后右击show in explorer,找到..\xxx\build\outputs\aar\xxx-debug.arr的压缩包,解压获取class.jar(包名可以更改)包导入unity工程plugins/Android/下即可。
Unity Project:
新建一个脚本然后添加如下内容
AndroidJavaClass jc;
AndroidJavaObject jo,act;
private void GetAndroidAcitvity()
{
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
act = jc.GetStatic<AndroidJavaObject>("currentActivity");
}
public void SetSoundVolume()
{
GetAndroidAcitvity();
jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.SoundVolume", act);
jo.Call("SetSoundVolume", volume);
}
public void SetBrightness()
{
GetAndroidAcitvity();
jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.Brightness", act);
jo.Call("SetBrightness", intensity);
}
public void GetSoundVolume()
{
GetAndroidAcitvity();
jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.SoundVolume", act);
float curVolume = jo.Call<float>("GetSoundVolume");
volume = (int)(curVolume * 100);
}
public void GetBrightness()
{
GetAndroidAcitvity();
jo = new AndroidJavaObject("erlangshensystem.datanetwork.com.mylibrary.Brightness", act);
float curBrightness = jo.Call<float>("GetBrightness");
intensity = (int)(curBrightness * 100);
}