Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: aholla on May 09, 2014, 05:23:58 AM

Title: Destroying Prefab with UIRoot and creating a new one breaks anchors
Post by: aholla on May 09, 2014, 05:23:58 AM
Hi I have two menus which are prefabs that I am instantiating. Both have UI roots.

If i instantiate them individually they are fine but if I instantiate one and then destroy it and immediately instantiate the other one, the other one loses all its anchoring.

I presume this is a bug, It's as if the UIRoot does not have any dimensions hence no anchors, everything appears in the center and not relative to the left or right.

As a solution I will have to create a persistent UIRoot and attached my prefabs to it.

Title: Re: Destroying Prefab with UIRoot and creating a new one breaks anchors
Post by: ArenMook on May 09, 2014, 06:06:04 AM
Not a bug per say. Just how Unity works. When you destroy something via Object.Destroy, it's not destroyed immediately. It is still there, in your scene, until the next frame. It's why I had to write NGUITools.Destroy for destroying child objects within a transform. In your case though... anchoring does work, per say. NGUI starts up, searches for the proper camera, and finds the one that you've destroyed that's still present in the scene. You can likely fix it by modifying NGUITools.FindCameraForLayer function to this:
  1.         static public Camera FindCameraForLayer (int layer)
  2.         {
  3.                 int layerMask = 1 << layer;
  4.  
  5.                 Camera cam;
  6.  
  7.                 for (int i = 0; i < UICamera.list.size; ++i)
  8.                 {
  9.                         cam = UICamera.list.buffer[i].cachedCamera;
  10.                         if (cam && (cam.cullingMask & layerMask) != 0)
  11.                                 return cam;
  12.                 }
  13.  
  14.                 cam = Camera.main;
  15.                 if (cam && (cam.cullingMask & layerMask) != 0) return cam;
  16.  
  17.                 Camera[] cameras = NGUITools.FindActive<Camera>();
  18.  
  19.                 for (int i = 0, imax = cameras.Length; i < imax; ++i)
  20.                 {
  21.                         cam = cameras[i];
  22.                         if (cam && (cam.cullingMask & layerMask) != 0)
  23.                                 return cam;
  24.                 }
  25.                 return null;
  26.         }
Title: Re: Destroying Prefab with UIRoot and creating a new one breaks anchors
Post by: aholla on May 09, 2014, 11:30:46 AM
Brilliant, thanks for the info!