Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 02, 2015 at 10:33 PM by DaiMangouDev for the following reason:

Other

avatar image
0
Question by DaiMangouDev · May 13, 2015 at 04:27 PM · c#editorprogrammingeditorwindow

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
 }
 }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by FortisVenaliter · May 13, 2015 at 04:35 PM

Well, there are two main things I can suggest:

  1. Don't use foreach, especially in update loops. It generates memory garbage which takes time to clean up.

  2. Why not use the Unity UI? Add a Canvas and some Images. This will allow Unity to handle the drawing itself so it can batch and such, and remove the need for your loop entirely.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaiMangouDev · May 13, 2015 at 05:00 PM 0
Share

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 ?

avatar image FortisVenaliter · May 13, 2015 at 05:33 PM 0
Share

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.

avatar image DaiMangouDev · May 13, 2015 at 05:36 PM 0
Share

each circle is what is instanced and added to the elements list

each circle i what is instanced and added to the elements list

nbe.jpg (6.7 kB)
avatar image FortisVenaliter · May 13, 2015 at 05:43 PM 0
Share

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.

avatar image DaiMangouDev · May 13, 2015 at 05:58 PM 0
Share

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

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How can I make function run in the editor only when I press a button in the inspector? 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Editor Windows - Where to get started? 1 Answer

OnGui elements as objects? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges