Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using Character;
- using Character.Sprites;
- using UnityEditor;
- using UnityEngine;
- namespace EditorTools.Sprites
- {
- public class AnimationGenerator: ScriptableWizard
- {
- [SerializeField] private string pieceName;
- [SerializeField] private Animator destinationAnimator;
- [SerializeField] private List<ActionData> actions = new();
- [SerializeField] private List<Direction> directions = new()
- {
- Direction.Left,
- Direction.LefUp,
- Direction.Up,
- Direction.RightUp,
- Direction.Right,
- Direction.RightDown,
- Direction.Down,
- Direction.LeftDown
- };
- [SerializeField] private List<Sprite> sprites = new();
- private const string PATH = "Assets/Animations";
- private Sprite[,] sprites2dArray;
- [MenuItem("Tools/Animation Generator")]
- public static void ShowWindow()
- {
- DisplayWizard<AnimationGenerator>("Animation Generator", "Generate");
- }
- public void OnWizardCreate()
- {
- sprites2dArray = new Sprite[directions.Count, actions.Count];
- DistributeSpritesInto2DArray();
- CreateAssets();
- }
- private void CreateAssets()
- {
- AssetDatabase.CreateFolder(PATH, pieceName);
- foreach (var direction in directions)
- {
- foreach (var action in actions)
- {
- CreateAnimationAsset(direction, action);
- }
- }
- }
- private void CreateAnimationAsset(Direction direction, ActionData actionData)
- {
- var animationClip = new AnimationClip
- {
- name = $"{pieceName.ToUpper()}_{actionData.Id}_{direction.ToString()}",
- frameRate = 12,
- wrapMode = ActionToWrapMode(actionData),
- };
- //Create the CurveBinding
- var spriteBinding = new EditorCurveBinding
- {
- type = typeof(SpriteRenderer),
- path = "",
- propertyName = "m_Sprite"
- };
- var spriteKeyFrames = new ObjectReferenceKeyframe[actionData.FramesAmount];
- var currentFrame = 0;
- for (int j = 0; j < actionData.FramesAmount; j++)
- {
- spriteKeyFrames[j] = new ObjectReferenceKeyframe
- {
- time = j / animationClip.frameRate,
- value = sprites2dArray[(int) direction, currentFrame]
- };
- currentFrame++;
- }
- AnimationUtility.SetObjectReferenceCurve(animationClip, spriteBinding, spriteKeyFrames);
- //Set Loop Time to True
- var settings = AnimationUtility.GetAnimationClipSettings(animationClip);
- settings.loopTime = true;
- AnimationUtility.SetAnimationClipSettings(animationClip, settings);
- AssetDatabase.CreateAsset(animationClip, $"{PATH}/{pieceName}/{animationClip.name}.anim");
- AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
- }
- private void DistributeSpritesInto2DArray()
- {
- for (int row = 0; row < directions.Count; row++)
- {
- for (int column = 0; column < actions.Count; column++)
- {
- sprites2dArray[row, column] = sprites[row * actions.Count + column];
- }
- }
- }
- private static WrapMode ActionToWrapMode(ActionData action)
- {
- switch (action.AnimationType)
- {
- case AnimationType.OneShot:
- return WrapMode.Once;
- case AnimationType.Looping:
- return WrapMode.Loop;
- case AnimationType.PingPong:
- default:
- Debug.LogWarning($"{action.AnimationType} is not supported. Returning WrapMode.Once");
- return WrapMode.Once;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement