Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Is it possible by script to break any TextMesh into its individual letters?

Discussion in 'Scripting' started by citycrusher, May 7, 2013.

  1. citycrusher

    citycrusher

    Joined:
    Sep 26, 2010
    Posts:
    41
    if so, please give me some hint how to do it :D

    what i want is write a script to take any textMesh split it apart so each letter in the textMesh becomes a separate gameObject.

    is such a function possible to write?? i'm not expecting anyone to write it for me, just hope for a clue where to start on this idea.

    had this great idea that needs a lot of break apart text :D

    please let me know if i am having a scripting fantasy or if there is a chance i could do such an effect.
     
  2. wolfhunter777

    wolfhunter777

    Joined:
    Nov 3, 2011
    Posts:
    534
    Well the most easiest solution I'm thinking of right now is to create an individual text mesh for every character in the string in the text mesh. After that, I would remove or disable the old text mesh and do whatever to the new ones (probably store them in an array when I create them for easier access). How efficient is that? I would say, no.

    Try experimenting. There's no harm in that.
     
  3. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    Well this is the first idea i had (pretty much what hunter said)

    You need to adjust somehow the position of the newly created letters as it will differ from font to font (If you dont want brain pain then you could hard code it if you need it only for 1 situation)

    add: the script has to be named TextBreak since its declaring itself in the script to avoid a recursive effect which would create thousands of letters.

    Code (csharp):
    1. #pragma strict
    2.  
    3. private var myText : TextMesh;
    4.  
    5. private var letters = new ArrayList();
    6. private var newText : TextMesh;
    7.  
    8. function Start () {
    9.     myText = transform.GetComponent(TextMesh);
    10.    
    11.     for ( var i = 0; i < myText.text.Length; i ++ ) {
    12.         var newObj : Transform;
    13.         var nText  : TextMesh;
    14.         var tb     : TextBreak;
    15.        
    16.         newObj = Instantiate(transform, transform.position, transform.rotation);
    17.         nText = newObj.GetComponent(TextMesh);
    18.         nText.text = myText.text.Chars[i] + "";
    19.        
    20.         tb = newObj.GetComponent(TextBreak);
    21.         tb.enabled = false;
    22.        
    23.         //and now figure somehow out how to position the letters right...
    24.        
    25.         letters.Add(newObj);
    26.     }
    27.    
    28.     Destroy(gameObject);
    29. }
    30.  
    31. function Update () {
    32.  
    33. }
     
    Last edited: May 7, 2013