- Home /
Question by
CallToAdventure · Jun 08, 2011 at 07:50 AM ·
guitextguiskinstyle
Style dynamically created GUI Text
I have a nice health percent display for enemies and I am trying my best to skin the default Courier font with something more visible on screen. The enemy is also a prefab; I tried setting a GUISkin but didn't work; here's what I use now:
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour { public float currentHealth = 100; public float maxHealth = 100; public GameObject shipWreck; private GUIText text; public Vector3 offset; private Camera cam; private Vector3 textPos; //~ public GUISkin NautilusSkin;
void Start ()
{
cam = Camera.main;
GameObject go = new GameObject("GUIText_ship");
text = (GUIText)go.AddComponent(typeof(GUIText));
text.text = "100%";
text.alignment = TextAlignment.Center;
text.anchor = TextAnchor.MiddleCenter;
text.enabled = false;
}
//~ void OnGUI()
//~ {
//~ GUI.skin = NautilusSkin;
//~ }
void Update ()
{
textPos = cam.WorldToViewportPoint(transform.position + offset);
text.transform.position = textPos;
if (currentHealth < maxHealth)
{
text.enabled = true;
float p = (100 * currentHealth / maxHealth);
text.text = " "+p+"%";
}
}
void ApplyDamage(float damage)
{
currentHealth -= damage;
if (currentHealth <= 0.0f)
{
WreckShip();
}
}
void WreckShip ()
{
Destroy(text);
Instantiate(shipWreck, transform.position, transform.rotation);
Destroy(gameObject);
}
}
Comment
Your answer
Follow this Question
Related Questions
Can't change button GUIStyle background. 1 Answer
Unity Custom Styles and how to access in scripts 1 Answer
apply GUI styles to GUIText 1 Answer
GUIskin and c#, How? 1 Answer
GUI Text as a Button? 1 Answer