Created
July 14, 2017 18:22
-
-
Save cjacobwade/09be475d943c038828503ea2855ac33f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEngine.UI; | |
| public class TextTweenIn : Text | |
| { | |
| List<Vector3> charCorners = new List<Vector3>(); | |
| List<float> _charScales = new List<float>(); | |
| [SerializeField] | |
| float _scaleTime = 0.2f; | |
| [SerializeField] | |
| AnimationCurve _scaleCurve = new AnimationCurve(); | |
| readonly UIVertex[] m_TempVerts = new UIVertex[4]; | |
| bool _rebuildScales = false; | |
| string _prevText = ""; | |
| protected override void OnPopulateMesh(VertexHelper toFill) | |
| { | |
| if (text != _prevText) | |
| { | |
| _rebuildScales = true; | |
| _charScales.Clear(); | |
| StopAllCoroutines(); | |
| _prevText = text; | |
| } | |
| if (font == null) | |
| return; | |
| // We don't care if we the font Texture changes while we are doing our Update. | |
| // The end result of cachedTextGenerator will be valid for this instance. | |
| // Otherwise we can get issues like Case 619238. | |
| m_DisableFontTextureRebuiltCallback = true; | |
| Vector2 extents = rectTransform.rect.size; | |
| var settings = GetGenerationSettings(extents); | |
| cachedTextGenerator.PopulateWithErrors(text, settings, gameObject); | |
| // Apply the offset to the vertices | |
| IList<UIVertex> verts = cachedTextGenerator.verts; | |
| float unitsPerPixel = 1 / pixelsPerUnit; | |
| //Last 4 verts are always a new line... (\n) | |
| int vertCount = verts.Count - 4; | |
| Vector2 roundingOffset = new Vector2(verts[0].position.x, verts[0].position.y) * unitsPerPixel; | |
| roundingOffset = PixelAdjustPoint(roundingOffset) - roundingOffset; | |
| toFill.Clear(); | |
| if (roundingOffset != Vector2.zero) | |
| { | |
| for (int i = 0; i < vertCount; ++i) | |
| { | |
| int tempVertsIndex = i & 3; | |
| m_TempVerts[tempVertsIndex] = verts[i]; | |
| m_TempVerts[tempVertsIndex].position *= unitsPerPixel; | |
| m_TempVerts[tempVertsIndex].position.x += roundingOffset.x; | |
| m_TempVerts[tempVertsIndex].position.y += roundingOffset.y; | |
| if (tempVertsIndex == 3) | |
| toFill.AddUIVertexQuad(m_TempVerts); | |
| } | |
| } | |
| else | |
| { | |
| charCorners.Clear(); | |
| Vector3 averagePos = Vector3.zero; | |
| List<Vector3> averagePoses = new List<Vector3>(); | |
| for (int i = 0; i < vertCount; ++i) | |
| { | |
| int tempVertsIndex = i & 3; | |
| m_TempVerts[tempVertsIndex] = verts[i]; | |
| m_TempVerts[tempVertsIndex].position *= unitsPerPixel; | |
| averagePos += m_TempVerts[tempVertsIndex].position; | |
| if (tempVertsIndex == 3) | |
| { | |
| averagePos /= 4f; | |
| averagePoses.Add(averagePos); | |
| averagePos = Vector3.zero; | |
| } | |
| } | |
| for (int i = 0; i < vertCount; ++i) | |
| { | |
| int tempVertsIndex = i & 3; | |
| m_TempVerts[tempVertsIndex] = verts[i]; | |
| m_TempVerts[tempVertsIndex].position *= unitsPerPixel; | |
| int characterIndex = i / 4; | |
| if ((_rebuildScales && i % 4 == 0) || characterIndex < _charScales.Count) | |
| _charScales.Add(0f); | |
| Vector3 toAverage = averagePoses[characterIndex] - m_TempVerts[tempVertsIndex].position; | |
| float offsetDist = 1f - _charScales[characterIndex]; | |
| if (!Application.isPlaying) | |
| offsetDist = 0f; | |
| m_TempVerts[tempVertsIndex].position += toAverage * offsetDist; | |
| charCorners.Add(transform.position + m_TempVerts[tempVertsIndex].position * unitsPerPixel); | |
| if (tempVertsIndex == 3) | |
| toFill.AddUIVertexQuad(m_TempVerts); | |
| } | |
| } | |
| _rebuildScales = false; | |
| m_DisableFontTextureRebuiltCallback = false; | |
| } | |
| public void ForceAllScaled() | |
| { | |
| for (int i = 0; i < _charScales.Count; ++i) | |
| _charScales[i] = 1f; | |
| StopAllCoroutines(); | |
| UpdateGeometry(); | |
| } | |
| public void ScaleCharacter(int charIndex) | |
| { | |
| UpdateGeometry(); | |
| StartCoroutine(ScaleCharacterRoutine(charIndex)); | |
| } | |
| IEnumerator ScaleCharacterRoutine(int charIndex) | |
| { | |
| float timer = 0f; | |
| while (timer < _scaleTime) | |
| { | |
| timer += Time.deltaTime; | |
| _charScales[charIndex] = _scaleCurve.Evaluate(timer / _scaleTime); | |
| UpdateGeometry(); | |
| yield return null; | |
| } | |
| } | |
| void OnDrawGizmosSelected() | |
| { | |
| for (int i = 0; i < charCorners.Count; i++) | |
| { | |
| if (i % 4 == 0) | |
| { | |
| Color color = Vector4.one * i / (float)charCorners.Count * 2f; | |
| color.a = 1; | |
| Gizmos.color = color; | |
| } | |
| Gizmos.DrawSphere(charCorners[i], 0.3f); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment