My GUI buttons work strangely. When pressed on touchscreen, they only react when the finger releases the button. After the finger leaves the button, the button stays in a pressed position. When I double tap the button, it performs the way I want it to which is as a single press and release. My question is, how do I make the button react as if it has been pressed and released when I touch it once?
Here is the code:
using UnityEngine;
using System.Collections;
public class SnakeHead : MonoBehaviour
{
public float fTurnRate = 200; // Degrees of turning per second
public float fSpeed = 9.5f; // Units per second of movement
public Tail var;
public bool dead = false;
public bool FlyUp = false;
public bool FlyDown = false;
public bool debugMode = false;
public bool centerButton = false;
public Texture2D buttonImage = null;
public Texture2D otherImage = null;
public Texture2D currentImage = null;
public string buttonLabel = "Fly Up";
public string otherbuttonLabel = "Fly Down";
private Rect currentRect = new Rect(15, 15, 150, 150);
private Rect otherRect = new Rect(15, 300, 150, 150);
//public string title = string.Empty;
public GUISkin skin = null;
public bool ButtonOnPress(Rect currentRect, Texture currentImage)
{
GUI.DrawTexture (currentRect, currentImage);
if( Input.touchCount == 0)
{
return false;
}
foreach (Touch touch in Input.touches)
{
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
{
if(currentRect.Contains(touch.position))
{
return true;
}
}
}
return false;
}
void Awake()
{
FlyUp = false;
FlyDown = false;
}
public void OnGUI()
{
if (debugMode || Application.isPlaying)
{
if (centerButton)
{
currentRect.x = (Screen.width * 0.5f) - (currentRect.width * 0.5f);
currentRect.y = (Screen.height * 0.5f) - (currentRect.height * 0.5f);
otherRect.x = (Screen.width * 0.5f) - (otherRect.width * 0.5f);
otherRect.y = (Screen.height * 0.5f) - (otherRect.height * 0.5f);
}
if (GUI.Button(currentRect, buttonLabel))
{
FlyUp = true;
FlyDown = false;
}
if (GUI.Button(otherRect, otherbuttonLabel))
{
FlyDown = true;
FlyUp = false;
}
}
}
void Update()
{
if (FlyUp)
transform.Rotate (Vector3.forward * fTurnRate * Time.deltaTime);
if (FlyDown)
transform.Rotate (-Vector3.forward * fTurnRate * Time.deltaTime);
transform.localPosition = transform.localPosition + transform.right * fSpeed * Time.deltaTime;
var.TailUpdate();
}
}
Sorry 767_2, but that uses JS and after trying it and researching how to access my C# code from the JS code, I think that moving my files into different folders to get it to work is a little inefficient. I appreciate the response though.
– Sp33dy_Sn1pai interpreted to c#
– 767_2