I have this Clicked behaviour attached to a camera:
public class Clicked : MonoBehaviour {
//stores the object that received MouseDown for consumption by the iterator
private RaycastHit originalHit;
void Update () {
if (!Input.GetMouseButtonDown(0)){
//mouse not being clicked
return;
}
var camera = Camera.main;
if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out originalHit, 100)){
print ("no object being clicked by mouse");
return;
}
var eventsBehaviour = originalHit.transform.GetComponent<SubscribedEvents>();
if(eventsBehaviour == null
|| !(eventsBehaviour is ISubscribeClicked)){
print ("no event subscriptions or does not subscribe to Clicked event");
return;
}
StartCoroutine(WaitOnUp(originalHit));
}
//waits on the mouse to be released on this object so that it only acts
//upon a mouse click that was completely on this object
IEnumerator WaitOnUp(RaycastHit hit){
while (!Input.GetMouseButtonUp(0)){
yield return 0;
}
if(!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 100)){
print ("MouseUp not on an object");
}
else if(hit.GetHashCode () != originalHit.GetHashCode ()){
print("MouseUp on a different object");
}
else{
//object received MouseDown and then MouseUp, so fire its OnClicked event
print ("MouseUp on original object");
(originalHit.transform.GetComponent<SubscribedEvents>() as ISubscribeClicked).OnClicked();
}
}
}
However, if I quickly move the mouse to a different object after ensuring that MouseUp has occurred on the original object, I will see “MouseUp on a different object”. Due to seeing this, it seems like calling
yield return 0
causes the program to not re-enter into the iterator every frame, as in it just skips some frames before re-entering. Is that true?