Author Topic: UIDraggableCamera lifecycle (wrong?)  (Read 2752 times)

Antitribu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
UIDraggableCamera lifecycle (wrong?)
« on: June 24, 2014, 09:42:21 AM »
Hey-yo, forum.
I'm using NGUI 3.6.1, and have stumbled upon a little problem.

Most of my GUI is constructed on-the-fly, using premade prefabs and dynamic instantiation (using AddComponent).

Yet, UIDraggableCamera effectively forbids such behaviour, by making a check for rootForBounds inside Awake:

  1.         void Awake ()
  2.         {
  3.                 mCam = camera;
  4.                 mTrans = transform;
  5.  
  6.                 if (rootForBounds == null)
  7.                 {
  8.                         Debug.LogError(NGUITools.GetHierarchy(gameObject) + " needs the 'Root For Bounds' parameter to be set", this);
  9.                         enabled = false;
  10.                 }
  11.         }
  12.  

making dynamic instantiation such as the one below throw an error:

  1. UIDraggableCamera draggableCamera = DraggableCamera.AddComponent<UIDraggableCamera>();
  2. draggableCamera.rootForBounds = SomeOtherGOInstance.transform;
  3. draggableCamera.dragEffect = UIDragObject.DragEffect.None;
  4.  

so, i need an explicit .enable call to make things right (it won't remove the LogError, ofc).
  1. draggableCamera.enabled = true;
  2.  

The question is: why would UIDraggableCamera need such a check inside Awake?
Judging by the implementation, the only unchecked call to rootForBounds is inside Update.

... So moving the check to Start would fix things?

P.S. I've checked the forum for similar threads, and found and old one, http://www.tasharen.com/forum/index.php?topic=1144.msg5896 , that doesn't have any point.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIDraggableCamera lifecycle (wrong?)
« Reply #1 on: June 25, 2014, 05:09:17 AM »
UIDraggableCamera? What are you using it for? It's a very old legacy component. I don't remember a single instance when I've ever had to use it outside that one scroll view example that was created before clipping was added to panels.

You can move all the code in Awake() into Start() instead safely.

Antitribu

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: UIDraggableCamera lifecycle (wrong?)
« Reply #2 on: June 26, 2014, 10:42:59 AM »
I'm making a scrollable map, and, for now, I find a "ScrollView via secondary camera" approach more suitable for my needs - so I'm making use of UIDraggableCamera+UIViewPort.

As for why I'm not using a plain clipped scroll view: the component ("Map") that's rendered in the viewport, could potentially contain 3D objects in perspective.