Adding Icon and Text

Hello Everyone

8929-addicon.png

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 + "

" + combatLog;

// and OnGUI
GUILayout.Label(combatLog, myStyle);

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"));

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);
}

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

8933-screenshot.16.png

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