- Home /
Replace Text with coresponding 3D Models
So here is my idea but I am not really sure how to implement it. I want to develop a script that evaluates what the user types in a string into a public field or even the Text Mesh component. This script will run through each letter of what was typed and instantiate the corresponding 3D model. So in essence, I can create all the characters of the alphabet in 3D and export them into Unity. When I type into the text field and hit play...the script will display the 3D models of the characters.
Positioning, size and readability don't matter so much here. I just wanted to know if it was possible and maybe some ideas on how to start it.
I am newbie when it comes to C#, but I figured out that you can use something like this to display when a textobject contains a certain string.
GetComponent<TextMesh>().text = test;
if (test.IndexOf("this") != -1)
{
Instantiate(textObj, transform.position, transform.rotation);
Debug.Log("string contains 'this'");
}
I know realistically, that I need to do some sort of Loop in order to check each character of the string and then match it to the corresponding 3D Model. Thoughts? Comments? Am I sort of on the right track?
Thanks for reading and for any help or suggestions you might have.
I'd suggest looking at FlyingText3D, which would simplify things a lot.
@Eric5h5, I would try something like that if it was free. But without a demo I do not want to spend any money on a plugin. Thanks though.
Answer by DaveA · Oct 10, 2012 at 10:01 PM
There are probably many ways to do this, but off the top:
Create an array of GameObjects, sized to 128. Drop your text object models (prefabs) into the slot corresponding to the ascii code in the array. Example: Element 65 would correspond to model 'A'. (note you can make an Editor script to aid this process).
When running, access the string you are converting character-at-a-time with String. Untested:
var chars : GameObject[]; // assign these inspector or with Editor script
for (int i = 0; i < str.length; i++)
Instantiate (chars[((int)str[i])], new Vector3(i, 0, 0), Quaternion.identity);
Thank you Dave, I will try to run through the method you suggested. Like i said I am still trying to get my head around C#. It looks like you idea is simple enough... in theory.... Thanks for the help either way, you have given me something to go with!
Hey Dave, so I have tried various versions of the code you listed trying to get it to function correctly with no success. Right now I am stuck on the very last line, where it Instantiates the GameObject. I don't really understand what you are doing in that last line so any explanation would be very helpful. Currently I am able to successfully log each letter typed in the "string" field but how do I convert each letter to a corresponding ID of the 3D gameObject? Thanks again!
chars[] contains the gameobjects. str[i] is the i'th character in the string. (int)str[i] converts the character (like "A") to a number (like 65). $$anonymous$$aybe you don't need the .ToString() part, try that. I'll edit my answer
Using a variation of Dave's example code I was able to add 3D letters using a public string variable in the inspector. Thanks Dave! I will try post the exact method I used when it is complete. Currently I am having issues with deleting duplicate letters.
Here is a part of the script I ended with.
byte[] ASCIIBytes = Encoding.ASCII.GetBytes(test);
float po = 0;
foreach (byte b in ASCIIBytes)
{
Debug.Log(test);
// Adds current letter to List and Instantiates letter .obj
currentLetters.Add((Instantiate(testObj[b], new Vector3(po, 0, 0), Quaternion.Euler(270, -180, 0)) as GameObject));
testObj[b].transform.tag = "Word";
}
Your answer
Follow this Question
Related Questions
Problem with fonts with ttf format 2 Answers
3D Text Mesh - Display Glitch 0 Answers
i Have AN error when i create assets font editor 0 Answers
3D text face camera 2 Answers
how do I add text to a 3D cube? 2 Answers