Created
July 22, 2013 03:28
-
-
Save AlexTiTanium/6051128 to your computer and use it in GitHub Desktop.
Yield example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using System.Collections; | |
| public class GuiTouchListener : RuntimeInteractiveObject | |
| { | |
| const float SWIPE_MIN_LENGTH = 25.0f; | |
| const float TOUCH_BEGIN_DELAY = 0.03f; | |
| // Events methods names | |
| const string EVENT_TOUCHE_BEGIN = "OnGuiTouchBegin"; | |
| const string EVENT_TOUCHE_DISMISS = "OnGuiTouchDismiss"; | |
| const string EVENT_TOUCHE_END = "OnGuiTouchEnd"; | |
| //************************************************************************** | |
| // | |
| // Runtime Interactive | |
| // | |
| //************************************************************************** | |
| //************************************************************************** | |
| // On touch begin | |
| public bool HandleTouchBegin(Vector3 position) | |
| { | |
| if (collide2DPoint(position)) { | |
| // StartCoroutine return immediately, without any delay, GuiTouchBegin will be called without delay too | |
| // But if "GuiTouchBegin" have "yield" expresion "StartCoroutine" will stuck on this expression until "yield retrun null" | |
| StartCoroutine(GuiTouchBegin(position, TOUCH_BEGIN_DELAY)); | |
| // Important! No any delay return immediately | |
| return true; | |
| } | |
| return false; | |
| } | |
| //************************************************************************** | |
| // On touch move | |
| public bool HandleTouchMove(Vector3 position) | |
| { | |
| if (collide2DPoint(position)) | |
| { | |
| Vector3 delta = position - tochBeginPosition; | |
| if (delta.magnitude > SWIPE_MIN_LENGTH) | |
| { | |
| GuiTouchDismiss(); | |
| } | |
| } else | |
| { | |
| GuiTouchDismiss(); | |
| } | |
| return true; | |
| } | |
| //************************************************************************** | |
| // On touch end | |
| public bool HandleTouchEnd(Vector3 position) | |
| { | |
| if (collide2DPoint(position)) | |
| { | |
| GuiTouchEnded(position); | |
| return true; | |
| } | |
| return false; | |
| } | |
| //************************************************************************** | |
| // | |
| // Private | |
| // | |
| //************************************************************************** | |
| private Vector3 tochBeginPosition = Vector3.zero; | |
| private bool doActionAtTheEnd = false; | |
| private bool dismissToucheBegin = false; | |
| //************************************************************************** | |
| // | |
| // Send events to the game object components | |
| // | |
| //************************************************************************** | |
| private IEnumerator GuiTouchBegin(Vector3 position, float delay) | |
| { | |
| // Prepare object state | |
| dismissToucheBegin = false; | |
| doActionAtTheEnd = true; | |
| float pauseEndTime = Time.realtimeSinceStartup + delay; // Calculate end time | |
| // This while will do @delay@ seconds | |
| while(Time.realtimeSinceStartup < pauseEndTime) { | |
| yield return 0; | |
| } | |
| // When "yield return new WaitForSecond" after @delay seconds will return yield break, this line begin execute | |
| if(dismissToucheBegin) { | |
| yield break; // It dismiss code execution below, "yield break" not "return" because this method must return IEnumerator | |
| } | |
| tochBeginPosition = position; | |
| SendMessage(EVENT_TOUCHE_BEGIN, position, SendMessageOptions.RequireReceiver); | |
| } | |
| //-------------------------------------------------------------------------- | |
| private void GuiTouchDismiss() | |
| { | |
| doActionAtTheEnd = false; | |
| dismissToucheBegin = true; | |
| SendMessage(EVENT_TOUCHE_DISMISS, SendMessageOptions.RequireReceiver); | |
| } | |
| //-------------------------------------------------------------------------- | |
| private void GuiTouchEnded(Vector3 position) | |
| { | |
| dismissToucheBegin = true; | |
| if (!doActionAtTheEnd) return; | |
| SendMessage(EVENT_TOUCHE_END, position, SendMessageOptions.RequireReceiver); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment