• 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 May 14, 2013 at 11:42 PM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by TheDarkVoid · Aug 17, 2012 at 03:11 PM · guiiconsoptimazation

Optimizing a Script

i have a script that creates icons based on content in an array.

 using UnityEngine;
 using System.Collections;
 
 public class HUD_Icon_V2 : MonoBehaviour {
     public Texture2D[] Icons;
     public Rect StartPoint;
     public Rect IconSize;
     public int MaxCol;
     public float Padding;
     public GUISkin Skin;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     void OnGUI()
     {
         GUI.skin = Skin;
         int Col = 0;
         int Row = 0;
         int Count = 0;
         if(Count <= Icons.Length-1)
         {
             foreach(Texture2D ico in Icons)
             {
                 //GUI.DrawTexture(new Rect( StartPoint.x +((IconSize.width + Padding) *Col), StartPoint.y +((IconSize.height+ Padding)*Row), IconSize.width, IconSize.height), ico);
                 if(GUI.Button(new Rect( StartPoint.x +((IconSize.width + Padding) *Col), StartPoint.y +((IconSize.height+ Padding)*Row), IconSize.width, IconSize.height), ico))
                 {
                     Debug.Log("You Clicked: " + Col + "," + Row + " Icon#: " + (int)(Count+ 1));
                 }
                 Debug.Log("Col: " + Col + " Row: " + Row + " Drawn.");
                 Col++;
                 if(Col > MaxCol)
                 {
                     Col = 0;
                     Row++;
                 }
                 Count++;
                 }
         }else
         {
             Debug.Log("Done.");
         }
     }
 }

this works correctly, but it at a high cost to the framerate: alt text

is there any way this could be optimized and still work the same?

untitled.png (60.6 kB)
Comment
Add comment · Show 5
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 Khada · Aug 17, 2012 at 03:45 PM 0
Share

Highly suggest you get NGUI and use that ins$$anonymous$$d. Will make the world of difference.

avatar image TheDarkVoid · Aug 17, 2012 at 04:54 PM 0
Share

i dont wanto get that since unity 4 looks like it will supplement those features

avatar image Khada · Aug 17, 2012 at 05:08 PM 0
Share

If I'm not mistaken, Unity4 is only improving the performance of it's GUI system. Which means you'll still want NGUI. There is a good reason for why so many people use it. :)

avatar image TheDarkVoid · Aug 17, 2012 at 05:25 PM 0
Share

did u see the new GUI system unity 4 uses, it's kind of awesome: http://blogs.unity3d.com/2012/06/29/the-new-gui/

avatar image Khada · Aug 17, 2012 at 05:36 PM 0
Share

Oh! Looks very cool. Sounds like it could be worth using for sure. I did note the "GUI ready for a later release" though. So it's not co$$anonymous$$g out with Unity4. I would still recomend NGUI as a placeholder until then. Either way your going to have to change the way you've done things for the new system. :)

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by nikescar · Aug 17, 2012 at 03:47 PM

I know this may seem silly, but comment out the Debug.Log expressions and it will run just fine.

Comment
Add comment · Show 7 · 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 Khada · Aug 17, 2012 at 03:54 PM 0
Share

I have to spit out 50-100 logs each frame to reach a low fps but I guess a less than average machine could be killed by less. Still, he only has 3 log calls. Surely that's not going to make a huge difference even if OnGUI gets multiple calls per frame?

avatar image TheDarkVoid · Aug 17, 2012 at 04:13 PM 0
Share

it only logs the first time, i only see one of each log, there are no repeats

avatar image nikescar · Aug 17, 2012 at 04:24 PM 0
Share

I ran your code on my machine and it was crawling at about 6fps. When I got rid of the Debug.Log it ran at full speed. I've ran across this before on scripts of my own too. In my project I had Debug.Log in a for loop that kept writing the same debug message over and over in the same loop. In that project I was getting 1600fps without Debug and .2fps with Debug.

avatar image TheDarkVoid · Aug 17, 2012 at 04:56 PM 0
Share

i'll try, i dont need those debug, those are for testing

avatar image TheDarkVoid · Aug 17, 2012 at 04:58 PM 0
Share

that is very weird, removing the debug fix the probelm, but it wasn't logging per frame, it only logged once.. must be a bug.

Show more comments
avatar image
1

Answer by SolidSnake · Aug 17, 2012 at 03:32 PM

UnityGUI has bad performance in general and there is no draw call batching.. Since you are not using GUILayout or GUI.Window you can set the variable useGUILayout to false which could save few frames per second

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour-useGUILayout.html

also make sure that you have GUI textures with good compression

Comment
Add comment · Show 2 · 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 TheDarkVoid · Aug 17, 2012 at 04:15 PM 0
Share

i'll try compressing the textures, and setting the var.

avatar image TheDarkVoid · Aug 17, 2012 at 04:46 PM 0
Share

there is only a slight improvement.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

RTS minimap (mobile but still taking all ideas) 1 Answer

Custom Inspector, Access built-in component icons? 2 Answers

The GUI Icons in Unity have become blurry? 2 Answers

GUI Button with both texture and text? 4 Answers

NGUI: Recommended way of hiding/showing multiple menu panels? 1 Answer


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