Author Topic: OnGUI in NGUI  (Read 2883 times)

dkozlovtsev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
OnGUI in NGUI
« on: February 21, 2014, 08:07:04 AM »
I've noticed that there is OnGUI code inside one of the Monobehaviors supplied with NGUI. As far as i know existance of OnGUI function in runtime causes Unity3d to allocate additional memory to support their GUI solution. Is it possible in future releases to remove this code or provide a way to disable it in Release builds(via define for example)

rain

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 79
    • View Profile
Re: OnGUI in NGUI
« Reply #1 on: February 21, 2014, 08:10:36 AM »
I feel like you didn't actually read why that piece of code is inside OnGUI.

dkozlovtsev

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: OnGUI in NGUI
« Reply #2 on: February 21, 2014, 08:17:41 AM »
I feel like that you don't understand the point of my question. Let me repeat
As far as i know mere presense of OnGUI method in project causes additional memory allocations from unity3d, it very well may be that my knowledge is outdated, but why the code is inside NGUI is really irrelevant.

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: OnGUI in NGUI
« Reply #3 on: February 21, 2014, 10:32:22 AM »
I understand what you're saying, but it's a trade-off...

Do I want my cursor to blink, be able to move around, cut/paste, and whatnot and use OnGUI or not?

I thought there was a comment in the code explaining why OnGUI was there.

I went around trying to replace the OnGUI code with non-OnGUI code and failed miserably.

I had read the same thing, the "mere presence of OnGUI" creates GC allocations and can wreak havoc on your program.

My suggestion is... run your game window, use the profiler.  Stop.  Comment out OnGUI code.  run the game window, use the profiler. 

Actually, I think I'll try that in a bit.  Also, I believe a lot of those forum posts regarding the awfulness of OnGUI was before they moved a lot of the code to C++ (maybe).  I know it used to be worse.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnGUI in NGUI
« Reply #4 on: February 21, 2014, 01:13:46 PM »
The only OnGUI usage in NGUI that is not Editor-only is inside UIInput.cs. The reason is explained in a comment above:
  1.         /// <summary>
  2.         /// Unfortunately Unity 4.3 and earlier doesn't offer a way to properly process events outside of OnGUI.
  3.         /// </summary>
If you don't have an input field showing on the screen, this OnGUI function won't exist.