Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active August 29, 2015 14:14
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 tsubaki/497d81dbca51084a708d to your computer and use it in GitHub Desktop.
Save tsubaki/497d81dbca51084a708d to your computer and use it in GitHub Desktop.
AnimatorをInactiveにしても状態を復帰させる
using UnityEngine;
using System.Collections;
public class AnimatorParameter : ScriptableObject
{
[SerializeField]
RuntimeAnimatorController animatorController;
public AnimatorParameterInfo[] parameterInfos;
[System.Serializable]
public struct AnimatorParameterInfo
{
public ParameterType type;
public int hashName;
public string name;
public enum ParameterType
{
NONE,
BOOL,
FLOAT,
INT,
TRIGGER
}
}
#if UNITY_EDITOR
[ContextMenu("Setup")]
void Setup()
{
var controller = animatorController as UnityEditorInternal.AnimatorController;
parameterInfos = new AnimatorParameterInfo[controller.parameterCount];
UnityEditor.AssetDatabase.RenameAsset(UnityEditor.AssetDatabase.GetAssetPath(this), animatorController.name);
name = animatorController.name;
for (int i=0; i<controller.parameterCount; i++) {
var param = controller.GetParameter (i);
parameterInfos [i].hashName = Animator.StringToHash (param.name);
parameterInfos[i].name = param.name;
if (param.type == UnityEditorInternal.AnimatorControllerParameterType.Bool) {
parameterInfos [i].type = AnimatorParameterInfo.ParameterType.BOOL;
} else if (param.type == UnityEditorInternal.AnimatorControllerParameterType.Float) {
parameterInfos [i].type = AnimatorParameterInfo.ParameterType.FLOAT;
} else if (param.type == UnityEditorInternal.AnimatorControllerParameterType.Int) {
parameterInfos [i].type = AnimatorParameterInfo.ParameterType.INT;
} else if (param.type == UnityEditorInternal.AnimatorControllerParameterType.Trigger) {
parameterInfos [i].type = AnimatorParameterInfo.ParameterType.TRIGGER;
}
}
UnityEditor.EditorUtility.SetDirty(this);
}
[UnityEditor.CustomEditor(typeof(AnimatorParameter)), UnityEditor.CanEditMultipleObjects]
class AnimatorParameterEditor : UnityEditor.Editor
{
public override void OnInspectorGUI ()
{
base.OnInspectorGUI ();
UnityEditor.EditorGUILayout.Space();
if( GUILayout.Button("Setup") ){
foreach( var obj in targets )
{
var animparameter = obj as AnimatorParameter;
animparameter.Setup();
}
}
}
}
#endif
}
using UnityEngine;
using System.Collections;
using System.Linq;
using System;
[DisallowMultipleComponent]
public class ResumeAnimator : MonoBehaviour
{
[System.Serializable]
struct LayerInfo
{
public int nameHash;
public float time;
}
private Animator anim;
private LayerInfo[] layerInfo = null;
private object[] parameters ;
[SerializeField]
AnimatorParameter parameter;
void Awake ()
{
anim = GetComponent<Animator> ();
layerInfo = new LayerInfo[anim.layerCount];
for (int i=0; i<layerInfo.Length; i++) {
layerInfo [i] = new LayerInfo ();
}
}
void OnEnable ()
{
Restore ();
}
public void Restore ()
{
for (int currentLayerCount=0; currentLayerCount<anim.layerCount; currentLayerCount++) {
var layer = layerInfo [currentLayerCount];
anim.Play (layer.nameHash, currentLayerCount, layer.time);
}
if (parameter == null || parameters == null)
return;
for (int i=0; i<parameter.parameterInfos.Length; i++) {
if( parameters[i] == null )
continue;
var parameterInfo = parameter.parameterInfos [i];
if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.BOOL) {
anim.SetBool (parameterInfo.hashName, (bool)parameters [i]);
} else if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.FLOAT) {
anim.SetFloat (parameterInfo.hashName, (float)parameters [i]);
} else if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.INT) {
anim.SetInteger (parameterInfo.hashName, (int)parameters [i]);
} else if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.TRIGGER) {
anim.SetBool (parameterInfo.hashName, (bool)parameters [i]);
}
}
}
public void Save ()
{
for (int currentLayerCount=0; currentLayerCount<anim.layerCount; currentLayerCount++) {
layerInfo [currentLayerCount].nameHash = anim.GetCurrentAnimatorStateInfo (currentLayerCount).nameHash;
layerInfo [currentLayerCount].time = anim.GetCurrentAnimatorStateInfo (currentLayerCount).normalizedTime;
}
if (parameter == null)
return;
if( parameters == null )
parameters = new object[parameter.parameterInfos.Length];
for (int i=0; i<parameter.parameterInfos.Length; i++) {
var parameterInfo = parameter.parameterInfos [i];
if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.BOOL) {
parameters [i] = anim.GetBool (parameterInfo.hashName);
} else if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.FLOAT) {
parameters [i] = anim.GetFloat (parameterInfo.hashName);
} else if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.INT) {
parameters [i] = anim.GetInteger (parameterInfo.hashName);
} else if (parameterInfo.type == AnimatorParameter.AnimatorParameterInfo.ParameterType.TRIGGER) {
parameters [i] = anim.GetBool (parameterInfo.hashName);
}
}
}
public static void RestoreAnimator (GameObject target)
{
foreach (var resumeAnim in target.GetComponentsInChildren<ResumeAnimator>()) {
resumeAnim.Save ();
}
}
#if UNITY_EDITOR
public static void AddResumeAnimatorInChildren (GameObject obj)
{
var components = obj.GetComponentsInChildren<Animator> ();
foreach (var item in components) {
ResumeAnimator resumeAnim = item.GetComponent<ResumeAnimator> ();
if (resumeAnim == null)
resumeAnim = item.gameObject.AddComponent<ResumeAnimator> ();
}
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment