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 Daniel G · Jul 26, 2013 at 02:35 PM · javascriptsystem

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!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

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 Daniel G · Jul 26, 2013 at 05:32 PM 0
Share

@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

avatar image Jamora · Jul 26, 2013 at 05:48 PM 0
Share

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.

avatar image
1

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();
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 Daniel G · Jul 26, 2013 at 05:30 PM 0
Share

Thank you, it seems as though my case is better without system stringbuilder

avatar image Daniel G · Jul 26, 2013 at 05:33 PM 0
Share

I just want to say your answer is correct! I just realized i dont need it :P

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

16 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

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


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