- Home /
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?
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.
Like this? consoleText += userInputInStringBuilder.ToString() + "\n";
I wonder if that would work. I'll try.
No just use the Append
method in the builder, did you check the link I gave above?
@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
Oh I see. Did you get any worthwhile performance enhancement out of that?
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.
What about if I use StringBuilder
as suggested by @vexe above?
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.
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.
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;
}
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
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.
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/
Your answer
Follow this Question
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