Skip to content

Instantly share code, notes, and snippets.

@ertdfgcvb
Last active April 15, 2018 19:03
Show Gist options
  • Save ertdfgcvb/784e322142fc9a3b07ff9db4c47eede9 to your computer and use it in GitHub Desktop.
Save ertdfgcvb/784e322142fc9a3b07ff9db4c47eede9 to your computer and use it in GitHub Desktop.
Sequencer (Unity)
using UnityEngine;
using System.Collections;
public class Sequencer : MonoBehaviour {
/*
* Sequencer v0.3
* This script allows to dynamically load numbered image sequences into a texture.
* The images must reside insie the "Assets/Resources" folder.
* path:
* the path of the image files inside the Assets/Resources
* for example: if an image sequence is located in Assets/Resources/Sequence
* the path will result in "Sequence".
* material:
* the component must have a material with a texture assigned (for example "Unlit Texture").
*/
private Material material;
private int currentFrame = 0;
private string path = "";
private bool paused = false;
private bool waiting = false;
public enum Mode {Tap, DragX, DragY, Delay};
[Header("Interactive or automatic playback")]
public Mode playMode = Mode.Delay;
[Header("Allow looping of the sequence")]
public bool loop = true;
[Header("Number of leading zeroes: 1(0), 05(1), 0012(3)")]
public int leadingZeroes = 0;
[Header("Start frame of the sequence")]
public int start = 1;
[Header("End frame of the sequence")]
public int end = 30;
[Header("Name of the folder inside Assets/Resources")]
public string folderName = "Sequence";
[Header("Base name of the files without the numbering")]
public string baseName = "frame_";
[Header("The wait time (for play mode 'Delay')")]
public float delay = 0.3f;
void Awake() {
material = GetComponent<Renderer>().material;
path = folderName + "/" + baseName;
currentFrame = start;
}
void Start () {
loadTex(start);
}
void Update () {
if (playMode == Mode.Delay) {
if (!paused && !waiting) {
waiting = true;
StartCoroutine ("DelayedStep", delay);
}
}
}
IEnumerator DelayedStep(float delay) {
yield return new WaitForSeconds(delay);
Step ();
}
void loadTex(int t){
string p = path + t.ToString ("D" + leadingZeroes);
Texture tex = (Texture)Resources.Load(p, typeof(Texture));
material.mainTexture = tex;
}
public void Rewind(){
currentFrame = start;
}
public void Play(){
paused = false;
}
public void Pause(){
paused = true;
}
void Step(){
currentFrame++;
if (currentFrame > end){
if (loop && !paused) {
currentFrame = start;
loadTex(currentFrame);
}
} else {
loadTex(currentFrame);
}
waiting = false;
}
void OnGUI(){
Event e = Event.current;
if (e.type == EventType.MouseDown){
if (playMode == Mode.Tap) {
Step ();
}
} else if (e.type == EventType.MouseDrag){
if (playMode == Mode.DragX) {
int num = end - start + 1;
int tile = (int)Screen.width / num;
int f = (int)e.mousePosition.x / tile + start;
f = Mathf.Clamp (f, start, end);
loadTex(f);
} else if (playMode == Mode.DragY) {
int num = end - start + 1;
int tile = (int)Screen.height / num;
int f = (int)e.mousePosition.y / tile + start;
f = Mathf.Clamp (f, start, end);
loadTex(f);
}
}
}
float map(float t, float inA, float inB, float outA, float outB){
return outA+(outB-outA)*((t-inA)/(inB-inA));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment