Unity 摇一摇震动并随机播放音乐

//摇一摇手机震动并随机切换音乐
//测量重力感应在各个轴上的最大,最小值
using UnityEngine;
using System.Collections;

public class Shake : MonoBehaviour {

	public AudioClip []clips;
	
	public float newAcceX;
	public float oldAcceX;
	public float deltaAcceX;
	public float maxAcceX;
	public float minAcceX;

	public float newAcceY;
	public float oldAcceY;
	public float deltaAcceY;
	public float maxAcceY;
	public float minAcceY;

	public float newAcceZ;
	public float oldAcceZ;
	public float deltaAcceZ;
	public float maxAcceZ;
	public float minAcceZ;

	public float shakeThreshold;//摇晃的阈值


	public bool isBeginShaking;
	public bool isReadyToShake;
	
	void Awake()
	{
		isBeginShaking=true;
		isReadyToShake=true;

		Screen.sleepTimeout=SleepTimeout.NeverSleep;//防止手机休眠
	}



	
	void Update()
	{
		newAcceX=Input.acceleration.x;
		newAcceY=Input.acceleration.y;
		newAcceZ=Input.acceleration.z;
		if(Time.frameCount==1)
		{
			maxAcceX=newAcceX;
			maxAcceY=newAcceY;
			maxAcceZ=newAcceZ;

			minAcceX=newAcceX;
			minAcceY=newAcceY;
			minAcceZ=newAcceZ;
		}
		//获取最大值
	    maxAcceX=(newAcceX>=maxAcceX?newAcceX:maxAcceX);
		maxAcceY=(newAcceY>=maxAcceY?newAcceY:maxAcceY);
		maxAcceZ=(newAcceZ>=maxAcceZ?newAcceZ:maxAcceZ);

		//获取最小值
		minAcceX=(newAcceX>=minAcceX?minAcceX:newAcceX);
		minAcceY=(newAcceY>=minAcceY?minAcceY:newAcceY);
		minAcceZ=(newAcceZ>=minAcceZ?minAcceZ:newAcceZ);


		if(isReadyToShake)
		{
			if(isBeginShaking)
			{
				deltaAcceX=0;
				deltaAcceY=0;
				deltaAcceZ=0;
				isBeginShaking=false;
			}
			else
			{
				//每两帧之间的重力感应值增量
				deltaAcceX=newAcceX-oldAcceX;
				deltaAcceY=newAcceY-oldAcceY;
				deltaAcceZ=newAcceZ-oldAcceZ;
			}
		}

		oldAcceX=newAcceX;
		oldAcceY=newAcceY;
		oldAcceZ=newAcceZ;

		//当增量值大于阈值时,开始震动并随机播放音乐
		if((deltaAcceX>shakeThreshold)&&isReadyToShake)
		{
			ShakePhone();
		}

		//返回键退出游戏
		if(Input.GetKeyDown(KeyCode.Escape))
		{
			Application.Quit();
		}

		//按菜单键也可以震动并切换音乐
		if(Input.GetKeyDown(KeyCode.Menu))
		{
			ShakePhone();
		}
		
	}

	void ShakePhone()
	{
		PlayRandom();
		Handheld.Vibrate();
		isReadyToShake=false;
		Invoke("ResetShake",0.5f);
	}

	void PlayRandom()
	{
		int length=clips.Length;
		audio.clip=clips[Random.Range(0,length)];
		audio.Play();
	}

	void ResetShake()
	{
		isReadyToShake=true;
		isBeginShaking=true;
	}

	void OnGUI()
	{

		GUILayout.BeginArea(new Rect(Screen.width/2-250,Screen.height/2-250,500,500));
		GUILayout.BeginVertical();

		//显示重力感应在各个方向上的值
		GUILayout.Button("accex:"+Input.acceleration.x.ToString());
		GUILayout.Button("accey:"+Input.acceleration.y.ToString());
		GUILayout.Button("accez:"+Input.acceleration.z.ToString());

	    //显示各个方向所能达到的最大和最小值,多次摇晃后该值固定不变
		GUILayout.Button("maxAcceX:"+maxAcceX.ToString());
		GUILayout.Button("maxAcceY:"+maxAcceY.ToString());
		GUILayout.Button("maxAcceZ:"+maxAcceZ.ToString());
		GUILayout.Button("minAcceX:"+minAcceX.ToString());
		GUILayout.Button("minAcceY:"+minAcceY.ToString());
		GUILayout.Button("minAcceZ:"+minAcceZ.ToString());

		GUILayout.EndVertical();
		GUILayout.EndArea();
	}

}

 

核心思想是:检测两帧之间的重力感应增量值,若超过一定值,则执行相应操作。

还测量了重力感应在各个方向所能达到的最大值,在我的手机上,X,Y,Z方向所能达到的最大,最小值分别为2.000071,2.000071,1.999969和-1.999969,-1.999969,-2.000071

你可能感兴趣的:(Unity 摇一摇震动并随机播放音乐)