Last active
February 20, 2017 00:39
Star
You must be signed in to star a gist
[Unity] ポップする数字
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
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine.UI; | |
namespace TSKT | |
{ | |
[RequireComponent(typeof(Text))] | |
public class Popping : BaseMeshEffect | |
{ | |
const int VertexCountPerChar = 6; | |
[Range(0.01f, 100f)] | |
public float popLength = 40f; | |
[Range(0f, 1f)] | |
public float charInterval = 0.1f; | |
[Range(0.01f, 1f)] | |
public float popDuration = 0.4f; | |
float? startedTime; | |
bool finished = false; | |
public override void ModifyMesh(VertexHelper vh) | |
{ | |
if (!IsActive()) | |
{ | |
return; | |
} | |
var vertices = UIVerticesPool.Get(); | |
vh.GetUIVertexStream(vertices); | |
ModifyMesh(vertices); | |
vh.Clear(); | |
vh.AddUIVertexTriangleStream(vertices); | |
} | |
void ModifyMesh(List<UIVertex> list) | |
{ | |
var charCount = list.Count / VertexCountPerChar; | |
if (ElapsedTime > GetDuration(charCount)) | |
{ | |
finished = true; | |
return; | |
} | |
for (int i = 0; i < charCount; ++i) | |
{ | |
float timeOffset = -charInterval * (charCount - 1 - i); | |
float time = ElapsedTime + timeOffset; | |
if (time < 0f) | |
{ | |
for (int j = 0; j < VertexCountPerChar; ++j) | |
{ | |
var vertex = list[i * VertexCountPerChar + j]; | |
vertex.color = Color.clear; | |
list[i * VertexCountPerChar + j] = vertex; | |
} | |
} | |
else if (time < popDuration) | |
{ | |
float normalizedTime = time / popDuration; | |
float yOffset = -normalizedTime * (normalizedTime - 1f) * popLength * 4f; | |
for (int j = 0; j < VertexCountPerChar; ++j) | |
{ | |
var vertex = list[i * VertexCountPerChar + j]; | |
vertex.position.y += yOffset; | |
list[i * VertexCountPerChar + j] = vertex; | |
} | |
} | |
} | |
} | |
Text text; | |
Text Text | |
{ | |
get | |
{ | |
return text ?? (text = GetComponent<Text>()); | |
} | |
} | |
void Update() | |
{ | |
if (finished) | |
{ | |
return; | |
} | |
Text.SetAllDirty(); | |
} | |
float ElapsedTime | |
{ | |
get | |
{ | |
if (!startedTime.HasValue) | |
{ | |
startedTime = Time.time; | |
} | |
return Time.time - startedTime.Value; | |
} | |
} | |
float GetDuration(int charCount) | |
{ | |
return popDuration + (charCount - 1) * charInterval; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こっちも必要
https://gist.github.com/enue/9999df944dd39bd1fc3a2677e14db26e