Unity_EasyAR实现微信摇一摇换贴图功能

来源:互联网 发布:手机站如何优化 编辑:程序博客网 时间:2024/06/08 06:12
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;

//摇晃的阈值需要自己去测试把数值给上
public class Shake : MonoBehaviour
{


public Texture[] textture;
public GameObject targetObject;
int index;
//public Text te;
//public Text te1;
//public Text te2;
//public Text te3;


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 Start()
{
ChangeTexture(textture[index]);
}


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();  
}  




if(Input.GetKeyDown(KeyCode.A))
{
Shakes();
targetObject.GetComponent<Renderer>().material.SetTexture("_MainTex",textture[0]);
}
}  


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


void ChangeTexture(Texture tex)
{
targetObject.GetComponent<Renderer>().material.SetTexture("_MainTex",tex);
}


void Shakes()
{
//te3.text=deltaAcceX+"...."+shakeThreshold;
if(targetObject==null) return;


index=(index+1)%textture.Length;
//te2.text=index+"///////"+textture[index].name;
ChangeTexture(textture[index]);
//te1.text=index+"....."+textture[index].name;
if(index==textture.Length-1)
{
Array.Reverse(textture);
index+=1;
}
}



void ResetShake()  
{  
isReadyToShake=true;  
isBeginShaking=true;  
}  
0 0
原创粉丝点击