Author Topic: Relative anchors and new UITable directions  (Read 3389 times)

chrome.alex

  • Guest
Relative anchors and new UITable directions
« on: November 06, 2012, 03:13:16 AM »
So, i think about relative anchor position by the uisprite. Its need for make window works correct, change their dimensions and now it's not need to reposition all sprites on it.

And i add UITable new directions. Now its able to view as RightDown, RightUp, LeftDown, LeftUp (and Down/Up if you already use them as RightDown/RightUp):

UITable.cs - find RepositionVariableSize void function (you need to change the second FOR):
  1.                 for (int i = 0, imax = children.Count; i < imax; ++i)
  2.                 {
  3.                         Transform t = children[i];
  4.                         Bounds b = bounds[y, x];
  5.                         Bounds br = boundsRows[x];
  6.                         Bounds bc = boundsCols[y];
  7.  
  8.                         Vector3 pos = t.localPosition;
  9.                        
  10.                         float yset;
  11.                        
  12.                         if (direction == Direction.Down || direction == Direction.RightDown || direction == Direction.LeftDown)
  13.                         {
  14.                                 pos.x = xOffset + b.extents.x - b.center.x;
  15.                                 pos.y = -yOffset - b.extents.y - b.center.y;
  16.                                 yset = (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  17.                                 if (direction == Direction.LeftDown) {
  18.                                         pos.y -= yset;
  19.                                         pos.x = -xOffset - b.extents.x - b.center.x;
  20.                                         pos.x -= b.min.x - br.min.x + padding.x;
  21.                                 } else {
  22.                                         pos.y += yset;
  23.                                         pos.x += b.min.x - br.min.x + padding.x;
  24.                                 }
  25.                                
  26.                                
  27.                         }
  28.                         else if (direction == Direction.Up || direction == Direction.RightUp || direction == Direction.LeftUp)
  29.                         {
  30.                                 pos.x = xOffset + b.extents.x - b.center.x;
  31.                                 pos.y = yOffset + b.extents.y - b.center.y;
  32.                                 yset = (b.max.y - b.min.y - bc.max.y + bc.min.y) * 0.5f - padding.y;
  33.                                 if (direction == Direction.LeftUp) {
  34.                                         pos.y -= yset;
  35.                                         pos.x = -xOffset - b.extents.x - b.center.x;
  36.                                         pos.x -= b.min.x - br.min.x + padding.x;
  37.                                 } else {
  38.                                         pos.y += yset;
  39.                                         pos.x += b.min.x - br.min.x + padding.x;
  40.                                 }
  41.                                
  42.                         }
  43.  
  44.                         xOffset += br.max.x - br.min.x + padding.x * 2f;
  45.                        
  46.                         t.localPosition = pos;
  47.  
  48.                         if (++x >= columns && columns > 0)
  49.                         {
  50.                                 x = 0;
  51.                                 ++y;
  52.  
  53.                                 xOffset = 0f;
  54.                                 yOffset += bc.size.y + padding.y * 2f;
  55.                         }
  56.                 }
  57.  

And add some new directions:
  1.  
  2.         public enum Direction
  3.         {
  4.                 Down,
  5.                 Up,
  6.                 RightDown,
  7.                 RightUp,
  8.                 LeftDown,
  9.                 LeftUp,
  10.         }
  11.  
----------------------
Now, lets change UIAnchor.cs (you need to replace Update function)
  1.  
  2.         void Update ()
  3.         {
  4.                 if (uiCamera != null || uiSprite != null)
  5.                 {
  6.                        
  7.                         Vector3 v;
  8.                         Vector3 vScale;
  9.                         Rect rect;
  10.                        
  11.                         if (uiSprite != null) {
  12.                                
  13.                                 rect = new Rect();
  14.                                
  15.                                 vScale = uiSprite.cachedTransform.localScale;
  16.                                 v = uiCamera.WorldToScreenPoint(uiSprite.cachedTransform.position);
  17.                                 rect.x = v.x - vScale.x / 2.0f;
  18.                                 rect.y = v.y - vScale.y / 2.0f;
  19.                                
  20.                                 rect.width = vScale.x;
  21.                                 rect.height = vScale.y;
  22.                                
  23.                         } else {
  24.                                
  25.                                 rect = uiCamera.pixelRect;
  26.                                
  27.                         }
  28.                        
  29.                         float cx = (rect.xMin + rect.xMax) * 0.5f;
  30.                         float cy = (rect.yMin + rect.yMax) * 0.5f;
  31.                         v = new Vector3(cx, cy, depthOffset);
  32.                        
  33.                         if (side != Side.Center)
  34.                         {
  35.                                 if (side == Side.Right || side == Side.TopRight || side == Side.BottomRight)
  36.                                 {
  37.                                         v.x = rect.xMax;
  38.                                 }
  39.                                 else if (side == Side.Top || side == Side.Center || side == Side.Bottom)
  40.                                 {
  41.                                         v.x = cx;
  42.                                 }
  43.                                 else
  44.                                 {
  45.                                         v.x = rect.xMin;
  46.                                 }
  47.  
  48.                                 if (side == Side.Top || side == Side.TopRight || side == Side.TopLeft)
  49.                                 {
  50.                                         v.y = rect.yMax;
  51.                                 }
  52.                                 else if (side == Side.Left || side == Side.Center || side == Side.Right)
  53.                                 {
  54.                                         v.y = cy;
  55.                                 }
  56.                                 else
  57.                                 {
  58.                                         v.y = rect.yMin;
  59.                                 }
  60.                         }
  61.  
  62.                         float screenWidth  = rect.width;
  63.                         float screenHeight = rect.height;
  64.  
  65.                         v.x += relativeOffset.x * screenWidth;
  66.                         v.y += relativeOffset.y * screenHeight;
  67.  
  68.                         if (uiCamera != null && uiCamera.orthographic)
  69.                         {
  70.                                 v.x = Mathf.RoundToInt(v.x);
  71.                                 v.y = Mathf.RoundToInt(v.y);
  72.  
  73.                                 if (halfPixelOffset && mIsWindows)
  74.                                 {
  75.                                         v.x -= 0.5f;
  76.                                         v.y += 0.5f;
  77.                                 }
  78.                         }
  79.                        
  80.                         // Convert from screen to world coordinates, since the two may not match (UIRoot set to manual size)
  81.                         if (uiCamera != null) v = uiCamera.ScreenToWorldPoint(v);
  82.                        
  83.                         // Wrapped in an 'if' so the scene doesn't get marked as 'edited' every frame
  84.                         if (mTrans.position != v) mTrans.position = v;
  85.                 }
  86.         }
  87.  
And of course add some variable:
  1.         public UISprite uiSprite = null;
  2.  
Now you can add some background as UISprite to anchor element and all elements in that anchor will set their positions according on it.

Hope, it helps for somebody :)