IEnumerator coHandleButtonPress(int fingerId)
{
buttonDown = true; // inhibit processing in Update()
bool buttonPressed = true; // the button is currently being pressed
Vector3 defaultScale = transform.localScale;
// Button has been pressed for the first time, cursor/finger is still on it
if (targetScale != 1.0f)
{
// Only do this when the scale is actually enabled, to save one frame of latency when not needed
yield return StartCoroutine( coScale(defaultScale, 1.0f, targetScale) );
}
PlaySound(buttonDownSound);
if (buttonDownSpriteId != -1)
sprite.spriteId = buttonDownSpriteId;
if (ButtonDownEvent != null)
ButtonDownEvent(this);
while (true)
{
Vector3 cursorPosition = Vector3.zero;
bool cursorActive = true;
// slightly akward arrangement to keep exact backwards compatibility
#if !UNITY_FLASH
if (Input.multiTouchEnabled)
{
bool found = false;
for (int i = 0; i < Input.touchCount; ++i)
{
Touch touch = Input.GetTouch(i);
if (touch.fingerId == fingerId)
{
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
break; // treat as not found
cursorPosition = touch.position;
found = true;
}
}
if (!found) cursorActive = false;
}
else
#endif
{
if (!Input.GetMouseButton(0))
cursorActive = false;
cursorPosition = Input.mousePosition;
}
// user is no longer pressing mouse or no longer touching button
if (!cursorActive)
break;
Ray ray = viewCamera.ScreenPointToRay(cursorPosition);
RaycastHit hitInfo;
bool colliderHit = collider.Raycast(ray, out hitInfo, 1.0e8f);
if (buttonPressed && !colliderHit)
{
if (targetScale != 1.0f)
{
// Finger is still on screen / button is still down, but the cursor has left the bounds of the button
yield return StartCoroutine( coScale(defaultScale, targetScale, 1.0f) );
}
PlaySound(buttonUpSound);
if (buttonUpSpriteId != -1)
sprite.spriteId = buttonUpSpriteId;
if (ButtonUpEvent != null)
ButtonUpEvent(this);
buttonPressed = false;
}
else if (!buttonPressed & colliderHit)
{
if (targetScale != 1.0f)
{
// Cursor had left the bounds before, but now has come back in
yield return StartCoroutine( coScale(defaultScale, 1.0f, targetScale) );
}
PlaySound(buttonDownSound);
if (buttonDownSpriteId != -1)
sprite.spriteId = buttonDownSpriteId;
if (ButtonDownEvent != null)
ButtonDownEvent(this);
buttonPressed = true;
}
if (buttonPressed && ButtonAutoFireEvent != null)
{
ButtonAutoFireEvent(this);
}
yield return 0;
}
if (buttonPressed)
{
if (targetScale != 1.0f)
{
// Handle case when cursor was in bounds when the button was released / finger lifted
yield return StartCoroutine( coScale(defaultScale, targetScale, 1.0f) );
}
PlaySound(buttonPressedSound);
if (buttonPressedSpriteId != -1)
sprite.spriteId = buttonPressedSpriteId;
if (targetObject)
{
targetObject.SendMessage(messageName);
}
if (ButtonUpEvent != null)
ButtonUpEvent(this);
if (ButtonPressedEvent != null)
ButtonPressedEvent(this);
// Button may have been deactivated in ButtonPressed / Up event
// Don't wait in that case
#if UNITY_3_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5 || UNITY_3_6 || UNITY_3_7 || UNITY_3_8 || UNITY_3_9
if (gameObject.active)
#else
if (gameObject.activeInHierarchy)
#endif
{
yield return StartCoroutine(LocalWaitForSeconds(pressedWaitTime));
}
if (buttonUpSpriteId != -1)
sprite.spriteId = buttonUpSpriteId;
}
buttonDown = false;
}