我的 gui 屏幕上有一个 2d 纹理。我想用手指触摸旋转这个纹理直到 +540 和 -540 度。超过这个限制它应该停止旋转。任何人都可以帮忙吗?
问问题
474 次
2 回答
1
我相信你不能旋转 GUI 纹理。我想做你想做的最好的方法是将一个平面作为一个孩子分配给面向它的相机,然后根据需要旋转那个平面。
于 2012-07-17T17:45:18.037 回答
0
public Texture2D steering;
public static TouchControl Instance;
public float angleRotate=0f;
public float carRotation=0f;
public int finger;
private Matrix4x4 savedMatrix;
private float steerAngleRad;
public float steerAngleDeg;
private float steerAngleRadTemp;
public float steerAngleDegTemp;
private float dx=0f;
private float dy=0f;
private int i;
private float newAngle =0f;
private float initAngle;
private float rotationLimit=540f;
public float secondWheelRot=0f;
private float preRotDeg=0f;
private float angleDiff=0f;
private bool clockWheel=false;
private bool onlyOnce=false;
private Vector2 steeringPos;
private Vector2 centre;
private bool inXRange=false;
private bool inYRange=false;
// private bool moving=false;
private float steeringWidth=0f;
private float steeringHeight=0f;
private float gobackSpeed=20f;
void Start()
{
Instance=this;
i=0;
finger=3;
steeringPos=new Vector2(Screen.width*0.025f,Screen.height*0.68f);
steeringWidth=Screen.width*0.12f;
steeringHeight=steeringWidth;
centre = new Vector2(steeringPos.x + steeringWidth / 2, steeringPos.y + steeringHeight / 2);
}
void Update()
{
if(Input.touchCount>0 && (!PauseButton.pauseFlag && !DamageBar.Instance.levelLost && !GameTimer.Instance.levelLost && !CommonIndicator.Instance.levelComplete))
{
for(i=0;i< Input.touchCount; ++i)
{
if(Input.GetTouch(i).position.x > 0f && Input.GetTouch(i).position.x < steeringWidth+Screen.width*0.1f)
{
inXRange=true;
}
else
{
inXRange=false;
}
if(Input.GetTouch(i).position.y > 0f && Input.GetTouch(i).position.y < Screen.height-(steeringPos.y))
{
inYRange=true;
}
else
{
inYRange=false;
}
///////////////if touch is in the range/////////////////////
if(inXRange && inYRange)
{
if(Input.GetTouch(i).phase==TouchPhase.Began)
{
finger=Input.touches[i].fingerId;
dx=Input.GetTouch(i).position.x-centre.x;
dy=Input.GetTouch(i).position.y-(Screen.height-centre.y);
steerAngleRadTemp=Mathf.Atan2(dy,dx);
steerAngleDegTemp=-steerAngleRadTemp*Mathf.Rad2Deg;
}
if((Input.GetTouch(i).phase==TouchPhase.Stationary || Input.GetTouch(i).phase==TouchPhase.Moved))
{
preRotDeg=newAngle;
if(preRotDeg<-180f && preRotDeg>-360f)
{
preRotDeg+=360f;
}
if(preRotDeg>180f && preRotDeg<360f)
{
preRotDeg-=360f;
}
dx=Input.GetTouch(i).position.x-centre.x;
dy=Input.GetTouch(i).position.y-(Screen.height-centre.y);
steerAngleRad=Mathf.Atan2(dy,dx);
steerAngleDeg=-steerAngleRad*Mathf.Rad2Deg;
angleRotate=steerAngleDeg-steerAngleDegTemp;
newAngle=angleRotate;
if(newAngle<-180f && newAngle>-360f)
{
newAngle+=360f;
}
if(newAngle>180f && newAngle<360f)
{
newAngle-=360f;
}
if(!onlyOnce)
{
onlyOnce=true;
if(newAngle>preRotDeg)
clockWheel=true;
else
clockWheel=false;
}
if(clockWheel && preRotDeg>0f && newAngle < 0f)
{
clockWheel=true;
}
else if(clockWheel && preRotDeg<0f && newAngle >0f)
{
clockWheel=true;
}
else if(!clockWheel && preRotDeg>0f && newAngle <0f)
{
clockWheel=false;
}
else if(!clockWheel && preRotDeg<0f && newAngle >0f)
{
clockWheel=false;
}
else if(newAngle>preRotDeg)
{
clockWheel=true;
}
else if(newAngle<preRotDeg)
{
clockWheel=false;
}
if(Mathf.Abs(newAngle)>Mathf.Abs(preRotDeg))
{
angleDiff=Mathf.Abs(newAngle)-Mathf.Abs(preRotDeg);
}
else
{
angleDiff=Mathf.Abs(preRotDeg)-Mathf.Abs(newAngle);
}
if(clockWheel)
{
carRotation+=angleDiff;
secondWheelRot+=angleDiff;
if(secondWheelRot>rotationLimit)
{
secondWheelRot=rotationLimit;
}
}
else
{
carRotation-=angleDiff;
secondWheelRot-=angleDiff;
if(secondWheelRot<-rotationLimit)
{
secondWheelRot=-rotationLimit;
}
}
}
// else
// {
// steeringToZero();
// }
if(Input.GetTouch(i).phase==TouchPhase.Ended)// && Input.touches[i].fingerId != AccControl.Instance.finger)
{
onlyOnce=false;
carRotation=0f;
angleRotate=0f;
newAngle=0f;
steeringToZero();
finger=3;
}
}
else if(!AccControl.Instance.onPadTouch && finger==3)
{
steeringToZero();
}
}
}
else if(Input.touchCount==0)
{
steeringToZero();
}
if(Input.touchCount==1 && AccControl.Instance.onPadTouch)
{
steeringToZero();
}
if (Input.GetKeyDown(KeyCode.Escape) && !PauseButton.pauseFlag)
{
Application.LoadLevel("MainMenu");
}
}
void steeringToZero()
{
if(secondWheelRot>0)
{
secondWheelRot-=gobackSpeed/(Input.touchCount+1);
}
else if(secondWheelRot<0)
{
secondWheelRot+=gobackSpeed/(Input.touchCount+1);
}
if(secondWheelRot<gobackSpeed && secondWheelRot>-gobackSpeed)
{
secondWheelRot=0f;
}
}
void OnGUI()
{
savedMatrix = GUI.matrix;
GUIUtility.RotateAroundPivot(carRotation,centre);
GUI.DrawTexture(new Rect(steeringPos.x,steeringPos.y, steeringWidth, steeringHeight), steering);
GUI.matrix = savedMatrix;
}
于 2013-10-25T06:32:54.180 回答