Created
May 30, 2019 11:59
-
-
Save iamgabrielma/f4502b97aeb10a5a75b90c0f2aeb0e93 to your computer and use it in GitHub Desktop.
Creates floating text on events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* FloatingTextController.cs*/ | |
public class FloatingTextController : MonoBehaviour | |
{ | |
private static FloatingText popupText; | |
private static GameObject canvas; | |
public static void Initialize() | |
{ | |
canvas = GameObject.Find("Canvas"); | |
popupText = Resources.Load<FloatingText>("Prefabs/FloatingTextHolder"); | |
} | |
public static void CreateFloatingText(string text, Color32 color) | |
{ | |
FloatingText instance = Instantiate(popupText); | |
instance.transform.SetParent(canvas.transform, false); | |
color = new Color32(90, 133, 90, 255); | |
instance.SetText(text, color); | |
} | |
} | |
/* FloatingText.cs */ | |
public class FloatingText : MonoBehaviour | |
{ | |
public Animator animator; | |
private Text floatingText; | |
void Awake() | |
{ | |
AnimatorClipInfo[] clipInfo = animator.GetCurrentAnimatorClipInfo(0); | |
Destroy(gameObject, clipInfo[0].clip.length); | |
floatingText = animator.GetComponent<Text>(); | |
} | |
public void SetText(string text, Color32 color) | |
{ | |
floatingText.text = text; | |
floatingText.color = color; | |
} | |
} | |
/* Calling the method */ | |
private Color32 _floatingTextColor = new Color32(90, 133, 90, 255); | |
FloatingTextController.Initialize(); | |
FloatingTextController.CreateFloatingText("Test goes here", _floatingTextColor); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment