how can i make 3d text editable when you click it? cheers
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
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.