- Home /
editable 3d text?
how can i make 3d text editable when you click it? cheers
Answer by save · Jul 15, 2011 at 11:51 AM
You can use a hidden TextField and copy the text into a 3d-text GameObject. It's a bit of a workaround as the 3d-text isn't made to be editable directly - but Unity always finds a way.
I cooked up a little script for you to get you started:
/* 3d-Text Edit
Editable 3d-text object.
Put this script on a 3d-text GameObject.
Note: Make sure to have a collider on the GameObject to register Raycasts
*/
private var inEditMode : boolean = false;
private var storedString : String;
private var textComponent : TextMesh;
private var guiString : String;
function Start () {
//Store the String
textComponent = GetComponent(TextMesh);
storedString = textComponent.text;
guiString = storedString;
//Visual Aid for Focus (example)
renderer.material.color.a = 0.5;
checkChars(); //Check so that the 3d-text isn't empty
fitCollider(); //Set the Collider to fit the 3d Text Size
}
function OnGUI () {
if(inEditMode) {
//Make a TextField which sends to the 3d-text GameObject
GUI.SetNextControlName ("hiddenTextField"); //Prepare a Control Name so we can focus the TextField
GUI.FocusControl ("hiddenTextField"); //Focus the TextField
guiString = GUI.TextField (Rect (90, -100, 200, 25), guiString, 25); //Display a TextField outside the Screen Rect
//Listen for keys
if (Input.anyKey) {
textComponent.text = guiString; //Set the 3d-text to the same as our hidden TextField
fitCollider(); //Resize the Collider
}
}
//Begin Edit on RightClick
if (Input.GetMouseButtonDown(1)) {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if(hit.transform==transform) {
inEditMode = true;
renderer.material.color.a = 1; //Alpha 100% on selection
} else {
inEditMode = false;
renderer.material.color.a = 0.5; //Alpha 50% on deselection
checkChars(); //Check so the 3d-text isn't empty
}
}
}
//Exit Edit on KeyCode Return or Escape
if (inEditMode && Input.GetKeyDown(KeyCode.Return) || inEditMode && Input.GetKeyDown(KeyCode.Escape)) {
inEditMode = false;
renderer.material.color.a = 0.5; //Alpha 50% on deselection
checkChars(); //Check so the 3d-text isn't empty
}
}
//Set the Collider to fit the 3d Text Size
function fitCollider () {
collider.size.x= renderer.bounds.size.x;
collider.size.y= renderer.bounds.size.y;
}
//Check the Size of the 3d-text
function checkChars () {
if(textComponent.text.ToCharArray().Length==0) {
textComponent.text = "NULL";
fitCollider();
}
}
/*
//If you want inEditMode on Left Click use this function instead of manual Raycasting
function OnMouseDown () {
inEditMode = true;
}
*/
Feel free to ask if I left something you wonder uncommented.
References for this method: GUI-Class, GUI.SetNextControlName, GUI.FocusControl, GUI.TextField, Input-Class, Input.anyKey Physics.Raycast, RaycastHit
I updated the script with better independency (if you want to have several objects with this function in one scene), continuous keys, a simple visual example for selecting/deselecting, a NULL-check (so the text wont disappear) and collider-resize (for user convenience).
It is the new version in the answer, I don't know if you can see the revisions (can you?).
Resurrecting an old post - but this is exactly what I am looking for. save - I wonder if you would be kind enough to save me?! What I am trying to do is edit your script so that I can test for specific text entered by the user. So, if the user types "reset", the game state resets. I have inserted if statements and checks - but repeatedly get syntax errors, unity was expecting x but found y. I even tried just inserting an onCollision test to see if I could my head around the syntax and placement in your script but I fail every time. $$anonymous$$ay I be as bold to ask for you to edit this fine script and show me how to also include a test for specific text being inputted by the user please? If I knew you, i'd buy you a pint :)
Answer by Xian55 · Feb 02, 2013 at 08:09 PM
Hello, i have a problem with your script, im write my string but i cant see while im typing, and if i press the Enter or Esc key nothings happed.
Your answer
Follow this Question
Related Questions
3D Text Mesh produces wrong letters 1 Answer
Gameobject: 3D Text, how to change text? 2 Answers
Change 3D text priority 1 Answer
Center TextMesh to 3DModel 1 Answer
3D Text fading out 2 Answers