Author Topic: Destroying Prefab with UIRoot and creating a new one breaks anchors  (Read 1494 times)

aholla

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 40
    • View Profile
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.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
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.         }

aholla

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 40
    • View Profile
Brilliant, thanks for the info!