- Home /
Adding Icon and Text
Hello Everyone
![]()
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);
Answer by Chronos-L · Mar 14, 2013 at 10:48 AM
This is what I got after I write a simple code.

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
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
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.
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
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 );
}
@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
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"));
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);
}
Your answer
Follow this Question
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