A 2D Farming RPG
This is just a simple 2D farming RPG game
Loading...
Searching...
No Matches
AnimationOverrides.cs
Go to the documentation of this file.
1using System.Diagnostics;
2using System.Collections.Generic;
3using UnityEngine;
4
5public class AnimationOverrides : MonoBehaviour
6{
7 [SerializeField] private GameObject m_Character = null;
8 [SerializeField] private SO_AnimationType[] m_SOAnimationTypeArray = null;
9
10 private Dictionary<AnimationClip, SO_AnimationType> m_AnimationTypeDictionaryByAnimation;
11 private Dictionary<string, SO_AnimationType> m_AnimationTypeDictionaryByCompositeAttributeKey;
12
16 private void Start()
17 {
18 // Initialize animation type dictionary keyed by animation key
19 m_AnimationTypeDictionaryByAnimation = new Dictionary<AnimationClip, SO_AnimationType>();
20
22 {
24 }
25
26 // Initialize animation type dictionary keyed by string
27 m_AnimationTypeDictionaryByCompositeAttributeKey = new Dictionary<string, SO_AnimationType>();
28
30 {
31 string key = item.m_CharacterPart.ToString() + item.m_PartVariantColor.ToString() + item.m_PartVariantType.ToString() + item.m_AnimationName.ToString();
33 }
34
35 }
36
41 public void ApplyCharacterCustomisationParameters( List<CharacterAttribute> characterAttributesList )
42 {
43 // Loop through all character attributes and set the animation override controller for each
44 foreach( CharacterAttribute characterAttribute in characterAttributesList )
45 {
46 Animator currentAnimator = null;
47 List<KeyValuePair<AnimationClip, AnimationClip>> animsKeyValuePairList = new List<KeyValuePair<AnimationClip, AnimationClip>>();
48
49 string animatorSOAssetName = characterAttribute.characterPart.ToString();
50
51 // Find animators in scene that match scriptable object animator type
52 Animator[] animatorsArray = m_Character.GetComponentsInChildren<Animator>();
53
54 foreach( Animator animator in animatorsArray )
55 {
56 if ( animator.name == animatorSOAssetName )
57 {
58 currentAnimator = animator;
59 break;
60 }
61 }
62
63 // Get base current animations for animator
64 AnimatorOverrideController aoc = new AnimatorOverrideController( currentAnimator.runtimeAnimatorController );
65 List<AnimationClip> animationList = new List<AnimationClip>( aoc.animationClips );
66
67 foreach( AnimationClip animationClip in animationList )
68 {
69 // Find animation in dictionary
70 SO_AnimationType so_AnimationType;
71
72 bool hasFoundAnimation = m_AnimationTypeDictionaryByAnimation.TryGetValue( animationClip, out so_AnimationType );
73
74 if( hasFoundAnimation )
75 {
76 string key = characterAttribute.characterPart.ToString() + characterAttribute.partVariantColor.ToString() + characterAttribute.partVariantType.ToString() + so_AnimationType.m_AnimationName.ToString();
77
78 SO_AnimationType swapSO_AnimationType;
79
80 bool hasFoundSwapAnimation = m_AnimationTypeDictionaryByCompositeAttributeKey.TryGetValue( key, out swapSO_AnimationType );
81
82 if( hasFoundAnimation )
83 {
84 AnimationClip swapAnimationClip = swapSO_AnimationType.m_AnimationClip;
85
86 animsKeyValuePairList.Add( new KeyValuePair<AnimationClip, AnimationClip>( animationClip, swapAnimationClip ) );
87 }
88 }
89 }
90
91 // Apply animation updates to animation override controller and then update animator with the new controller
92 aoc.ApplyOverrides( animsKeyValuePairList );
93 currentAnimator.runtimeAnimatorController = aoc;
94 }
95 }
96}
Dictionary< string, SO_AnimationType > m_AnimationTypeDictionaryByCompositeAttributeKey
Dictionary< AnimationClip, SO_AnimationType > m_AnimationTypeDictionaryByAnimation
void Start()
Initialize the lists
SO_AnimationType[] m_SOAnimationTypeArray
void ApplyCharacterCustomisationParameters(List< CharacterAttribute > characterAttributesList)
Handles the animation overrides
Create a scriptable animation type. This objects holds infomration about animation variants.
AnimationName m_AnimationName
CharacterPartAnimator m_CharacterPart
PartVariantColor m_PartVariantColor
PartVariantType m_PartVariantType
AnimationClip m_AnimationClip
Define what character variant we want to switch into.
PartVariantType partVariantType
PartVariantColor partVariantColor
CharacterPartAnimator characterPart