If in the same frame we set UICamera.selectedObject = objectA, then UICamera.selectedObject = objectB, the result will be that the coroutine ChangeSelection is called once at the end of frame and sets mCurrentSelection = objectA. I expect the result to be mCurrentSelection = objectB.
If in the same frame we set UICamera.selectedObject = objectA, then UICamera.selectedObject = null, the result will be that the coroutine ChangeSelection is called once at the end of frame and sets mCurrentSelection = objectA. I expect the result to be mCurrentSelection = null.
If in the same frame we set UICamera.selectedObject = objectA, then UICamera.selectedObject = null, and finally UICamera.selectedObject = objectB, the result will be that two ChangeSelection coroutines will be started and executed at the end of the frame. I am unfamiliar with whether or not the execution order on coroutines is defined but if it is not then the selected object will be undefined.
Am I right in assuming this is a bug?
My current temporary fix for this is:
static public GameObject selectedObject
{
get
{
return mCurrentSelection;
}
set
{
/*if (mNextSelection != null)
{
mNextSelection = value;
}
else */if (mCurrentSelection != value)
{
if (mCurrentSelection != null)
{
UICamera uicam = FindCameraForLayer(mCurrentSelection.layer);
if (uicam != null)
{
current = uicam;
currentCamera = uicam.mCam;
Notify(mCurrentSelection, "OnSelect", false);
if (uicam.useController || uicam.useKeyboard) Highlight(mCurrentSelection, false);
current = null;
}
}
mCurrentSelection = null;
mNextSelection = value;
if (mNextSelection != null)
{
UICamera uicam = FindCameraForLayer(mNextSelection.layer);
if (uicam != null) uicam.StartCoroutine(uicam.ChangeSelection(mNextSelection));
}
}
}
}
System.Collections.IEnumerator ChangeSelection (GameObject go)
{
yield return new WaitForEndOfFrame
();
if ( go == mNextSelection ) // added this if statement
{
mCurrentSelection = go;
mNextSelection = null;
if (mCurrentSelection != null)
{
current = this;
currentCamera = mCam;
if (useController || useKeyboard) Highlight(mCurrentSelection, true);
Notify(mCurrentSelection, "OnSelect", true);
current = null;
}
}
}
I am unaware of any cases that this does not solve, however, this can execute many unnecessary coroutines which could hurt performance. Do you think this is a reasonable temporary solution?