Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dreeka · Aug 25, 2011 at 04:49 PM · guitextoutline

Text with outline

Hello,

I am looking for a way to create GUI text with outlines. While searching, I have found the following method:

 public static function DrawOutline(position : Rect, text : String, style : GUIStyle, outColor : Color, inColor : Color){
     var backupStyle = style;
     style.normal.textColor = outColor;
     position.x--;
     GUI.Label(position, text, style);
     position.x +=2;
     GUI.Label(position, text, style);
     position.x--;
     position.y--;
     GUI.Label(position, text, style);
     position.y +=2;
     GUI.Label(position, text, style);
     position.y--;
     style.normal.textColor = inColor;
     GUI.Label(position, text, style);
     style = backupStyle;
 }

It works well on smaller text, but my text size is raging between 22 and 56, and I dont know how to change the code to work with those size's aswell.

Can someone help me to undersand the code, so I can make a function from it?

Thanks!

Comment
Add comment · Show 2
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 Owen-Reynolds · Aug 25, 2011 at 05:58 PM 0
Share

Is the problem that the borders are too small when the font-size grows? If so, could shift by fontSize/12 ins$$anonymous$$d of always 1 and 2.

avatar image BerggreenDK · Aug 25, 2011 at 10:58 PM 0
Share

that example generates 5 GUI elements pr. text. Thats how it was done with old HT$$anonymous$$L and CSS, but I am not sure this is the best approach in a realtime environment as Unity. You can provide your own FONTS too.

3 Replies

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

Answer by BerggreenDK · Aug 25, 2011 at 11:04 PM

First of all, it IS ALREADY a function, but I guess you want to know how to use it then?

Try something like:

    // this first part initializes what font, size etc you want to use. 
    // Read Script reference on how to use GUIStyle.
    gsText: GUIstyle = new GUIStyle();
 
    // then the actual function is called.
    // 1st parameter is the Rectangle of where to put / generate/ 
    // show the text on the screen. You have Screen.width and Screen.height
    // if you need to align to sides or center.

    // 2nd parameter is the actual text you want to show 

    // 3rd is GUIStyle which is the stylesheet for your text. 
    // Including alignment, font-size, padding etc.
 
    DrawOutline ( Rect(10,10,200,30, "Hello World", gsText);


The function does it like this:

First we have a "center", then we go north west of this (-1,-1) and plot the first version (black outline?)

Then it moves +2 on either direction. Lets take east, then plot once more.

Then move +2 south, plot again

Then move -2 west again. Now we are in the bottom corner. Plot last part of 4 corners of the outline/shadow.

Finally. Move into center. +1,+1 and plot in "white" ontop of the other 4. Then we have a text surrounded by 4 other texts that are "outlining" the inner text. Hench the two variable names for textcolors: in/out

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 adam718 · Mar 22, 2012 at 08:45 AM 0
Share

But if width of outline is get high, with the code above won't make outline text. That is not outline.

So, could you tell me how to handle it with outline's width? Help.

avatar image
1

Answer by Praesidium · May 08, 2014 at 02:18 PM

 void DrawOutline(Rect r,string t,int strength,GUIStyle style){
         GUI.color=new Color(0,0,0,1);
         int i;
         for (i=-strength;i<=strength;i++){
             GUI.Label(new Rect(r.x-strength,r.y+i,r.width,r.height),t,style);
             GUI.Label(new Rect(r.x+strength,r.y+i,r.width,r.height),t,style);
         }for (i=-strength+1;i<=strength-1;i++){
             GUI.Label(new Rect(r.x+i,r.y-strength,r.width,r.height),t,style);
             GUI.Label(new Rect(r.x+i,r.y+strength,r.width,r.height),t,style);
         }
         GUI.color=new Color(1,1,1,1);
     }

There u go ,an outline function with width. Even though the draw calls get higher the bigger the outline, at least it only draws (width*2)+((width-2)*2) times.

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
avatar image
0

Answer by nateonguitar · Jul 30, 2017 at 11:14 PM

The previous answers only work for smaller fonts and borders (except for Praesidium's, that answer's code is just not very easy to understand). This method is a little less performant than the simpler answers but will get you the border you want. It should be commented so you understand what's going on.

 void OnGUI()
 {
         drawScore(score);
     }
 }
 
 public void drawScore(int score)
 {
     Rect scoreRect = new Rect(10, 10, 300, 100);
     if (score > 0)
     {
         GUIStyle style = new GUIStyle();
         int borderWidth = 15;
 
         style.fontSize = 150;
         style.fontStyle = FontStyle.Bold;
         DrawTextWithOutline(scoreRect, score.ToString(), style, Color.black, Color.white, borderWidth);
     }
 }
 
 void DrawTextWithOutline(Rect centerRect, string text, GUIStyle style, Color borderColor, Color innerColor, int borderWidth)
 {
     // assign the border color
     style.normal.textColor = borderColor;
 
     // draw an outline color copy to the left and up from original
     Rect modRect = centerRect;
     modRect.x -= borderWidth;
     modRect.y -= borderWidth;
     GUI.Label(modRect, text, style);
 
         
     // stamp copies from the top left corner to the top right corner
     while(modRect.x <= centerRect.x + borderWidth)
     {
         modRect.x++;
         GUI.Label(modRect, text, style);
     }
 
     // stamp copies from the top right corner to the bottom right corner
     while (modRect.y <= centerRect.y + borderWidth)
     {
         modRect.y++;
         GUI.Label(modRect, text, style);
     }
 
     // stamp copies from the bottom right corner to the bottom left corner
     while (modRect.x >= centerRect.x - borderWidth)
     {
         modRect.x--;
         GUI.Label(modRect, text, style);
     }
 
     // stamp copies from the bottom left corner to the top left corner
     while (modRect.y >= centerRect.y - borderWidth)
     {
         modRect.y--;
         GUI.Label(modRect, text, style);
     }
         
     // draw the inner color version in the center
     style.normal.textColor = innerColor;
     GUI.Label(centerRect, text, style);
 }

alt text

This stamping motion can be modified for better performance if you know your font widths will be wider by changing all of the increments to += someNumber, like so:

 . . . . . .
     // stamp copies from the top left corner to the top right corner
     while(modRect.x <= centerRect.x + borderWidth)
     {
         modRect.x += 2;
         if(modRect.x > centerRect.x + borderWidth){
             modRect.x = centerRect.x + borderWidth;
         }
         GUI.Label(modRect, text, style);
     }
 . . . . . .


but smaller font widths will break this as it skips pixels.


border.png (12.6 kB)
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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Creation of checkbox next to text 1 Answer

GUI.TextField duplicates texts on android. 0 Answers

v4.6 Create GUI Elements Via Script? 1 Answer

Changing 3d text through script 1 Answer

Mario style gui! 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