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 D3m0nE · Mar 14, 2013 at 09:15 AM · guiiconpicture

Adding Icon and Text

Hello Everyone

alt text

in this picture u can see CombatWindow

But i want to Remove "Killed" and add Icon

Is there way to do that?

I Use This Codes :

 // To Apply the Attacker and The Destroyed Name
 
 combatLog = attackerName + " Killed " + destroyedName + "\n" + combatLog;    
 
 // and OnGUI
 GUILayout.Label(combatLog, myStyle);
addicon.png (19.8 kB)
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

3 Replies

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

Answer by Chronos-L · Mar 14, 2013 at 10:48 AM

This is what I got after I write a simple code.

alt text

My Code

using UnityEngine; using System.Collections;

 [ExecuteInEditMode]
 public class KillMessage : MonoBehaviour {
     
     public Texture2D icon;
     
     void OnGUI() {
         CreateMessage("Cutter", "xxShooterxx", icon);
         CreateMessage("Anonymous", "M3n1nBlaCK", icon);
     }
     
     void CreateMessage( string player1, string player2, Texture2D actionIcon ) {
         GUILayout.BeginHorizontal();
         
         GUILayout.Label(player1);    
         GUILayout.Label(actionIcon, GUILayout.ExpandWidth(true));
         GUILayout.Label(player2);
         
         GUILayout.EndHorizontal();    
     }
 }
 

This is very rudimentary. You will need to do a few more things to improve it:

  • Adjust font and icon size to match each other seamlessly

  • Use a List to store the latest sets of action


screenshot.16.png (9.9 kB)
Comment
Add comment · Show 9 · 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 D3m0nE · Mar 14, 2013 at 02:32 PM 0
Share

Thanks

i will use your code

but If You Could Tell $$anonymous$$e How To $$anonymous$$ake NEW "Create$$anonymous$$essage"

because i use now

 Create$$anonymous$$essage (attackerName, destroyedName, icon);

And It keep Replace same $$anonymous$$SG. i want New Create$$anonymous$$essage in New Line

avatar image Chronos-L · Mar 15, 2013 at 12:43 AM 0
Share

Look at my example. I called Create$$anonymous$$essage() twice to get 2 lines of message. A typical way to do it is to store the messages in a List.

You will need a new class to store the information.

$$anonymous$$essage Class

using UnityEngine; using System.Collections;

 [ExecuteInEdit$$anonymous$$ode]
 public class $$anonymous$$ill$$anonymous$$essage : $$anonymous$$onoBehaviour {
    ... ....
 }
 
 public class $$anonymous$$essage {
    public String a;
    public String b;
    public Texture2D t2d;
 
    public $$anonymous$$essage( string strA, string strB, Texture2D tex2D )  {
    a = strA;
    b = strB;
    t2d = tex2D;
 }
    
 }

You can then create a List to store the new $$anonymous$$essage:

 List<$$anonymous$$essage> messageStream = new List<$$anonymous$$essage>();

Then, whenever there a player perform a kill:

 messageStream.Add( new $$anonymous$$essage("Player1", "Player2", knifeIcon) );

You can display it in GUI by:

 foreach( $$anonymous$$essage m in messageStream ) {
    Create$$anonymous$$essage( m.a, m.b, m.t2d );
 }

Or you can modify the Create$$anonymous$$essage() to use $$anonymous$$essage as parameter:

 foreach( $$anonymous$$essage m in messageStream ) {
    Create$$anonymous$$essage( m );
 }

You need to do some more work to maintain the size of the messageStream, so that it will display the latest kills, not every kills since the start of the game.

avatar image D3m0nE · Mar 15, 2013 at 01:27 AM 0
Share

Thank You! :)) But I Got 2 Problems 1- Sorting.. the new message show at the last it should be show at the first

2-Is There Way To Use Color For Each Line? i'm useing now

  void Create$$anonymous$$essage( string player1, string player2, Texture2D actionIcon ) {
 GUILayout.BeginHorizontal();
  
 if($$anonymous$$yTeam == "red"){
 GUILayout.Label(player1,$$anonymous$$illerStyle);
 GUILayout.Label(actionIcon,myStyle);
 GUILayout.Label(player2,DeadStyle);        
         }
         else{
 GUILayout.Label(player1,DeadStyle);
 GUILayout.Label(actionIcon,myStyle);
 GUILayout.Label(player2,$$anonymous$$illerStyle);    
     
             
         }
 

but the problem that its change all The $$anonymous$$illed To List and when the other $$anonymous$$m kill its change all to blue

avatar image Chronos-L · Mar 15, 2013 at 01:31 AM 0
Share

1st Question

ns$$anonymous$$d of messageStream.Add( new $$anonymous$$essage(...) ), use messageStream.Insert(0, new $$anonymous$$essage(...) )

2nd Question

o not use $$anonymous$$yTeam. Do you remember this question? It is almost the same thing again.

I will suggest you to expand the $$anonymous$$essage class.

 public class $$anonymous$$essage {
    public String a;
    public String b;
    public Texture2D t2d;
    public GUIStyle styleA, styleB, styleTexture;
 
    //Change your constructor to take 6 parameters
    public $$anonymous$$essage( ... )  {
       ...
    }
 }

Then, change your Create$$anonymous$$essage() to:

 void Create$$anonymous$$essage( $$anonymous$$essage m ) {
    GUILayout.BeginHorizontal();
  
    GUILayout.Label( m.a, m.styleA ); 
    GUILayout.Label( m.t2d, m.styleTexture );
    GUILayout.Label( m.b, m.styleB );
  
    GUILayout.EndHorizontal();    
 }

So, you will use them like this:

 messageStream.Add( new $$anonymous$$essage("RedPlayer1", "OtherPlayer2", knifeIcon, readStyle, otherStyle, iconStyle) );
 

To display:

 foreach( $$anonymous$$essage m in messageStream ) {
    Create$$anonymous$$essage( m );
 }

avatar image D3m0nE · Mar 15, 2013 at 05:55 AM 0
Share

@first. Thanks so much for helping and the Sorting works fibe! and that code helped me Alot

i'm not good at GUI

but for color i still got problem

because i want to do is to check

if the Player is From Red Team then The $$anonymous$$illerName will be in Red

And If He Is In Blue Team The $$anonymous$$illerName Will be in Blue Color

i'm using "if($$anonymous$$yTeam == "red){" the problem I Get that its change the color for all lines. not just the new one

i'm still watching your codes And Your Chat Code that you gave me :) i might get the idea perfectly.

sorry for my bad English .. and thanks again

Show more comments
avatar image
1

Answer by whydoidoit · Mar 14, 2013 at 09:52 AM

You can use a GUIContent which can contain both, or in a BeginHorizontal() you could draw one label with an image and one with the text of the score, then EndHorizontal().

C#

  GUILayout.Label(new GUIContent(someGUITexture, "some text"));
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 D3m0nE · Mar 14, 2013 at 02:33 PM 0
Share

Thanks For Help +1

avatar image
1

Answer by Khada · Mar 14, 2013 at 10:23 AM

 public Texture2D icon; //set this in the inspector or whatever
     
 void OnGUI()
 {
     //screen point to start drawing
     Vector2 startPoint = new Vector2(10, 10);
 
     //the gap between each GUI draw call
     float gap = 10;
 
     //strings for who killed who
     string killer = "Player";
     string killed = "Player";
 
     //get the number of pixels (up & down) each string will take on screen
     Vector2 killerSize = GUI.skin.label.CalcSize(new GUIContent(killer));
     Vector2 killedSize = GUI.skin.label.CalcSize(new GUIContent(killed));
 
     //create rects for our strings and icon
 
     Rect killerRect = new Rect(
         startPoint.x, //starts at the begining
         startPoint.y, 
         killerSize.x, 
         killerSize.y);
 
     Rect iconRect = new Rect(
         startPoint.x + killerSize.x + gap, //starts after previous rect + a small gap
         startPoint.y, 
         icon.width, 
         icon.height);
 
     Rect killedRect = new Rect(
         startPoint.x + killerSize.x + icon.width + gap * 2, //starts after previous rect + a small gap
         startPoint.y, 
         killedSize.x, 
         killedSize.y);
 
     //draw each
     GUI.Label(killerRect, killer);
     GUI.DrawTexture(iconRect, icon);
     GUI.Label(killedRect, killed);
 }
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 D3m0nE · Mar 14, 2013 at 02:32 PM 0
Share

Thank You Your Code Helped $$anonymous$$e +1

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

14 People are following this question.

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

Related Questions

Can't Figure out how to go about creating this part. 3 Answers

how to connect a printer in unity? 1 Answer

Inventory SlotID vs. RectPosition 0 Answers

Custom Inspector, Textfield with Icon 1 Answer

Problem with icon in GUISkin 0 Answers


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