Author Topic: NGUI Renderers  (Read 5601 times)

jaised

  • Guest
NGUI Renderers
« on: August 24, 2012, 10:01:36 AM »
Still working on the tutorial described here: http://www.tasharen.com/forum/index.php?topic=1466.0

I am trying to point to NGUI objects and I was wondering if they had a renderer? I need access to the bounds of the sprite (http://docs.unity3d.com/Documentation/ScriptReference/Bounds.html) so I can adjust the arrow offset and have them point to the object correctly. Maybe there is a way to do this, but I can't seem to find it. Any suggestions would be great! Thanks for the help, again.

PhilipC

  • Guest
Re: NGUI Renderers
« Reply #1 on: August 24, 2012, 11:12:40 AM »
The sprites dont have there own renderer no. All sprites that are on a single draw call share geometry and the renderer (go to the UIPanel and change the Debug to geometry and you will see what i mean). In order to get the bounds you will have to look at using a combination of relativeSize and pivotOffset. Take a look at UIAnchor.cs, in the update function you will see a else if(widgetContainer != null). This does the calculations to determine the "bounds" of the widgetContainer so you can do something similar.

Your other option is to just use UIAnchor itself. If you assign you arrow a anchor which points to the correct object it will then stay attached to that object. If you then wanted to change what the arrow was pointing at you can just change the widgetContainer for the arrows UIAnchor.

jaised

  • Guest
Re: NGUI Renderers
« Reply #2 on: August 24, 2012, 12:24:03 PM »
I am not quite sure what you mean. I am also looking at the UIAnchor.cs and that line is no where in it:

  1. //----------------------------------------------
  2. //            NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2012 Tasharen Entertainment
  4. //----------------------------------------------
  5.  
  6. using UnityEngine;
  7.  
  8. /// <summary>
  9. /// This script can be used to anchor an object to the side of the screen,
  10. /// or scale an object to always match the dimensions of the screen.
  11. /// </summary>
  12.  
  13. [ExecuteInEditMode]
  14. [AddComponentMenu("NGUI/UI/Anchor")]
  15. public class UIAnchor : MonoBehaviour
  16. {
  17.         public enum Side
  18.         {
  19.                 BottomLeft,
  20.                 Left,
  21.                 TopLeft,
  22.                 Top,
  23.                 TopRight,
  24.                 Right,
  25.                 BottomRight,
  26.                 Bottom,
  27.                 Center,
  28.         }
  29.  
  30.         public Camera uiCamera = null;
  31.         public Side side = Side.Center;
  32.         public bool halfPixelOffset = true;
  33.         public float depthOffset = 0f;
  34.         public Vector2 relativeOffset = Vector2.zero;
  35.  
  36.         // Stretching is now done by a separate script -- UIStretch, as of version 1.90.
  37.         [HideInInspector][SerializeField] bool stretchToFill = false;
  38.  
  39.         Transform mTrans;
  40.         bool mIsWindows = false;
  41.  
  42.         /// <summary>
  43.         /// Cache the transform.
  44.         /// </summary>
  45.  
  46.         void Awake () { mTrans = transform; }
  47.  
  48.         /// <summary>
  49.         /// Legacy support.
  50.         /// </summary>
  51.  
  52.         void Start ()
  53.         {
  54.                 if (stretchToFill)
  55.                 {
  56.                         stretchToFill = false;
  57.  
  58.                         UIStretch stretch = gameObject.AddComponent<UIStretch>();
  59.                         stretch.style = UIStretch.Style.Both;
  60.                         stretch.uiCamera = uiCamera;
  61.                 }
  62.         }
  63.  
  64.         /// <summary>
  65.         /// Automatically find the camera responsible for drawing the widgets under this object.
  66.         /// </summary>
  67.  
  68.         void OnEnable ()
  69.         {
  70.                 mIsWindows = (Application.platform == RuntimePlatform.WindowsPlayer ||
  71.                         Application.platform == RuntimePlatform.WindowsWebPlayer ||
  72.                         Application.platform == RuntimePlatform.WindowsEditor);
  73.  
  74.                 if (uiCamera == null) uiCamera = NGUITools.FindCameraForLayer(gameObject.layer);
  75.         }
  76.  
  77.         /// <summary>
  78.         /// Anchor the object to the appropriate point.
  79.         /// </summary>
  80.  
  81.         void Update ()
  82.         {
  83.                 if (uiCamera != null)
  84.                 {
  85.                         Rect rect = uiCamera.pixelRect;
  86.                         float cx = (rect.xMin + rect.xMax) * 0.5f;
  87.                         float cy = (rect.yMin + rect.yMax) * 0.5f;
  88.                         Vector3 v = new Vector3(cx, cy, depthOffset);
  89.  
  90.                         if (side != Side.Center)
  91.                         {
  92.                                 if (side == Side.Right || side == Side.TopRight || side == Side.BottomRight)
  93.                                 {
  94.                                         v.x = rect.xMax;
  95.                                 }
  96.                                 else if (side == Side.Top || side == Side.Center || side == Side.Bottom)
  97.                                 {
  98.                                         v.x = cx;
  99.                                 }
  100.                                 else
  101.                                 {
  102.                                         v.x = rect.xMin;
  103.                                 }
  104.  
  105.                                 if (side == Side.Top || side == Side.TopRight || side == Side.TopLeft)
  106.                                 {
  107.                                         v.y = rect.yMax;
  108.                                 }
  109.                                 else if (side == Side.Left || side == Side.Center || side == Side.Right)
  110.                                 {
  111.                                         v.y = cy;
  112.                                 }
  113.                                 else
  114.                                 {
  115.                                         v.y = rect.yMin;
  116.                                 }
  117.                         }
  118.  
  119.                         float screenWidth  = rect.width;
  120.                         float screenHeight = rect.height;
  121.  
  122.                         v.x += relativeOffset.x * screenWidth;
  123.                         v.y += relativeOffset.y * screenHeight;
  124.  
  125.                         if (uiCamera.orthographic)
  126.                         {
  127.                                 v.x = Mathf.RoundToInt(v.x);
  128.                                 v.y = Mathf.RoundToInt(v.y);
  129.  
  130.                                 if (halfPixelOffset && mIsWindows)
  131.                                 {
  132.                                         v.x -= 0.5f;
  133.                                         v.y += 0.5f;
  134.                                 }
  135.                         }
  136.  
  137.                         // Convert from screen to world coordinates, since the two may not match (UIRoot set to manual size)
  138.                         v = uiCamera.ScreenToWorldPoint(v);
  139.  
  140.                         // Wrapped in an 'if' so the scene doesn't get marked as 'edited' every frame
  141.                         if (mTrans.position != v) mTrans.position = v;
  142.                 }
  143.         }
  144. }

PhilipC

  • Guest
Re: NGUI Renderers
« Reply #3 on: August 24, 2012, 12:41:12 PM »
In that case your are a couple version behind  :).

  1. else if (widgetContainer != null)
  2. {
  3.         // Widget is used -- use its bounds as the container's bounds
  4.         Transform t = widgetContainer.cachedTransform;
  5.         Vector3 ls = t.localScale;
  6.         Vector3 lp = t.localPosition;
  7.  
  8.         Vector2 size = widgetContainer.relativeSize;
  9.         Vector3 offset = widgetContainer.pivotOffset;
  10.         offset.y -= size.y;
  11.  
  12.         offset.x *= (size.x * ls.x);
  13.         offset.y *= (size.y * ls.y);
  14.  
  15.         rect.x = lp.x + offset.x;
  16.         rect.y = lp.y + offset.y;
  17.         rect.width = size.x * ls.x;
  18.         rect.height = size.y * ls.y;
  19. }
  20.  

I would suggest you update if only to get the newest UIAnchor as i do feel it will do what you require.


jaised

  • Guest
Re: NGUI Renderers
« Reply #4 on: August 24, 2012, 12:41:22 PM »
Since I am adding a panel to whatever object I want to bring forward, would using ConstrainTargetToBounds work to get the bounds?

jaised

  • Guest
Re: NGUI Renderers
« Reply #5 on: August 24, 2012, 12:44:52 PM »
After digger further in, won't
  1. mBounds = NGUIMath.CalculateRelativeWidgetBounds(mPanel.cachedTransform, target);
, more specifically, help out?

PhilipC

  • Guest
Re: NGUI Renderers
« Reply #6 on: August 24, 2012, 12:48:16 PM »
Sorry yes i forgot about that method, it should do what you need.