RockyHong / UnityGetComponentCache

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

English | 繁體中文

Unity Get Component Cache Overview

Unity Get Component Cache is designed to solve and optimize the efficient access to Unity Components.

Problems Addressed:

  • Frequent use of 'GetComponent' can significantly reduce performance.
void Update()
{
    var rigidbody = GetComponent<Rigidbody>();
    // Performing some operations with rigidbody...
}
  • Using 'GetComponent' in 'Awake' or 'Start' methods for one-time initialization of all component fields might lead to lengthy and confusing code. It's also easy to forget to initialize new fields.
Animator animator;
Rigidbody rigidbody;
Collider collider;
AudioSource audioSource;
// Many other fields...

void Awake()
{
   animator = GetComponent<Animator>();
   rigidbody = GetComponent<Rigidbody>();
   collider = GetComponent<Collider>();
   audioSource = GetComponent<AudioSource>();
    // More GetComponent calls...
}
  • Implementing lazy-loading with properties might lead to verbose and redundant code.
Animator _animator;
Animator animator
{
    get
    {
        if (_animator == null)
        {
            _animator = GetComponent<Animator>();
        }
        return _animator;
    }
}

RigidBody _rigidbody;
RigidBody rigidbody
{
    get
    {
        if (_rigidbody == null)
        {
            _rigidbody = GetComponent<RigidBody>();
        }
        return _rigidbody;
    }
}

Using Unity Get Component Cache:

Installation

In your Unity project, open 'Window -> Package Manager', and add a Package from the Git URL:

https://github.com/RockyHong/UnityGetComponentCache.git

Usage 1. One-Time Caching:

Use the [GetComponentCache] attribute to mark fields for caching. Initialize these caches in a one-time method like 'Awake'. This ensures no initialization is missed even when new fields are added.

using UnityGetComponentCache;

public class ExampleBehaviour : MonoBehaviour
{
    [GetComponentCache]
    Animator _animator;

    [GetComponentCache]
    Rigidbody _rigidbody;

    void Awake()
    {
        // Initialize cached components at once.
        GetComponentCacheInitializer.InitializeCaches(this);
    }
}

Usage 2. Editor Pre-Configuration:

  • Mark public fields or serializable private fields with [GetComponentCache].
  • Use the 'GetComponent' button in the Inspector to pre-fill values, simplifying pre-run setup.
using UnityGetComponentCache;

// Use [GetComponentCache] for public or [SerializeField] private fields.
public class ExampleBehaviour : MonoBehaviour
{
    [GetComponentCache]
    public Animator animator; // Public field.

    [GetComponentCache, SerializeField]
    private Rigidbody _rigidbody; // Private field.
}

imageimage

  • 'Get' button status:
    • Normal: field is non-null.
    • Red: field is null.

Usage 3. Runtime Lazy Caching:

Utilize GetComponentCache for efficient component retrieval. This extension method streamlines component access by caching them on first use. Ensure to prefix the method with this as it's an extension method.

using UnityGetComponentCache;

public class ExampleBehaviour : MonoBehaviour
{
    Animator _animator => this.GetComponentCache<Animator>();
    Rigidbody _rigidbody => this.GetComponentCache<Rigidbody>();
}

or

using UnityGetComponentCache;

public class ExampleBehaviour : Monobehaviour
{
    void Foo()
    {
        var animator = this.GetComponentCache<Animator>();
        var rigidbody = this.GetComponentCache<Rigidbody>();
        // Perform operations with cached values...
    }
}

Additional Note

Using [RequireComponent] Attribute:

Consider combining with [RequireComponent] to ensure essential components are always included in the GameObject, enhancing the caching mechanism's effectiveness.

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(Animator))]
public class ExampleBehaviour : MonoBehaviour
{
    [GetComponentCache]
    Animator _animator;

    [GetComponentCache]
    Rigidbody _rigidbody;
}

About

License:MIT License


Languages

Language:C# 100.0%