dpeter99

Anim stuff

Jan 7th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. using Malee;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using UnityEditor;
  8. using UnityEditor.Animations;
  9. using UnityEngine;
  10.  
  11. //For storing the types of animations / animation actions that the player have
  12. //For example Walk - NWSE
  13. //And the animation contoller to connect them all
  14.  
  15. /*
  16.  * Actios
  17.  *  - Name
  18.  *  - Directions
  19.  *    + Animations
  20.  *      -List:
  21.  *          + Index
  22.  *          + Event
  23.  *
  24. */
  25.  
  26.  
  27. [CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
  28. public class CharacterAnimationData : ScriptableObject
  29. {
  30.  
  31.  
  32.     //Settings
  33.     public List<AnimationAction> actions;
  34.     public TileSet referenceSheet;
  35.  
  36.     //Animator Controller
  37.     public AnimatorController animator;
  38.     //public AnimatorController Animator { get => animator; }
  39.  
  40.     //To create the AnimSetup or update it
  41.     public void Create()
  42.     {
  43.         string path = AssetDatabase.GetAssetPath(this);
  44.         AnimatorController a = AssetDatabase.LoadAssetAtPath<AnimatorController>(path);
  45.         animator = a;
  46.         //Create the conroller
  47.         if (animator == null)
  48.         {
  49.             animator = new AnimatorController();
  50.             animator.name = "Anim_cont";
  51.             AssetDatabase.AddObjectToAsset(animator, this);
  52.  
  53.         }
  54.         else if (animator.layers.Length > 0)
  55.         {
  56.             //Clear the Controller
  57.             animator.RemoveLayer(0);
  58.             foreach (var item in animator.parameters)
  59.             {
  60.                 animator.RemoveParameter(item);
  61.             }
  62.  
  63.         }
  64.  
  65.         //Add a new layer
  66.         animator.AddLayer("Base");
  67.         var rootStateMachine = animator.layers[0].stateMachine;
  68.  
  69.         //Add parameters
  70.         AnimatorControllerParameter XParam = new AnimatorControllerParameter();
  71.         XParam.name = "XParam";
  72.         XParam.type = AnimatorControllerParameterType.Float;
  73.         animator.AddParameter(XParam);
  74.  
  75.         AnimatorControllerParameter YParam = new AnimatorControllerParameter();
  76.         YParam.name = "YParam";
  77.         YParam.type = AnimatorControllerParameterType.Float;
  78.         animator.AddParameter(YParam);
  79.  
  80.         for (int i1 = 0; i1 < actions.Count; i1++)
  81.         {
  82.             AnimationAction item = (AnimationAction)actions[i1];
  83.             AnimatorState state = new AnimatorState();
  84.             state.name = item.name;
  85.  
  86.             BlendTree blend = new BlendTree();
  87.             blend.blendType = BlendTreeType.FreeformDirectional2D;
  88.             blend.name = item.name + " Blend";
  89.             blend.blendParameter = "XParam";
  90.             blend.blendParameterY = "YParam";
  91.  
  92.             blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_N").FirstOrDefault(), new Vector2(0, 1));
  93.             blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_W").FirstOrDefault(), new Vector2(-1, 0));
  94.             blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_S").FirstOrDefault(), new Vector2(1, 0));
  95.             blend.AddChild(referenceSheet.anims.Where(i => i.name == item.name + "_E").FirstOrDefault(), new Vector2(0, -1));
  96.  
  97.             state.motion = blend;
  98.  
  99.             rootStateMachine.AddState(state, new Vector2(200 + i1 * 100, i1 * 50));
  100.  
  101.         }
  102.  
  103.         AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(animator));
  104.     }
  105.  
  106.  
  107.     public enum ActionDirections
  108.     {
  109.         four_dir,
  110.         eight_dir
  111.     }
  112.  
  113.     //Stores a animation action
  114.     //The 4/8 animations for NSEW
  115.     //The Name
  116.     [System.Serializable]
  117.     public class AnimationAction
  118.     {
  119.         public string name;
  120.         //[NaughtyAttributes.OnValueChanged("CreateArray")]
  121.         public ActionDirections type;
  122.  
  123.         public AnimationClipData[] animations;
  124.  
  125.         public void CreateArray()
  126.         {
  127.             if (type == ActionDirections.four_dir)
  128.             {
  129.                 animations = new AnimationClipData[4];
  130.                 animations[0] = new AnimationClipData("North");
  131.                 animations[1] = new AnimationClipData("East");
  132.                 animations[2] = new AnimationClipData("South");
  133.                 animations[3] = new AnimationClipData("West");
  134.             }
  135.  
  136.             else if (type == ActionDirections.eight_dir)
  137.             {
  138.                 animations = new AnimationClipData[8];
  139.                 animations[0] = new AnimationClipData("North");
  140.                 animations[1] = new AnimationClipData("North-East");
  141.                 animations[2] = new AnimationClipData("East");
  142.                 animations[3] = new AnimationClipData("South-East");
  143.                 animations[4] = new AnimationClipData("South");
  144.                 animations[5] = new AnimationClipData("South-West");
  145.                 animations[6] = new AnimationClipData("West");
  146.                 animations[7] = new AnimationClipData("North-West");
  147.             }
  148.         }
  149.     }
  150.  
  151.     [System.Serializable]
  152.     public class AnimationClipData
  153.     {
  154.         public string name;
  155.         public List<AnimationFrameData> frames = new List<AnimationFrameData>();
  156.  
  157.         public AnimationClipData(string name)
  158.         {
  159.             this.name = name;
  160.             frames = new List<AnimationFrameData>();
  161.         }
  162.     }
  163.  
  164.     [System.Serializable]
  165.     public class AnimationFrameData :ScriptableObject
  166.     {
  167.         public void OnEnable() { hideFlags = HideFlags.HideAndDontSave; }
  168.  
  169.         [NaughtyAttributes.MaxValue(0)]
  170.         public int spriteId;
  171.         public bool eventmarker;
  172.         public float time;
  173.  
  174.        
  175.     }
  176.  
  177.     [System.Serializable]
  178.     public class AnimationFrameSequenceData : AnimationFrameData
  179.     {
  180.         public int min;
  181.         public int max;
  182.         public int action;
  183.  
  184.        
  185.     }
  186.  
  187. }
Advertisement
Add Comment
Please, Sign In to add comment