Advertisement
Pro_Unit

ZenjectStateMachineBehaviourAutoInjecter.cs

Dec 25th, 2023
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using ModestTree;
  2. using UnityEngine;
  3.  
  4. namespace Zenject
  5. {
  6.     public class ZenjectStateMachineBehaviourAutoInjecter : MonoBehaviour
  7.     {
  8.         public DiContainer Container { get; private set; }
  9.         Animator _animator;
  10.  
  11.         [Inject]
  12.         public void Construct(DiContainer container)
  13.         {
  14.             this.Container = container;
  15.             _animator = GetComponent<Animator>();
  16.             Assert.IsNotNull(_animator);
  17.         }
  18.  
  19.         // The unity docs (https://unity3d.com/learn/tutorials/modules/beginner/5-pre-order-beta/state-machine-behaviours)
  20.         // mention that StateMachineBehaviour's should only be retrieved in the Start method
  21.         // which is why we do it here
  22.         public void Start()
  23.         {
  24.             // Animator can be null when users create GameObjects directly so in that case
  25.             // Just don't bother attempting to inject the behaviour classes
  26.             if (_animator != null)
  27.             {
  28.                 var behaviours = _animator.GetBehaviours<StateMachineBehaviour>();
  29.  
  30.                 if (behaviours != null)
  31.                 {
  32.                     foreach (var behaviour in behaviours)
  33.                     {
  34.                         Container.Inject(behaviour);
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement