LyCheSis:ShowCase

LyCheSis:ShowCase

Who am I?

Unity / Unreal Developer and 3D Artist


What I write about


Recent Posts

Unity Optimisations

Here's a list of optimisation tips collected together from various sources for faster access:

Watch the whole videos linked below to get additional explanations.

Optimization tips for maximum performance – Part 1 | Unite Now 2020

https://www.youtube.com/watch?v=ZRDHEqy2uPI

Use arrays or lists if you need to access elements fast by index. Use dictionary if you often need to add or remove elements.


Use object pools instead of instantiating and destroying objects Destroying objects creates a lot of garbage. Create a pooling system and reuse unused objects.


Use Scriptable Objects for storing general object settings instead of using individual variables. If using multiple instances of the objects then the settings are only referenced once, instead of storing the settings with each instance.

public class EnemyConfiguration : ScriptableObject
{
    public float MaxSpeed;
    public float RadiusAttack;
}

public class Enemy : MonoBehaviour
{
    public EnemyConfiguration enemyConfiguration;
}

Getters/Setters cost performance Be aware that this added code safety measure will come at a cost. You can add a preprocessing directive to remove these for the final build.

#if DEVELOPMENT_BUILD
    private int health;
    public int Health { get { return health; } }
#else
    public int Health;
#endif

Note: We still need to take into account that the value will be stored in the Health and not the health variable.


**Be careful with the Resources folder*** Unity will bundle all files in these folders with the final build, regardless if used or not. A large number of files in these folders will slow down the start-up time of the application. Use Addressable Asset System instead.


Remove empty Unity events Unity will automatically scan for these methods in a component and add them to it's call stack. If it is empty this will result in an unnecessary jump into this component. If they are only used for debugging purposes then you can hide them from the final build with preprocessing directive.

#if UNITY_EDITOR
private void Update()
{
    // debugging code
}
#endif

Avoid heavy logic on Start() or Awake() Unity renders the first frame only after all Start and Awake methods of all gameobjects have finished executing. Having a long calculation running there will result in a long delay until something will appear on-screen.


Use property hashes instead of their string names. Unity uses the hashes internally anyway so using strings will result of constant conversion overhead, everytime the string name is used. It also uses less memory and is faster to compare.

int blendId = Animator.StringToHash("Blend");
animator.SetFloat(blendId, 0.5f);

int shininessId = Shader.PropertyToID("_Shininess");
meshRenderer.material.SetFloat(shininessId, 0.5f);

Reduce hierarchy complexity. Unity has to apply all transforms within an object tree to determin its final position/rotation. Try to reduce the amount of transforms and tree depth as much as possible and move objects out of a transformed branch of a tree, if it itself doesn't require to be transformed.


Transforming Rigidbody objects Use Rigidbody methods to transform objects that include the Rigidbody component, because the transform methods are computationally more expensive for these. Also update the Rigidbody component in the FixedUpdate method.

private void FixedUpdate()
{
    rigidBody.MovePosition(Vector3.zero);
}

Avoid using GameObject.AddComponent() at runtime Unity has to perform a lot of checks when using this method. Try to use prefabs instead.


Cache references to components Using GameObject.GetComponent() and especially GameObject.GetComponentsInChildren() is computationally very expensive. If you need to do this, only do it once and cache the reference.