CustomTMP_Flip

2023. 10. 9. 15:09Public/Unity

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

#if UNITY_EDITOR
[ExecuteInEditMode]
#endif
public class TMP_FlipRotateFX : MonoBehaviour
{
    public enum View
    {
        Show,
        Hide
    }


    [Range(0f, 1f)] public float m_t = 0f;
    [SerializeField][Range(0f, 1f)] private float m_flipDelay = 0f;
    [SerializeField] private float m_wooble;
    private Vector3[] vertices;
    private TMP_Text m_tmp;
    private Mesh mesh;
    public View m_view;
    private void Start()
    {
        m_tmp = GetComponent<TMP_Text>();
        //Renderer renderer = GetComponent<Renderer>();
        //renderer.material = Instantiate(renderer.material);
        //m_tmp.fontMaterial = Instantiate(m_tmp.fontMaterial);
    }

    private void LateUpdate()
    {
        m_tmp.ForceMeshUpdate(true, true);
        mesh = m_tmp.mesh;
        vertices = mesh.vertices;
        float power = m_tmp.fontSize;
        int count = m_tmp.textInfo.characterCount;

        for (int i =0;i< count; ++i)
        {
            TMP_CharacterInfo c = m_tmp.textInfo.characterInfo[i];
            if (c.character == ' ') continue;
            int index = c.vertexIndex;

            /*
             12
             03
             */
            Vector3 offset = Wobble(Time.time + i)* m_wooble;
                        
            float indexDuration = Mathf.InverseLerp(0, count,i);           

            float delay = Mathf.Lerp(0f, m_flipDelay, indexDuration);

            float t = Mathf.Lerp(0f,1f, Mathf.InverseLerp(delay,1f,m_t));

            //Hide
            if(m_view == View.Hide)
            {
                Vector3 flip00 = new Vector3(power * 0.1f, -power * 0f, 0) * t + offset;
                Vector3 flip01 = new Vector3(-power * 0.1f, -power*1.1f, 0) * t + offset;
                Vector3 flip02 = new Vector3(-power * 0.1f, -power * 1f, 0) * t + offset;
                Vector3 flip03 = new Vector3(power * 0.1f, -power * 0.1f, 0) * t + offset;

                vertices[index] += flip00;
                vertices[index + 1] += flip01;
                vertices[index + 2] += flip02;
                vertices[index + 3] += flip03;
            }
            else
            {
                Vector3 flip00 = new Vector3(0 , power*1f, 0) * (1-t) + offset;
                Vector3 flip01 = new Vector3(power * 0.1f , 0 , 0) * (1 - t) + offset;
                Vector3 flip02 = new Vector3(0 , -power*0.1f , 0) * (1 - t) + offset;
                Vector3 flip03 = new Vector3(0 , power , 0) * (1 - t) + offset;

                vertices[index] += flip00;
                vertices[index + 1] += flip01;
                vertices[index + 2] += flip02;
                vertices[index + 3] += flip03;
            }

        }

        mesh.vertices = vertices;
        m_tmp.canvasRenderer.SetMesh(mesh);
    }

    Vector2 Wobble(float time)
    {
        return new Vector2(Mathf.Sin(time * 3.3f), Mathf.Cos(time * 2.5f));
    }
}

'Public > Unity' 카테고리의 다른 글

최적화 테스트(Job,Burst)  (0) 2024.08.13
SRP 정리  (0) 2023.09.12
JungUtil(MyCustomUtil)  (0) 2023.09.11
Custom SRP (진행중)  (0) 2023.09.10
Unity Tip  (0) 2023.09.04