- Home /
Is there a way to set a GUI.matrix that will be used by all OnGui() functions?
Hello,
I'm making my own gui library using mvc pattern, since each object I'm making (a button for example) has its own data/logic and its own OnGui function, I was wondering if there's a way to set a scale matrix on a script so that each OnGui function use that matrix or do I need to set this scale matrix on the beginning of each OnGui function? I've tried setting this scale matrix on a script and I made it execute as the first script by setting its priority on Project Settings-> Script execution order, the scale matrix is successfully applied however it works only for objects being drawn in the same script, objects being drawn from other OnGui use the default matrix, to make them use that matrix I need to reapply it in the beginning of the OnGui. any better workaround?
Answer by Bunny83 · Oct 17, 2012 at 10:55 PM
Unity prepares the environment for each callback it's going to invoke. So the answer is: Yes, you need to set it in each OnGUI function seperately.
The static nature of a lot of things in Unity might make you think it's a statemachine. Well, it actually is, but it's not completely under your control. Unity uses those variables to provide state information for the current execution of a callback. The Event class, which is the most important thing in OnGUI, works the same way. Camera.current is another example
Thank you for your reply. So I guess I'll leave it for each OnGUI and make some tests to see the impact it has on the performance, if it is unacceptable I'll rewrite my system to have a single OnGUI. :)
If you are worried about performance, don't use OnGUI! Performance isn't really it's strong point...
You could always store your matrix in a static variable and just put it in place on each OnGUI call.
of course I won't recalculate the matrix every time :) I know OnGUI is not that perfor$$anonymous$$g yet I found it good enough for a not too complex gui, also by enable/disable on-demand those scripts I got some very good results :)
Your answer
Follow this Question
Related Questions
Scaling problem for mobile 0 Answers
How can I scale the insides of a ScrollView 0 Answers
GUI - Scale GUIs According to Resolution 6 Answers
Scaling the GUI using a matrix issues 2 Answers
GUI.Matrix Problems 0 Answers