RuntimeAnimatorController
A representation of the Animator Controller, optimized for runtime.
Read time 5 minutesLast updated 3 days ago
Definition
- Type: Class
- Namespace: UnityEngine
- Assembly: UnityEngine.AnimationModule
- Inherits from: Object
[ExcludeFromObjectFactory]public class RuntimeAnimatorController : Object
Remarks
At runtime, Unity replaces the AnimatorController class with this optimized runtime class. Access to Editor functions, such as modifying the structure of an Animator Controller, are restricted.
This optimized class provides the following different ways to access and modify an Animator Controller at runtime:
-
Store the reference of an Animator Controller so you can replace the Animator Controller of an Animator. This is useful for modifiying the structure of an Animator Controller at runtime. Use _runtimeAnimatorController to access the controller to be replaced.
-
Create an AnimatorOverrideController that you can use to override the Animation Clips associated with an Animator Controller. This is more efficient than replacing a controller because only the clips are updated. The Animator Override Controller is based on the Runtime Animator Controller that initializes it. .
The following example demonstrates how to spawn GameObjects at runtime. Each GameObject is animated with different Animator Controllers.
Additional Resources: _runtimeAnimatorController
Examples
// This script spawns random zombie prefabs at runtime. The Animator component for// each zombie is assigned a different Animator Controller based on the zombie type.using UnityEngine;public class RandomZombieSpawner : MonoBehaviour{ // The prefab of the zombie Game Object containing an Animator component. public GameObject zombiePrefab; // Store the references to the Animator Controllers for the different zombie types that can be spawned public RuntimeAnimatorController standardZombieAnimator; public RuntimeAnimatorController bigZombieAnimator; public RuntimeAnimatorController smallZombieAnimator; void Start() { for (var i = 0; i < 10; i++) { SpawnZombie(); } } public void SpawnZombie() { // Instantiate a new zombie Game Object at a random position GameObject zombie = Instantiate(zombiePrefab, new Vector3(Random.Range(0f, 10f), 0, Random.Range(0f, 10f)), Quaternion.identity); Animator animator = zombie.GetComponent<Animator>(); // Randomly determine the type of zombie and assign its Animator with its corresponding Animator Controller. var randomValue = Random.Range(0f, 1f); if (randomValue <= 0.3f) { animator.runtimeAnimatorController = bigZombieAnimator; zombie.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f); } else if (randomValue <= 0.6f) { animator.runtimeAnimatorController = smallZombieAnimator; zombie.transform.localScale = new Vector3(0.75f, 0.75f, 0.75f); } else { animator.runtimeAnimatorController = standardZombieAnimator; } }}
Properties
Property | Description |
|---|---|
| animationClips | Retrieves all AnimationClip used by the controller. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |