项目中遇到手机摇一摇功能边整理下:
using UnityEngine;
using System.Collections;
public class ShakePhone : MonoBehaviour
{
float old_y = 0;
float new_y = 0;
float max_y = 0;
float min_y = 0;
float d_y = 0;
void Update()
{
new_y = Input.acceleration.y;
d_y = new_y - old_y;
old_y = new_y;
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
}
int i;
void OnGUI()
{
GUI.Label(new Rect(100, 100, 100, 100), "g:" + Input.acceleration + "d_y:" + d_y);
GUI.Label(new Rect(100, 210, 100, 100), "i:" + i);
if (d_y > 2)
{
i++;
Handheld.Vibrate(); //手机的震动效果
}
}
}