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 /
avatar image
0
Question by ikelaiah · Sep 19, 2013 at 04:53 AM · c#guistring

What is the best way to concatenate string to create an interaction logging console?

I need to display user interaction in a simulation, in a console-looking way. E.g, xterm, conEmu, etc.

I am currently using...

 void UpdateUserInteraction(string userInput) {
      consoleText += userInput.ToString() + "\n";
 }

... to concatenate strings, where consoleText is the text content of a label.

After 1000+ concatenation, everything sluggish and stop eventually. Now, my question, is there a better way to concatenate strings to simulate a console?

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 vexe · Sep 19, 2013 at 05:01 AM 0
Share

Not sure if this will solve it, but whenever you're dealing a lot with concatenation and heavy string duty, use StringBuilder to do the heavy lifting for you.

avatar image ikelaiah · Sep 19, 2013 at 05:18 AM 0
Share

Like this? consoleText += userInputInStringBuilder.ToString() + "\n"; I wonder if that would work. I'll try.

avatar image vexe · Sep 19, 2013 at 05:21 AM 0
Share

No just use the Append method in the builder, did you check the link I gave above?

avatar image ikelaiah · Sep 19, 2013 at 06:25 AM 0
Share

@vexe Sorry, I was thinking was of displaying it as string. What I had in $$anonymous$$d was actually.... userInputInStringBuilder.Append(userInteraction); consoleText = userInputBuilder.ToString(); // every OnGUI() calls

avatar image vexe · Sep 19, 2013 at 06:28 AM 0
Share

Oh I see. Did you get any worthwhile performance enhancement out of that?

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TrickyHandz · Sep 19, 2013 at 05:00 AM

First thing you might want to look at and see if it helps at all is the redundant casting of userInput to a string using the .ToString() method. This is wasting processor time because it is converting a string to a string unnecessarily, so just do this:

 void UpdateUserInteraction(string userInput) 
 {
      consoleText += userInput + "\n";
 }

Additionally, the problem may exist with the GUI implementation in Unity itself. It is notoriously slow because the entire GUI has to be redrawn every time OnGUI() gets called, which is potentially multiple times per frame.

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 ikelaiah · Sep 19, 2013 at 05:20 AM 0
Share

What about if I use StringBuilder as suggested by @vexe above?

avatar image syclamoth · Sep 19, 2013 at 05:48 AM 2
Share

The problem is most likely to be OnGUI's inherent slowness, so no it doesn't really make a difference. Try storing the parts of the console seperately, and only draw to the screen those parts that are currently in the player's view.

avatar image vexe · Sep 19, 2013 at 05:56 AM 0
Share

I hate UnityGUI, I gave up on it a long time ago. Expect to have many more headaches in the future with UnityGUI. Wanna spare a couple of hair-$$anonymous$$ring moments and a lot of frustration? Go with NGUI.

avatar image ikelaiah · Sep 19, 2013 at 06:31 AM 0
Share

@vexe Bought it. Will give it a whirl.

avatar image RudyTheDev · Sep 12, 2015 at 02:27 PM 0
Share

userInput.ToString() + "" is exactly the same as userInput + "". The compiler treats everything as ToString() once it decides it's a string you are making, because it's using Concat(). It's not casting and you cannot not have ToString() called when making a string from a non-String type. Even String has ToString:

 public override String ToString() {
     return this; 
 }
avatar image
0

Answer by BowlerBitesLane · Sep 12, 2015 at 07:14 PM

I believe that the intended use of the StringBuilder class would be to define the stringBuilder outside of your function. This would then allow you to use the .append method to add strings to the string builder by reference rather than assigning more memory to them, and also to avoid reinstantiating the StringBuilder each iteration.

If you need to refresh the contents within the StringBuilder just call the .Clear method.

This may marginally improve the performance of your app.
This post is a bit late but I hope it is helpful for others who are looking into this.

Also there is a nice manual entry available here that explains this in more detail: http://docs.unity3d.com/Manual/UnderstandingAutomaticMemoryManagement.html

Comment
Add comment · Show 1 · 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 SarperS · Sep 12, 2015 at 07:21 PM 0
Share

Just a little addition, afaik Clear method is not yet supported by the Unity's $$anonymous$$ono version. If so, you can set Length and Capacity properties to 0. "Clear is a convenience method that is equivalent to setting the Length property of the current instance to 0 (zero)." anyway.

avatar image
0

Answer by SarperS · Sep 12, 2015 at 07:16 PM

Use StringBuilder class instead of + concat operator. It is much faster. source: http://malideveloper.arm.com/documentation/developer-guides/arm-guide-unity-enhancing-mobile-games/

Comment
Add comment · 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
0

Answer by vexe · Sep 12, 2015 at 07:43 PM

Use gstring http://forum.unity3d.com/threads/gstring-gc-free-string-for-unity.338588/

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 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 avatar image

Related Questions

how to display the damage value from a mob 1 Answer

Problems with GUI and Resolutions. (C#) 1 Answer

How to display Int variable onto the GUI/HUD 0 Answers

Multiple Cars not working 1 Answer

How do I change the fount size of GUI 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