mdechatech / CharTweener

Unity3D - Animate characters in text
MIT License
389 stars 39 forks source link

Only works first run (Domain reload off?) #9

Open nicmar opened 2 years ago

nicmar commented 2 years ago

It works great, but only the first time after compilation. If i stop and start the game, the effect doesn't show. I have the Project settings > Reload Domain disabled. Does this use any static variables that needs resetting?

nicmar commented 2 years ago

I seem to have fixed it with the tips from https://docs.unity3d.com/Manual/DomainReloading.html

using System.Collections.Generic;
using TMPro;
using UnityEngine;

namespace CharTween
{
    public static class CharTweenerUtility
    {
        private static Dictionary<TMP_Text, CharTweener> Tweeners = new Dictionary<TMP_Text, CharTweener>();

        /// <summary>
        /// Returns a <see cref="CharTweener"/> guaranteeing the same instance is used for the same text.
        /// </summary>
        public static CharTweener GetCharTweener(this TMP_Text text)
        {
            CharTweener tweener;
            if (Tweeners.TryGetValue(text, out tweener))
                return tweener;

            tweener = text.gameObject.AddComponent<CharTweener>();
            tweener.Initialize(text);
            Tweeners.Add(text, tweener);
            return tweener;
        }

        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
        static void Init()
        {
            Tweeners = new Dictionary<TMP_Text, CharTweener>();   
        }

    }
}