- Home /
How to Implement System.StringBuilder into JavaScript?
Hello, I beg you an example please of how to implement string building into my very simple broadcasting messages Javascript! I have read all documentation and examples and non have Javascript examples! I suck at c# currently!
I'm asking for an example and small explanation because I want to learn how to do it!
Here is my broadcasting messages code:
#pragma strict
//Is this for iPhone
var forIphone : boolean = false;
//All iPhone tap Variables
var ObjHitName : String;
function Start () {
}
function Update () {
if (forIphone) {
//iPhone Touch code
if (Input.touchCount>0){
var touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began){ // if screen touch...
// create a ray passing through the touch pos:
var iphoneray: Ray = Camera.main.ScreenPointToRay(touch.position);
var iphonehit:RaycastHit;
if (Physics.Raycast(iphoneray, iphonehit)){ // do a raycast
var ObjHit = iphonehit.collider.gameObject;
ObjHitName = ObjHit.name;
BroadcastMessage("iPhoneInputTap", ObjHitName);
}
}
}
}
}
Thank you in advance! Daniel
PS: I always take the time to rate and mark correct/Helpful comments and answers!
Answer by Jamora · Jul 26, 2013 at 03:26 PM
Your use case isn't what I imagined, based on your previous question. I thought you were e.g. removing characters from the middle of a string, or doing other minor modifications on button presses.
Performance to coding ratio in using the StringBuilder class isn't very good here, as you have to actually send a reference of the SB class in your BroadcastMessage, and modify your receiving functions to use the StringBuilder class as well if you want to completely eliminate creation of strings and even then you might have to use ToString() eventually. So I feel a bit bad for leading you down this road...
if objHitName is only used in the BroadcastMessage, a quick optimization you can do is BroadcastMessage("iPhoneInputTap", ObjHit.name);
, and get rid of the ObjHitName variable. Though in the grand scheme of things the effects are minimal.
To answer the original question:
import System.Text;
#pragma strict
//Is this for iPhone
var forIphone : boolean = false;
//All iPhone tap Variables
var ObjHitName : StringBuilder;
function Start () {
ObjHitName StringBuilder = new StringBuilder("");
}
/*In your raycasthit code block*/
var ObjHit = iphonehit.collider.gameObject;
ObjHitName.Clear()
ObjHitName.Insert(0, ObjHit.name);
BroadcastMessage("iPhoneInputTap", ObjHitName);
On an unrelated note, instead of using boolean variables to determine what code to run, you should look into Platform Dependant Compilation
@Jamora Okay then :D THank you so much for co$$anonymous$$g back! but i should still look into GC Empty(); and when and where to put it? I read it all, seems like doing it during loading screen is best? Will unity do it anyways at scene change?
-Used the edit Button- ;P
Thank you for the tip i may implement :P and YES i and going to use platform dependent compilation! I just wanted a quick turn off for toggle for profiler :D
To be honest, I think you should make your game first, then use the profiler (or just play the game and see if you can create stuttering etc.) to see where performance is poor. Finally optimize to imporove that performance.
The extent you should be concerned with GC is being aware of how much garbage you create and not creating any more than you must, and possibly call System.GC() at scene loads etc. where the user is already waiting. Let Unity handle the rest.
Answer by SubatomicHero · Jul 26, 2013 at 02:56 PM
All .NET methods are available to JS so there's no real change.
I use it in my java applications in my job so e.g:
StringBuilder sb = new StringBuilder(); // create the builder
sb.append("add your strings here");
sb.append("and here if you want. it will append it to the end");
// if you need to return string builder as a string or conver to a string
return sb.toString();
String myString = sb.toString();
Thank you, it seems as though my case is better without system stringbuilder
I just want to say your answer is correct! I just realized i dont need it :P
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Int and Javascript help 2 Answers
Integer in if statement, scripting help 1 Answer
Child Obj Ignoring Parents Rotation! Help 1 Answer
iTween Visual Editor Event Call from C# to Javascript 1 Answer