Last active
September 11, 2018 22:12
-
-
Save dotsquid/647148b52ba5eff35b61a1039d80cb21 to your computer and use it in GitHub Desktop.
So today my Windows 10 was updating for about 40 minutes (probably patch for Meltdown vulnerability) and this has taken a heavy toll. Put this Component on a GameObject which has a banch of children SpriteRenderers. Play with parameters. Have fun.
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; | |
namespace Tests.Spinner | |
{ | |
public class Spinner : MonoBehaviour | |
{ | |
private class Part | |
{ | |
private Data _data; | |
private Transform _transform; | |
private SpriteRenderer _sprite; | |
private float _phase; | |
public Part(Data data, float phase, SpriteRenderer sprite) | |
{ | |
_data = data; | |
_phase = phase; | |
_sprite = sprite; | |
_transform = sprite.transform; | |
} | |
public void Update(float dt) | |
{ | |
_phase = Mathf.Repeat(_phase + dt * _data.speed, 2.0f); | |
float t = 1.0f - _phase; | |
float p = Mathf.Pow(Mathf.Abs(t), _data.pow) * Mathf.Sign(t); | |
float theta = 2.0f * Mathf.PI * (p + _data.offset); | |
Vector3 position = default(Vector3); | |
position.x = _data.radius * Mathf.Cos(theta); | |
position.y = _data.radius * Mathf.Sin(theta); | |
_transform.localPosition = position; | |
var color = _sprite.color; | |
color.a = Mathf.Sign((_phase - _data.clip) * (2.0f - _data.clip - _phase)); | |
_sprite.color = color; | |
} | |
} | |
private struct Data | |
{ | |
public float offset; | |
public float radius; | |
public float speed; | |
public float clip; | |
public float pow; | |
} | |
[SerializeField] | |
private float _radius = 0.25f; | |
[SerializeField] | |
private float _speed = 0.7f; | |
[SerializeField] | |
private float _pow = 0.7f; | |
[SerializeField] | |
private float _offset = -0.25f; | |
[SerializeField] | |
private float _lag = 0.08f; | |
[SerializeField] | |
private float _clip = 0.2f; | |
private Part[] _parts; | |
private Data _data; | |
private void Awake() | |
{ | |
StoreData(); | |
CaptureParts(); | |
} | |
private void OnValidate() | |
{ | |
StoreData(); | |
CaptureParts(); | |
} | |
private void Update() | |
{ | |
float dt = Time.deltaTime; | |
foreach (var part in _parts) | |
part.Update(dt); | |
} | |
private void StoreData() | |
{ | |
_data = new Data() | |
{ | |
offset = _offset, | |
radius = _radius, | |
speed = _speed, | |
clip = _clip, | |
pow = _pow | |
}; | |
} | |
private void CaptureParts() | |
{ | |
var sprites = GetComponentsInChildren<SpriteRenderer>(); | |
int count = sprites.Length; | |
_parts = new Part[count]; | |
for (int i = 0; i < count; ++i) | |
{ | |
var sprite = sprites[i]; | |
var part = new Part(_data, i * _lag, sprite); | |
_parts[i] = part; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment