Skip to content

Instantly share code, notes, and snippets.

@rubit0
Last active November 22, 2015 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubit0/ca86dc31c708d6107aa6 to your computer and use it in GitHub Desktop.
Save rubit0/ca86dc31c708d6107aa6 to your computer and use it in GitHub Desktop.
NextGenSprites Helper for Sprite Sheet Animations
using UnityEngine;
using NextGenSprites.Helpers;
using System.Collections.Generic;
namespace NextGenSprites.Animation
{
[RequireComponent (typeof(SpriteRenderer))]
public class SpriteAnimationHandler : MonoBehaviour
{
public bool SharedMaterial;
public TriggerCollections[] Triggers;
private Dictionary<string, TriggerCollections> _dictTriggers = new Dictionary<string, TriggerCollections> ();
private Material _material;
private Animator _anim;
private bool _InitDone = false;
/// <summary>
/// Update Material Textures by the Trigger Id's corresponding to your Animator Controller.
/// </summary>
/// <param name="triggerId">Use a string to match with the desired Trigger on your Animator</param>
/// <param name="updateAnimator">Set false to not set the Trigger on the Animator</param>
public void UpdateMaterial (string triggerId, bool updateAnimator = true)
{
if (!_InitDone) {
Init ();
Debug.Log ("AnimationHandler has been auto initialised.\nIt's recommended to invoke Init() inside the Awake() or Start() callback.");
}
if (SetTextures (triggerId)) {
if (updateAnimator)
_anim.SetTrigger (triggerId);
}
}
/// <summary>
/// Update Material Textures by the Boolean Id's corresponding to your Animator Controller.
/// </summary>
/// <param name="triggerId">Use a string to match with the desired Trigger on your Animator</param>
/// <param name="boolValue">Set the paramater true or false</param>
/// <param name="updateAnimator">Set false to not set the bool balue on the Animator</param>
public void UpdateMaterial (string triggerId, bool boolValue, bool updateAnimator = true)
{
if (!_InitDone) {
Init ();
Debug.Log ("AnimationHandler has been auto initialised.\nIt's recommended to invoke Init() inside the Awake() or Start() callback.");
}
if (SetTextures (triggerId)) {
if (updateAnimator)
_anim.SetBool (triggerId, boolValue);
}
}
/// <summary>
/// Initialises the Animation Handler.
/// </summary>
/// <param name="animatorTarget">The Animator we Target</param>
public void Init (Animator animatorTarget = null)
{
//Check if an Animator is pressent
if (animatorTarget == null) {
//Try to find one on the holding GameObject
_anim = GetComponent<Animator> ();
//Return a message on this subject
if (_anim == null) {
Debug.LogError ("No Animator assisned!\nAborting initialisation.");
return;
} else {
Debug.LogWarning ("You have no Animator assigned.\nBut an Animator compoment has been found and automatically assigned.");
}
} else {
_anim = animatorTarget;
}
//Get the Material
if (SharedMaterial)
_material = GetComponent<SpriteRenderer> ().sharedMaterial;
else
_material = GetComponent<SpriteRenderer> ().material;
//Fill the Dictionary with the TriggerCollections
foreach (var item in Triggers) {
_dictTriggers.Add (item.TriggerName, item);
}
_InitDone = true;
}
//Set the Textures on the Material and return if it could complete
private bool SetTextures (string animatorParameters)
{
if (_dictTriggers.ContainsKey (animatorParameters)) {
foreach (var item in _dictTriggers[animatorParameters].Pair) {
_material.SetTexture (item.TargetProperty.GetString (), item.TargetTexture);
}
return true;
} else {
Debug.LogError ("There is no matching Key for" + animatorParameters);
return false;
}
}
}
}
namespace NextGenSprites.Helpers
{
//Our Data Model
[System.Serializable]
public class TriggerCollections
{
public string TriggerName;
public TexturePairs[] Pair;
}
//Helper Model class since Unity does not support .NET Tuple's
[System.Serializable]
public class TexturePairs
{
public Texture2D TargetTexture;
public NextGenSprites.ShaderTexture TargetProperty;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment