Unity自带的Input.acceleration功能和android原生的SensorEventListener监听器实现的功能是一样的
下面是android原生获取手机目前旋转状态值的代码:
private SensorEventListener listener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
@Override
public void onSensorChanged(SensorEvent event) {
String topBottomString = "";
String leftRightString = "";
if (event.values[1] == 0) {
//topBottomString = "手机头部或底部没有翘起";
topBottomString=event.values[1]+"";
} else if (event.values[1] > 0) {
//topBottomString = "底部向上翘起" + event.values[1] + "度";
topBottomString=event.values[1]+"";
} else if (event.values[1] < 0) {
//topBottomString = "顶部向上翘起" + Math.abs(event.values[1]) + "度";
topBottomString=event.values[1]+"";
}
if (event.values[2] == 0) {
//leftRightString = "手机左侧或右侧没有翘起";
leftRightString=event.values[2]+"";
} else if (event.values[2] > 0) {
//leftRightString = "右侧向上翘起" + event.values[2] + "度";
leftRightString=event.values[2]+"";
} else if (event.values[2] < 0) {
//leftRightString = "左侧向上翘起" + Math.abs(event.values[2]) + "度";
leftRightString=event.values[2]+"";
}
qingDaoValue(leftRightString,topBottomString);
//textView02.setText("手机顶部或尾部翘起的角度: " + topBottomString);
//textView03.setText("手机左侧或右侧翘起的角度: " + leftRightString);
}
};
unity自带的和android原生的 两者的实现功能是一样的
unity返回的是-1到1
Android返回的是-90 到 90
FR:海涛高软(hunk Xu) QQ技术交流群:386476712