Unity手機搖一搖

項目中遇到手機搖一搖功能邊整理下:c#

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(); //手機的震動效果
        }
    }
}
相關文章
相關標籤/搜索