Author Topic: HUD Text Camera Issue  (Read 8976 times)

Darkness

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 90
    • View Profile
HUD Text Camera Issue
« on: December 31, 2013, 05:59:23 PM »
I have been having some issues with HUD Text where the text wouldn't be in the right place when the object was destroyed and re-instantiated.  After doing some digging, I found out that it had to do with NGUI providing a different camera whenever it searched for one. 

  1. gameCamera = NGUITools.FindCameraForLayer(target.gameObject.layer);

Would come back with a different camera than the main camera which would mean the text would display in odd positions.  I had to do a little workaround to get this fixed, but I'm wondering what's a good solution?  What I ended up doing was to put the main camera variable on a script attached to the UI camera, this meant that since NGUITools could always get that camera it would be easier to get the main camera without having to do a GameObject.Find for it.  It works, but I wonder if there is a better way.

Any ideas, AM?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: HUD Text Camera Issue
« Reply #1 on: January 02, 2014, 11:44:21 AM »
That function finds the first camera that's able to see the specified layer. If you have more than one camera's mask set to draw the layer, then you will run into problems (for example if you set your game camera to also draw the UI layer).

Darkness

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: HUD Text Camera Issue
« Reply #2 on: January 02, 2014, 05:56:12 PM »
That function finds the first camera that's able to see the specified layer. If you have more than one camera's mask set to draw the layer, then you will run into problems (for example if you set your game camera to also draw the UI layer).

Yeah.  How should one avoid this?  If the game has multiple cameras, then this will sometimes pull up different cameras at different times.  If I wanted to guarantee that the same camera was pulled 100% of the time, then what would be the best way?  In my case, all cameras avoid drawing the UI layer so there's only one camera that does that.  Which means the UI camera will be the same 100% of the time.  After that, what would be the best way to find the appropriate camera without using a GameObject.Find() call?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: HUD Text Camera Issue
« Reply #3 on: January 03, 2014, 06:11:59 AM »
I would suggest you make use of tags. FindGameObjectWithTag is a quick operation as far as I know.

Darkness

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 90
    • View Profile
Re: HUD Text Camera Issue
« Reply #4 on: January 05, 2014, 02:54:49 AM »
I would suggest you make use of tags. FindGameObjectWithTag is a quick operation as far as I know.

I was mainly thinking about developers who want to use HUDText out of the box, or use a modification of the UIFollowTarget.  For those with large projects with more than one camera, this simple issue can cause some headaches, which is how I found it in the first place.  Another developer was using this line of code in their game which I assume was snipped from UIFollowTarget, but it was causing problems as the wrong camera was being found.  I fixed it in my code, but he was asking what would be the best way to handle it in his code for people who just want to use it out of the box without modification.

To be honest, I don't now...which is why I asked.  My knowledge of NGUI and Unity is not very good.  I figured you could have a solution so that code (even UIFollowTarget that comes with HUDText) could guarantee the correct camera was found in projects with multiple cameras without have to modify it or have the person run into the problem and not know how to fix it or what's causing it in the first place.  This is especially true if the person wants to avoid using GameObject.Find().