Skip to content

Instantly share code, notes, and snippets.

@llamacademy
Last active February 2, 2021 13:10
Show Gist options
  • Save llamacademy/23b3d975d85915c957adc3af172fbb02 to your computer and use it in GitHub Desktop.
Save llamacademy/23b3d975d85915c957adc3af172fbb02 to your computer and use it in GitHub Desktop.
Scripts for Floating Damage Text in the video: https://www.youtube.com/watch?v=3eM-6qlwFuU. If you get value from LlamAcademy, consider becoming a Patreon supporter at https://www.patreon.com/llamacademy
using UnityEngine;
public class Damageable : MonoBehaviour
{
public FadeOutTextTween DamagePrefab;
public string NumberFormat = "N0";
// for your game, probably don't need this piece. It should instead be damage from the attacking object
private RangeInt DamageRange = new RangeInt(10, 20_000);
private ObjectPool DamageTextPool;
private void Awake()
{
DamageTextPool = ObjectPool.CreateInstance(DamagePrefab, 50);
}
// for your game, probably just have damage
public void ButtonClickTakeDamage()
{
TakeDamage(Random.Range(DamageRange.start, DamageRange.end), Random.value < 0.2f);
}
public void TakeDamage(int Damage, bool isCritical)
{
PoolableObject poolableObject = DamageTextPool.GetObject();
if (poolableObject != null)
{
FadeOutTextTween tweenText = poolableObject.GetComponent<FadeOutTextTween>();
tweenText.transform.SetParent(transform, false);
tweenText.Text.SetText(Damage.ToString(NumberFormat));
tweenText.FadeOut(isCritical);
if(isCritical)
{
tweenText.transform.SetAsLastSibling();
}
else
{
tweenText.transform.SetSiblingIndex(transform.childCount - 2);
}
}
}
}
using TMPro;
using UnityEngine;
[RequireComponent(typeof(CanvasGroup), typeof(TextMeshProUGUI))]
public class FadeOutTextTween : PoolableObject
{
private CanvasGroup CanvasGroup;
[HideInInspector]
public TextMeshProUGUI Text;
public Vector3 BigScale = new Vector3(1.2f, 1.2f, 1.2f);
public float ScaleDuration = 0.2f;
public float ScaleDelay = 0.4f;
public float FadeOutDelay = 0.4f;
public float FadeOutDuration = 0.4f;
public float MoveAmount = 35;
private void Awake()
{
CanvasGroup = GetComponent<CanvasGroup>();
Text = GetComponent<TextMeshProUGUI>();
}
private void Start()
{
FadeOut(false);
}
public void FadeOut(bool isCritical)
{
if (isCritical)
{
Text.color = Color.red;
Text.rectTransform.localPosition = new Vector2(0, 40);
}
else
{
Text.color = Color.white;
Text.rectTransform.localPosition = new Vector2(0, 25);
}
CanvasGroup.alpha = 1;
LeanTween.scale(gameObject, BigScale, ScaleDuration).setOnComplete(ScaleToNormal);
}
private void ScaleToNormal()
{
LeanTween.scale(gameObject, Vector3.one, ScaleDuration).setDelay(ScaleDelay).setOnComplete(FadeOutText);
}
private void FadeOutText()
{
LeanTween.alphaCanvas(CanvasGroup, 0, FadeOutDuration).setDelay(FadeOutDelay);
LeanTween.moveLocalY(gameObject, 25 + MoveAmount, FadeOutDuration).setDelay(FadeOutDelay).setOnComplete(Disable);
}
private void Disable()
{
gameObject.SetActive(false);
}
}
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool
{
private PoolableObject Prefab;
private int Size;
private List<PoolableObject> AvailableObjectsPool;
private ObjectPool(PoolableObject Prefab, int Size)
{
this.Prefab = Prefab;
this.Size = Size;
AvailableObjectsPool = new List<PoolableObject>(Size);
}
public static ObjectPool CreateInstance(PoolableObject Prefab, int Size)
{
ObjectPool pool = new ObjectPool(Prefab, Size);
GameObject poolGameObject = new GameObject(Prefab + " Pool");
pool.CreateObjects(poolGameObject);
return pool;
}
private void CreateObjects(GameObject parent)
{
for (int i = 0; i < Size; i++)
{
PoolableObject poolableObject = GameObject.Instantiate(Prefab, Vector3.zero, Quaternion.identity, parent.transform);
poolableObject.Parent = this;
poolableObject.gameObject.SetActive(false);
}
}
public PoolableObject GetObject()
{
PoolableObject instance = AvailableObjectsPool[0];
AvailableObjectsPool.RemoveAt(0);
instance.gameObject.SetActive(true);
return instance;
}
public void ReturnObjectToPool(PoolableObject Object)
{
AvailableObjectsPool.Add(Object);
}
}
using UnityEngine;
public class PoolableObject : MonoBehaviour
{
public ObjectPool Parent;
public virtual void OnDisable()
{
Parent.ReturnObjectToPool(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment