Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Feltope · Mar 31, 2011 at 08:27 PM · textcolortextfieldtextarea

TextArea/Field - Individual Character's foreground and background color.

Hello everyone, I might be missing this in the docs, if so point me at it please.

I would like to find out if it is possible to change the individual foreground and background colors on a per character basis within a TextArea/TextField.

I know that you can change the whole thing with a GUIStyle but I can't seem to find (if it is possible) how to change the individual colors.

thanks.

Update: I added an image as an example of the output that I would need to achieve.

screen shot of a telnet client with parsed ansi color escape sequences, including background colors.

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

2 Replies

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

Answer by Mike 3 · Mar 31, 2011 at 11:53 PM

http://forum.unity3d.com/threads/9549-FancyLabel-Multicolor-and-Multifont-label

Comment
Add comment · Show 1 · 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 Feltope · Apr 01, 2011 at 12:02 AM 0
Share

We were just talking about that forum post in IRC and it won't work for my needs. However it does prove that Unity can't do exactly what I am asking so I will need to roll my own. thanks.

avatar image
1

Answer by alpha_wd40 · Dec 09, 2012 at 09:35 AM

 using UnityEngine;
 using System.Collections;
 
 public class MulticolorTextArea : MonoBehaviour 
 {
     public string stringToEdit = "Hello World\nI've got 2 lines...";
     
     void OnGUI() 
     {
         //backup color 
         Color backupColor = GUI.color;
         Color backupContentColor = GUI.contentColor;
         Color backupBackgroundColor = GUI.backgroundColor;
         
         //add textarea with transparent text
         GUI.contentColor = new Color(1f, 1f, 1f, 0f);
         GUIStyle style = new GUIStyle(GUI.skin.textArea);
         Rect bounds = new Rect(10, 20, Screen.width - 10, Screen.height - 20);
         stringToEdit = GUI.TextArea(bounds, stringToEdit);
         
         //get the texteditor of the textarea to control selection
         int controlID = GUIUtility.GetControlID(bounds.GetHashCode(), FocusType.Keyboard);    
         TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), controlID -1);
         
         //set background of all textfield transparent
         GUI.backgroundColor = new Color(1f, 1f, 1f, 0f);    
         
         //backup selection to remake it after process
         int backupPos = editor.pos;
         int backupSelPos = editor.selectPos;
         
         //get last position in text
         editor.MoveTextEnd();
         int endpos = editor.pos;
         
         Random.seed = 123;
         
         //draw textfield with color on top of text area
         editor.MoveTextStart();        
         while (editor.pos != endpos)
         {
             editor.SelectToStartOfNextWord();
             string wordtext = editor.SelectedText;    
     
             //set word color
             GUI.contentColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
             
             //draw each word with a random color
             Vector2 pixelselpos = style.GetCursorPixelPosition(editor.position, editor.content, editor.selectPos);
             Vector2 pixelpos = style.GetCursorPixelPosition(editor.position, editor.content, editor.pos);
             GUI.TextField(new Rect(pixelselpos.x - style.border.left, pixelselpos.y - style.border.top, pixelpos.x, pixelpos.y), wordtext);
         
             editor.MoveToStartOfNextWord();
         }
         
         //Reposition selection
         Vector2 bkpixelselpos = style.GetCursorPixelPosition(editor.position, editor.content, backupSelPos);    
         editor.MoveCursorToPosition(bkpixelselpos);    
             
         //Remake selection
         Vector2 bkpixelpos = style.GetCursorPixelPosition(editor.position, editor.content, backupPos);    
         editor.SelectToPosition(bkpixelpos);    
 
         //Reset color
         GUI.color = backupColor;
         GUI.contentColor = backupContentColor;
         GUI.backgroundColor = backupBackgroundColor;
     }
 }
Comment
Add comment · Show 1 · 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 Jamster · Jul 07, 2014 at 08:34 PM 0
Share

Brilliant solution! Prehaps put on the wiki?

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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Attach Text data to gameobjects by player 0 Answers

3D Text Object does not change to the color specified on Mouse Over 0 Answers

Text Alignment for Typing Effect 0 Answers

GameObject text colors in hierarchy panel 1 Answer

render.material.color is not working, or at least not completely? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges