• 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
1
Question by buskins · Jul 01, 2011 at 07:46 PM · textmesh3dtexteditclickable

editable 3d text?

how can i make 3d text editable when you click it? cheers

Comment
Add comment · Show 1
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 buskins · Jul 01, 2011 at 08:20 PM 0
Share

when you right click it you are able to edit it.

2 Replies

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

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

Comment
Add comment · Show 7 · 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 biohazard · Jul 15, 2011 at 12:26 PM 2
Share

nice! as always, save saves people

avatar image save · Jul 15, 2011 at 01:20 PM 1
Share

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).

avatar image buskins · Jul 19, 2011 at 04:21 PM 0
Share

can u put the updated version up plz

avatar image save · Jul 19, 2011 at 05:32 PM 0
Share

It is the new version in the answer, I don't know if you can see the revisions (can you?).

avatar image MightyMatty · Nov 08, 2011 at 11:44 PM 0
Share

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 :)

Show more comments
avatar image
0
Wiki

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.

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Insert 3d text to the front face of a cube GameObject 0 Answers

3DText with outline 0 Answers

3D Text font issue 2 Answers

Cannot get 3D Text/Text Mesh to wrap or a attached collider used as a button to scale based on word count. 2 Answers

Input 3D text with script? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges