Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Created July 17, 2017 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanYin/cc20ef9f580cdf55c8ab0297936ecea6 to your computer and use it in GitHub Desktop.
Save JonathanYin/cc20ef9f580cdf55c8ab0297936ecea6 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class GUIComponent : MonoBehaviour
{
public GUISkin guiSkin; // choose a guiStyle (Important!)
public static string text = "P1"; // choose your name
public Color color = Color.blue; // choose font color/size
public float fontSize = 10;
public float offsetX = 0;
public float offsetY = 0.5f;
float boxW = 150f;
float boxH = 20f;
public bool messagePermanent = true;
public float messageDuration { get; set; }
Vector2 boxPosition;
void Start()
{
if (messagePermanent)
{
messageDuration = 1;
}
}
void OnGUI()
{
if (messageDuration > 0)
{
if (!messagePermanent) // if you set this to false, you can simply use this script as a popup messenger, just set messageDuration to a value above 0
{
messageDuration -= Time.deltaTime;
}
GUI.skin = guiSkin;
boxPosition = Camera.main.WorldToScreenPoint(transform.position);
boxPosition.y = Screen.height - boxPosition.y;
boxPosition.x -= boxW * 0.1f;
boxPosition.y -= boxH * 0.5f;
guiSkin.box.fontSize = 10;
GUI.contentColor = color;
Vector2 content = guiSkin.box.CalcSize(new GUIContent(text));
GUI.Box(new Rect(boxPosition.x - content.x / 2 * offsetX, boxPosition.y + offsetY, content.x - 1, content.y - 1), text);
}
}
}
@JonathanYin
Copy link
Author

This script creates a small box with a tag above each player's tag, thus allowing people to easily distinguish between them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment