- Home /
Other
In my Editor Window ,How do i get maximum performance while calling OnGUI from within a foreach loop ?
How do i get maximum performance while calling OnGUI from within a foreach loop and is it even practical to be calling a GUI function that lives in a class whose base class is abstract and inherits from scriptableobject ?
Summary In a OnGUI function in the class 'showStuff', I am instancing a class (BoxElement) which inherits from the class'Element' , which inherits from ScriptableObject.
each instance of BoxElement is added to a a list named 'element'.
// Quick code example
//C# SCRIPT 1
[System.Serializable]
public abstract class Element: ScriptableObject
{
public virtual void DrawGUI()
{
}
}
public void BoxElement: Element
{
public override void DrawGUI()
{
GUI.DrawTexture(SomeRect, SomeTexture);
}
}
//C# SCRIPT 2
public class showStuff
{
private List <Element> elements = new List<Element>();
void OnGUI()
{
if(GUILayout.Button("add new"))
{
BoxElement Box_Element = ScriptableObject.CreateInstance<BoxElement>();
}
foreach(BoxElement el in elements )
{
el.DrawGUI(); // EXTEMEMELY SLOW
}
}
}
Answer by FortisVenaliter · May 13, 2015 at 04:35 PM
Well, there are two main things I can suggest:
thanks for the help , however this is used in an editor window. how else can i have the GUI displayed if not with the foeeach ?
A regular "for" loop is more efficient than "foreach" because it does not use Linq encapsulation.
Can you post an image of what you're trying to achieve? I guess I'm not totally clear on that.
And, is there a reason they need to all show up in the same editor? Usually you derive from CustomEditor to write a custom inspector for the individual objects, then you select one and edit it.
i'm making a node based editor in a editor window . so everything has to show up in the same editor window. I'm not making a custom inspector.
each circle you see is instanced and each instance is added to the elements list. the more i instance , the slower performance gets ,
if i instance 50 ,i end up with about a 100+ ms inpact on the CPU