Last active
December 1, 2018 07:31
-
-
Save kankikuchi/3d2125bf6bc2697e2ccc632dafbdf65f to your computer and use it in GitHub Desktop.
AnimatorControllerを使わずに簡単にアニメーションを再生出来るSimpleAnimation【Unity】
This file contains hidden or 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
// SimpleAnimationChanger.cs | |
// http://kan-kikuchi.hatenablog.com/entry/SimpleAnimation | |
// | |
// Created by kan.kikuchi on 2018.07.19. | |
using UnityEngine; | |
using System.Collections.Generic; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
/// <summary> | |
/// SimpleAnimationのアニメーションを切り替えるクラス | |
/// </summary> | |
public class SimpleAnimationChanger : MonoBehaviour { | |
//切り替える対象のアニメーション | |
[SerializeField] | |
private SimpleAnimation _simpleAnimation = null; | |
[SerializeField] | |
private AnimationClip _animationClip = null; | |
private AnimationClip _beforeAnimationClip = null; | |
//アニメーションの状態の名前のリスト | |
private Dictionary<string, string> _stateNameDict = new Dictionary<string, string>(); | |
public Dictionary<string, string> StateNameDict{ get { return _stateNameDict; }} | |
//初期化したか | |
private bool _isInitialized = false; | |
//================================================================================= | |
//初期化 | |
//================================================================================= | |
private void Start() { | |
if(_simpleAnimation != null){ | |
Init(); | |
} | |
} | |
/// <summary> | |
/// SimpleAnimationを設定する | |
/// </summary> | |
public void SetSimpleAnimation(SimpleAnimation simpleAnimation){ | |
_simpleAnimation = simpleAnimation; | |
Init(); | |
} | |
//初期化 | |
private void Init(){ | |
if(_isInitialized){ | |
return; | |
} | |
_isInitialized = true; | |
_beforeAnimationClip = _animationClip; | |
foreach (var state in _simpleAnimation.GetStates()) { | |
_stateNameDict[state.clip.name] = state.name; | |
} | |
} | |
private void OnValidate() { | |
if(!Application.isPlaying){ | |
return; | |
} | |
if(_isInitialized && _beforeAnimationClip != _animationClip){ | |
AddStateIfneeded(); | |
_beforeAnimationClip = _animationClip; | |
} | |
} | |
//================================================================================= | |
//変更 | |
//================================================================================= | |
/// <summary> | |
/// アニメーションを切り替える | |
/// </summary> | |
public void Change (string stateName) { | |
if(_simpleAnimation == null){ | |
Debug.LogError("_simpleAnimationがありません!"); | |
return; | |
} | |
if (_simpleAnimation.GetState(stateName) == null) { | |
Debug.LogError(stateName + "というアニメーションがありません!"); | |
return; | |
} | |
_simpleAnimation.CrossFade(stateName, 0.5f); | |
} | |
//================================================================================= | |
//追加 | |
//================================================================================= | |
//アニメーションを追加する必要があれば追加 | |
private void AddStateIfneeded(){ | |
string stateName = _animationClip.name; | |
if (_simpleAnimation.GetState(stateName) == null) { | |
_simpleAnimation.AddState(_animationClip, stateName); | |
_stateNameDict[_animationClip.name] = stateName; | |
} | |
Change(stateName); | |
} | |
} | |
#if UNITY_EDITOR | |
/// <summary> | |
/// SimpleAnimationのインスペクターの表示を変えるクラス | |
/// </summary> | |
[CustomEditor(typeof(SimpleAnimationChanger))] | |
public class SimpleAnimationChangerEditor : Editor { | |
//================================================================================= | |
//更新 | |
//================================================================================= | |
public override void OnInspectorGUI() { | |
base.OnInspectorGUI(); | |
//実行中だけ表示 | |
if (!Application.isPlaying) { | |
return; | |
} | |
SimpleAnimationChanger simpleAnimationChanger = target as SimpleAnimationChanger; | |
//変更ボタン | |
EditorGUILayout.Space(); | |
EditorGUILayout.LabelField("---------アニメ切り替えボタン---------"); | |
int count = 0; | |
foreach (var statePair in simpleAnimationChanger.StateNameDict) { | |
if (count % 5 == 0) { | |
if (count != 0) { | |
EditorGUILayout.EndHorizontal(); | |
} | |
EditorGUILayout.BeginHorizontal(GUI.skin.box); | |
} | |
if (GUILayout.Button(statePair.Key)) { | |
simpleAnimationChanger.Change(statePair.Value); | |
} | |
count++; | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment