Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Malee;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using UnityEditor;
- using UnityEditor.Animations;
- using UnityEngine;
- //For storing the types of animations / animation actions that the player have
- //For example Walk - NWSE
- //And the animation contoller to connect them all
- /*
- * Actios
- * - Name
- * - Directions
- * + Animations
- * -List:
- * + Index
- * + Event
- *
- */
- [CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
- public class CharacterAnimationData : ScriptableObject
- {
- //Settings
- public List<AnimationAction> actions;
- public TileSet referenceSheet;
- //Animator Controller
- public AnimatorController animator;
- //public AnimatorController Animator { get => animator; }
- //To create the AnimSetup or update it
- public void Create()
- {
- string path = AssetDatabase.GetAssetPath(this);
- AnimatorController a = AssetDatabase.LoadAssetAtPath<AnimatorController>(path);
- animator = a;
- //Create the conroller
- if (animator == null)
- {
- animator = new AnimatorController();
- animator.name = "Anim_cont";
- AssetDatabase.AddObjectToAsset(animator, this);
- }
- else if (animator.layers.Length > 0)
- {
- //Clear the Controller
- animator.RemoveLayer(0);
- foreach (var item in animator.parameters)
- {
- animator.RemoveParameter(item);
- }
- }
- //Add a new layer
- animator.AddLayer("Base");
- var rootStateMachine = animator.layers[0].stateMachine;
- //Add parameters
- AnimatorControllerParameter XParam = new AnimatorControllerParameter();
- XParam.name = "XParam";
- XParam.type = AnimatorControllerParameterType.Float;
- animator.AddParameter(XParam);
- AnimatorControllerParameter YParam = new AnimatorControllerParameter();
- YParam.name = "YParam";
- YParam.type = AnimatorControllerParameterType.Float;
- animator.AddParameter(YParam);
- for (int i1 = 0; i1 < actions.Count; i1++)
- {
- AnimationAction item = (AnimationAction)actions[i1];
- AnimatorState state = new AnimatorState();
- state.name = item.name;
- BlendTree blend = new BlendTree();
- blend.blendType = BlendTreeType.FreeformDirectional2D;
- blend.name = item.name + " Blend";
- blend.blendParameter = "XParam";
- blend.blendParameterY = "YParam";
- blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_N").FirstOrDefault(), new Vector2(0, 1));
- blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_W").FirstOrDefault(), new Vector2(-1, 0));
- blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_S").FirstOrDefault(), new Vector2(1, 0));
- blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_E").FirstOrDefault(), new Vector2(0, -1));
- state.motion = blend;
- rootStateMachine.AddState(state, new Vector2(200 + i1 * 100, i1 * 50));
- }
- AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animator));
- }
- public enum ActionDirections
- {
- four_dir,
- eight_dir
- }
- //Stores a animation action
- //The 4/8 animations for NSEW
- //The Name
- [System.Serializable]
- public class AnimationAction
- {
- public string name;
- //[NaughtyAttributes.OnValueChanged("CreateArray")]
- public ActionDirections type;
- public AnimationClipData[] animations;
- public void CreateArray()
- {
- if (type == ActionDirections.four_dir)
- {
- animations = new AnimationClipData[4];
- animations[0] = new AnimationClipData("North");
- animations[1] = new AnimationClipData("East");
- animations[2] = new AnimationClipData("South");
- animations[3] = new AnimationClipData("West");
- }
- else if (type == ActionDirections.eight_dir)
- {
- animations = new AnimationClipData[8];
- animations[0] = new AnimationClipData("North");
- animations[1] = new AnimationClipData("North-East");
- animations[2] = new AnimationClipData("East");
- animations[3] = new AnimationClipData("South-East");
- animations[4] = new AnimationClipData("South");
- animations[5] = new AnimationClipData("South-West");
- animations[6] = new AnimationClipData("West");
- animations[7] = new AnimationClipData("North-West");
- }
- }
- }
- [System.Serializable]
- public class AnimationClipData
- {
- public string name;
- public List<AnimationFrameData> frames = new List<AnimationFrameData>();
- public AnimationClipData(string name)
- {
- this.name = name;
- frames = new List<AnimationFrameData>();
- }
- }
- [System.Serializable]
- public class AnimationFrameData :ScriptableObject
- {
- public void OnEnable() { hideFlags = HideFlags.HideAndDontSave; }
- [NaughtyAttributes.MaxValue(0)]
- public int spriteId;
- public bool eventmarker;
- public float time;
- }
- [System.Serializable]
- public class AnimationFrameSequenceData : AnimationFrameData
- {
- public int min;
- public int max;
- public int action;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment