Skip to content

Instantly share code, notes, and snippets.

@rodrigoueda
Last active October 19, 2020 20:50
Show Gist options
  • Save rodrigoueda/c6a714d7cbbdc58641b89679e06d5efb to your computer and use it in GitHub Desktop.
Save rodrigoueda/c6a714d7cbbdc58641b89679e06d5efb to your computer and use it in GitHub Desktop.

Unity Object Pool

Usage

  • Use the namespace ObjectPool
  • Your new prefab reference needs to be of type PoolableGameObject instead of GameObject
  • On your Start method, call the Prewarm()function
using UnityEngine;
using ObjectPool;

public class TestObjectPool : MonoBehaviour
{
    public PoolableGameObject cubes;

    void Start()
    {
        cubes.Prewarm();
    }
}
  • On unity editor, there is additional information to edit:

PoolableGameObject data

  • Prefab: The GameObject to be pooled
  • AmountToPool: How many GameObjects should be instatiated
  • ShouldExpand: Should expand the pool when every instantiated objects are already in use?
  • Disposable: Should dispose when the pool is soft flushed?

Retrieving

  • Instead of using unity Object.Instantiate static method, switch to ObjectPoolManager.Instance.Retrieve(PooledGameObject)
  • PooledGameObject.instance is the now the way to access the created GameObject
void Update()
{
    if (Input.GetKeyUp(KeyCode.Space)) {
        PooledGameObject gameObj = ObjectPoolManager.Instance.Retrieve(cubes);

        gameObj.instance.transform.parent = this.transform;
        gameObj.instance.transform.position = Vector3.zero;
        gameObj.instance.SetActive(true);
    }
}
  • There is a second optional parameter to the Retrieve function which creates the objects with a delay self recycle
  • This parameter represents the time in seconds
void Update()
{
    if (Input.GetKeyUp(KeyCode.Space)) {
        PooledGameObject gameObj = ObjectPoolManager.Instance.Retrieve(cubes, 2f);

        gameObj.instance.transform.parent = this.transform;
        gameObj.instance.transform.position = Vector3.zero;
        gameObj.instance.SetActive(true);
    }
}

Recycling

  • Switch Object.Destroy to ObjectPoolManager.Instance.Recycle(PooledGameObject)
void Update()
{
    if (Input.GetKeyUp(KeyCode.Escape)) {
        ObjectPoolManager.Instance.Recycle(gameObj);
    }
}
  • Also, it`s possible to delay the recycle using a second parameter, the value is in seconds
void Update()
{
    if (Input.GetKeyUp(KeyCode.Escape)) {
        ObjectPoolManager.Instance.Recycle(gameObj, 1f);
    }
}

Flushing the pool

  • Use the method ObjectPoolManager.Instance.DisposePool(); to empty the pool
  • Remember that only the objects marked as Disposable will be destroyed
  • To hard flush the pool, removing every single object, use a boolean parameter ObjectPoolManager.Instance.DisposePool(true);

IPoolable interface

  • Optionally, the prefab script can implement IPoolable interface
  • This way, every time it`s instance is retrieved or recycled, the methods Retrieve() and ``` Recycle() ``` will be fired respectively.
using UnityEngine;
using ObjectPool;

public class CubeBehaviour : MonoBehaviour, IPoolable
{
    public void Retrieve()
    {
        print("Object Retrieved");
    }

    public void Recycle()
    {
        print("Object Recycled");
    }
}

Managed Update

  • When working with a great amount of objects, it`s considered a good practice to center each individual Update() into a single behaviour in order to optimize performance
  • ObjectPoolManager is also able to concentrate instanced objects updates, just extend IManagedUpdate interface
  • It`s possible to combine IPoolable and IManagedUpdate interfaces
using UnityEngine;
using ObjectPool;

public class CubeBehaviour : MonoBehaviour, IPoolable, IManagedUpdate
{
    public void Retrieve()
    {
        ObjectPoolManager.Instance.RegisterManagedUpdate(this);
    }

    public void Recycle()
    {
        ObjectPoolManager.Instance.UnregisterManagedUpdate(this);
    }

    public void ManagedUpdate(float deltaTime)
    {
        print(deltaTime);
    }

}

Unity Development Animator

Installation

https://docs.unity3d.com/Packages/com.unity.package-manager-ui@2.1/manual/index.html

{
    "dependencies": {
        "com.rodrigoueda.developmentanimator": "https://github.com/rodrigoueda/com.rodrigoueda.developmentanimator.git#0.1.0"
    }
}

Usage

This package is intended to work in order to reduce the amount of clips from a GameObject, specially in cases when is necessary to scroll through the list of animations.

Long animation list

Open the package window using the menu:

Window -> Development Animator

Select in your hierarchy a GameObject with the Animator you wish to work. The first time that you are working on your animator, you need to press the Create a Development Animator button so the package can generate a new development animator.

After that you will be able to see a list with all the current animations associated with your animator and choose wich one you want to copy to the dev animator pressing the + button.

Original Controller

There is also buttons to locate each animation in your Project window.

When pressing the Switch Controllers button you will have access to the reduced dev animator animation list.

Development Animator

At the Animation window it's now possible to select between the choosed clip list.

Reduced List

Caveats

Since the created development animator is managed by the package, manually editing it can create some bugs with the list window, specially when creating Transitions and Layers.

  • It's recommended to switch to the original animator in order to test your animations.

  • It's also recommended to create new animations on the original animator and then copy it to the development animator instead of the other way around although the package is able to indentify such cases.

Favorites List

Credits

This package is a fork from the original BrokenVector/favorites-list repository.

The original plugin is also available in the Unity Assetstore.

Review video

Review Video

You can check out the review to the original plugin here.

The two main changes from this version to the original are:

Installation

https://docs.unity3d.com/Packages/com.unity.package-manager-ui@2.1/manual/index.html

{
    "dependencies": {
        "com.rodrigoueda.unitybasecode": "https://github.com/rodrigoueda/com.rodrigoueda.favoriteslist.git#1.0.0"
    }
}

Usage

To open the window, just navigate to Tools -> Favorites List.

The first time you open this window, press the button Create a Favorites List to create a new profile.

Create a Favorites List

After that, you can create additional profile pressing the "+" button. To alternate betweeen profiles, use the dropdown.

Create a Favorites List

Screenshot

Just drop your favorite objects onto the list. If you quickly want to access them, click on its button in the list and the object will be selected (and pinged).

The following objects are supported:

  • Folders
  • All assets / objects in the project view (like prefabs)
  • GameObjects So basically everything which is a UnityEngine.Object.

The search works exactly like the Unity search (type search supported). E.g. "t:Camera" gives you a list with GameObjects containing the Camera component.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment