Lesson 6.1 - Project Optimization (Adapted for Unity EdLab)

Tutorial

·

Beginner

·

+10XP

·

30 mins

·

Unity Technologies

Lesson 6.1 - Project Optimization (Adapted for Unity EdLab)


Overview:

In this lesson, you will learn about a variety of different techniques to optimize your projects and make them more performant. You may not notice a huge difference in these small prototype projects, but when you’re exporting a larger project, especially one for mobile or web, every bit of performance improvement is critical.

Project Outcome:

Several of your prototype projects will have improved optimization, serving as examples for you to implement in your personal projects

Languages available:

1. Variable attributes

In the course, we only ever used “public” or “private” variables, but there are a lot of other variable attributes you should be familiar with.



  1. Open your Prototype 1 project and open the PlayerController.cs script

  1. Replace the keyword “private” with [SerializeField], then edit the values in the inspector

  1. In FollowPlayer.cs, add the [SerializeField] attribute to the Vector3 offset variable

  1. Try applying the “readonly”, “const”, or “static” attributes, noticing that all have the effect of removing the variable from the inspector


2. Unity Event Functions

In the course we only ever used the default Update() and Start() event functions, but there are others you might want to use in different circumstances.



  1. Duplicate your main Camera, rename it “Secondary Camera”, then deactivate the Main Camera

  1. Reposition the Secondary camera in a first-person view, then edit the offset variable to match that position

  1. Run your project and notice how choppy it is

  1. In PlayerController.cs, change “Update” to “FixedUpdate”, and in FollowPlayer.cs, change “Update” to “LateUpdate”, then test again

  1. Delete the Start() function in both scripts, then reactivate your Main Camera


3. Object Pooling

Throughout the course, we’ve created a lot of prototypes that instantiated and destroyed objects during gameplay, but there’s actually a more performant / efficient way to do that called Object Pooling.



  1. Open Prototype 2 and create a backup

  1. Delete the DetectCollisions, PlayerController, and DestroyOutOfBounds scripts from your project

  1. In your File Explorer, select the drive named “Unity Projects and Assets”. Navigate to CreateWithCode > Unit 6 - Next Steps > _Lesson 6.1 - Project Optimization > Object Pooling. Double-click the “Challenge-5_Start” package to import.

  1. Reattach the PlayerController script to your player. Reattach DestroyOutOfBounds script to your animal and food prefabs, and then reattach the DetectCollisions script to your animal prefabs (not to your food prefab)

  1. Attach the ObjectPooler script to your Spawn Manager, drag your food projectile into the “Objects To Pool” variable, and set the “Amount To Pool” to 20

  1. Run your project and see how the projectiles are activated and deactivated

4. Lesson Recap


New Concepts and Skills


  • Optimization

  • Serialized Fields

  • readonly / const / static / protected

  • Event Functions

  • FixedUpdate() vs. Update() vs. LateUpdate()

  • Awake() vs. Start()

  • Object Pooling

Complete this tutorial