Author Topic: Bug: Issues arise when setting UICamera.selectedObject multiple times per frame  (Read 1835 times)

Holdwick

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
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:
  1.         static public GameObject selectedObject
  2.         {
  3.                 get
  4.                 {
  5.                         return mCurrentSelection;
  6.                 }
  7.                 set
  8.                 {
  9.                         /*if (mNextSelection != null)
  10.                         {
  11.                                 mNextSelection = value;
  12.                         }
  13.                         else */if (mCurrentSelection != value)
  14.                         {
  15.                                 if (mCurrentSelection != null)
  16.                                 {
  17.                                         UICamera uicam = FindCameraForLayer(mCurrentSelection.layer);
  18.  
  19.                                         if (uicam != null)
  20.                                         {
  21.                                                 current = uicam;
  22.                                                 currentCamera = uicam.mCam;
  23.                                                 Notify(mCurrentSelection, "OnSelect", false);
  24.                                                 if (uicam.useController || uicam.useKeyboard) Highlight(mCurrentSelection, false);
  25.                                                 current = null;
  26.                                         }
  27.                                 }
  28.  
  29.                                 mCurrentSelection = null;
  30.                                 mNextSelection = value;
  31.  
  32.                                 if (mNextSelection != null)
  33.                                 {
  34.                                         UICamera uicam = FindCameraForLayer(mNextSelection.layer);
  35.                                         if (uicam != null) uicam.StartCoroutine(uicam.ChangeSelection(mNextSelection));
  36.                                 }
  37.                         }
  38.                 }
  39.         }
  40.  
  41.         System.Collections.IEnumerator ChangeSelection (GameObject go)
  42.         {
  43.                 yield return new WaitForEndOfFrame();
  44.  
  45.                 if ( go == mNextSelection ) // added this if statement
  46.                 {
  47.                         mCurrentSelection = go;
  48.                         mNextSelection = null;
  49.  
  50.                         if (mCurrentSelection != null)
  51.                         {
  52.                                 current = this;
  53.                                 currentCamera = mCam;
  54.                                 if (useController || useKeyboard) Highlight(mCurrentSelection, true);
  55.                                 Notify(mCurrentSelection, "OnSelect", true);
  56.                                 current = null;
  57.                         }
  58.                 }
  59.         }
  60.  

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?  8)
Kyle "Goin' for it since the early 90s" Holdwick
Developer | REFRACT
@kyleholdwick

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Yup, this was reported yesterday and has been fixed in the Pro repository. You'll see this fix live in the next update, thanks.