-
-
Save GarethIW/4feac837044196bb2058945c400a1d2b to your computer and use it in GitHub Desktop.
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
///////////////////////////////////////////////////////////////////////// | |
// | |
// PicaVoxel - The tiny voxel engine for Unity - http://picavoxel.com | |
// By Gareth Williams - @garethiw - http://gareth.pw | |
// | |
// Source code distributed under standard Asset Store licence: | |
// http://unity3d.com/legal/as_terms | |
// | |
///////////////////////////////////////////////////////////////////////// | |
using System; | |
using UnityEngine; | |
using System.Collections; | |
using Random = UnityEngine.Random; | |
namespace PicaVoxel | |
{ | |
[AddComponentMenu("PicaVoxel/Utilities/Basic Animator")] | |
[Serializable] | |
public class BasicAnimator : MonoBehaviour | |
{ | |
public float Interval = 0.1f; | |
public bool PingPong = false; | |
public bool Loop = true; | |
public bool RandomStartFrame = false; | |
public bool PlayOnAwake = true; | |
public bool IsPlaying = false; | |
public int CurrentFrame = 0; | |
public int NumFrames = 0; | |
private Volume voxelObject; | |
private float currentFrameTime = 0f; | |
private int dir = 1; | |
private bool isBakedVolume = false; | |
private void Awake() | |
{ | |
voxelObject = GetComponent<Volume>(); | |
// If we can't find a Volume component, check to see if this is a baked Volume | |
if (voxelObject == null) | |
{ | |
for(int i=0;i<transform.childCount;i++) | |
if (transform.GetChild(i).name.ToLower().StartsWith("frame")) NumFrames++; | |
// If we found some Frame objects, we're on a baked Volume | |
if (NumFrames > 0) isBakedVolume = true; | |
} | |
else | |
{ | |
NumFrames = voxelObject.NumFrames; | |
} | |
Reset(); | |
if (PlayOnAwake) Play(); | |
} | |
// Update is called once per frame | |
private void Update() | |
{ | |
if ((voxelObject == null && !isBakedVolume) || (NumFrames<=0) || !IsPlaying) return; | |
currentFrameTime += Time.deltaTime; | |
if (currentFrameTime >= Interval) | |
{ | |
currentFrameTime = 0; | |
CurrentFrame += dir; | |
if (dir == 1 && CurrentFrame == NumFrames) | |
{ | |
if (PingPong) | |
{ | |
CurrentFrame--; | |
dir = -1; | |
} | |
else CurrentFrame = 0; | |
if (!Loop) Reset(); | |
} | |
else if (dir == -1 && CurrentFrame == 0) | |
{ | |
dir = 1; | |
if (!Loop) Reset(); | |
} | |
if(!isBakedVolume) | |
voxelObject.SetFrame(CurrentFrame); | |
else | |
{ | |
int thisFrame = 0; | |
for(int i=0;i<transform.childCount;i++) | |
if (transform.GetChild(i).name.ToLower().StartsWith("frame")) | |
{ | |
transform.GetChild(i).gameObject.SetActive(false); | |
if (CurrentFrame == thisFrame) | |
{ | |
transform.GetChild(i).gameObject.SetActive(true); | |
} | |
thisFrame++; | |
} | |
} | |
} | |
} | |
public void Play() | |
{ | |
IsPlaying = true; | |
} | |
public void Pause() | |
{ | |
IsPlaying = false; | |
} | |
public void Reset() | |
{ | |
IsPlaying = false; | |
if (RandomStartFrame) CurrentFrame = Random.Range(0, NumFrames-1); | |
else CurrentFrame = 0; | |
currentFrameTime = 0; | |
} | |
} | |
} |
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
///////////////////////////////////////////////////////////////////////// | |
// | |
// PicaVoxel - The tiny voxel engine for Unity - http://picavoxel.com | |
// By Gareth Williams - @garethiw - http://gareth.pw | |
// | |
// Source code distributed under standard Asset Store licence: | |
// http://unity3d.com/legal/as_terms | |
// | |
///////////////////////////////////////////////////////////////////////// | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using UnityEditor; | |
using UnityEngine; | |
namespace PicaVoxel | |
{ | |
[CustomEditor(typeof (BasicAnimator))] | |
public class BasicAnimatorEditor : Editor | |
{ | |
private bool playOnAwake; | |
private bool pingPong; | |
private bool loop; | |
private bool randomStartFrame; | |
private float interval; | |
private BasicAnimator basicAnimator; | |
private void OnEnable() | |
{ | |
basicAnimator = (BasicAnimator) target; | |
interval = basicAnimator.Interval; | |
playOnAwake = basicAnimator.PlayOnAwake; | |
loop = basicAnimator.Loop; | |
pingPong = basicAnimator.PingPong; | |
randomStartFrame = basicAnimator.RandomStartFrame; | |
} | |
public override void OnInspectorGUI() | |
{ | |
EditorGUILayout.Space(); | |
interval = EditorGUILayout.FloatField("Frame interval:", interval); | |
if (interval != basicAnimator.Interval) | |
{ | |
if (interval < 0f) basicAnimator.Interval = 0f; | |
basicAnimator.Interval = interval; | |
} | |
loop = EditorGUILayout.ToggleLeft(new GUIContent(" Loop"), loop); | |
if (loop != basicAnimator.Loop) basicAnimator.Loop = loop; | |
pingPong = EditorGUILayout.ToggleLeft(new GUIContent(" Ping pong"), pingPong); | |
if (pingPong != basicAnimator.PingPong) basicAnimator.PingPong = pingPong; | |
randomStartFrame = EditorGUILayout.ToggleLeft(new GUIContent(" Random start frame"), randomStartFrame); | |
if (randomStartFrame != basicAnimator.RandomStartFrame) basicAnimator.RandomStartFrame = randomStartFrame; | |
playOnAwake = EditorGUILayout.ToggleLeft(new GUIContent(" Play on Awake"), playOnAwake); | |
if (playOnAwake != basicAnimator.PlayOnAwake) basicAnimator.PlayOnAwake = playOnAwake; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment