Skip to content

Instantly share code, notes, and snippets.

@enue
Last active February 20, 2017 00:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save enue/cfce564d8cd6a5fb8a24d35820384b4e to your computer and use it in GitHub Desktop.
[Unity] ポップする数字
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;
}
}
}
@enue
Copy link
Author

enue commented Feb 18, 2017

@enue
Copy link
Author

enue commented Feb 20, 2017

unity-5 5 1p4-64bit-fantasynumber

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment