Create a gist now

Instantly share code, notes, and snippets.

@enue /Popping.cs
Last active Feb 20, 2017

What would you like to do?
[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;
}
}
}
Owner

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