Hey guys
I'm probably reinventing the wheel here, but I'm trying to write a component for tracking a given UIRect in screen space to a GameObject in world space.

I'm basically trying to abstract the process of setting a UIRect to Unified anchor mode and assigning a camera. Unfortunately my panels still don't seem to move away from the center of the screen.
Apologies for the code dump, I promise it isn't complicated!
The component:
[ExecuteInEditMode]
/// <summary>
/// Anchor class which anchors a NGUI object to a GameObject
/// </summary>
public class AnchorNguiToGameObject : AthgoMonoBehaviour
{
#region Properties
private UIRect m_Target;
/// <summary>
/// Gets or sets the UIRect we are anchoring.
/// </summary>
/// <value>The target.</value>
public UIRect target
{
get
{
return this.m_Target;
}
set
{
this.SetTarget(value);
}
}
/// <summary>
/// Gets or sets the target camera.
/// </summary>
/// <value>The target camera.</value>
public Camera targetCamera
{
get
{
return this.GetTargetCamera();
}
set
{
this.SetTargetCamera(value);
}
}
#endregion
#region Methods
/// <summary>
/// Sets the target that is being anchored to the GameObject.
/// </summary>
/// <param name="target">Target.</param>
public void SetTarget(UIRect target)
{
if (target == this.m_Target)
return;
this.m_Target = target;
if (this.m_Target is UIPanel
) (this.m_Target as UIPanel).clipping = UIDrawCall.Clipping.ConstrainButDontClip;
foreach (UIRect.AnchorPoint anchor in this.GetAnchorPoints())
anchor.target = this.transform;
}
/// <summary>
/// Returns the camera that is used to track the UIRect.
/// </summary>
/// <returns>The target camera.</returns>
public Camera GetTargetCamera()
{
foreach (UIRect.AnchorPoint anchor in this.GetAnchorPoints())
return anchor.targetCam;
return null;
}
/// <summary>
/// Sets the camera that is used to track the UIRect.
/// </summary>
/// <param name="camera">Camera.</param>
public void SetTargetCamera(Camera camera)
{
if (this.m_Target == null)
throw new Exception
("Can't set target Camera. No target!");
foreach (UIRect.AnchorPoint anchor in this.GetAnchorPoints())
anchor.targetCam = camera;
}
/// <summary>
/// Returns the list of anchor points for the current target.
/// </summary>
/// <returns>The anchor points.</returns>
public List<UIRect.AnchorPoint> GetAnchorPoints()
{
List
<UIRect
.AnchorPoint> anchorPoints
= new List
<UIRect
.AnchorPoint>();
if (this.m_Target != null)
{
anchorPoints.Add(this.m_Target.bottomAnchor);
anchorPoints.Add(this.m_Target.topAnchor);
anchorPoints.Add(this.m_Target.leftAnchor);
anchorPoints.Add(this.m_Target.rightAnchor);
}
return anchorPoints;
}
#endregion
}
The result:

It's a coincidence that the first anchor is above the sphere:

But the second and third anchors are to the right and below the sphere:


My first question:
Am I missing anything important from my component to get a given UIRect to reliably anchor to the GameObject?
Second question:
I suspect the reason the UIRects are stuck in the center of the screen is because they are parented to the center anchor. If this is the case, what should they be parented to? It doesn't make sense to parent them to my tracking component since I don't want them moving in world space.
As always, please let me know if my hierarchy is garbage. I think the hierarchy stuff is the hardest part for me to grasp with NGUI.
Thanks,
Ves