So the blog is supposed to be a mixture of Game and Biz dev, so far its been pretty heavy on the biz but today I’m going to show you how to make some mobile friendly chaser lights in unity.

Chaser Lights

Rather than run you through all the code I’m going to talk through some of the design ideas and just upload all the code with comments so you can work through it in your own pace.

At its core the system subsists of 3 scripts.  The first ChaserLight.cs is just used to help find and create systems.  The public int allows you to set the system id that a series of lights will belong to.

ChaserLightWindow.cs is to keep the generation of the system outside the start function.  Depending on the size and number of systems there can be some overhead.  This creates a Chaser Lights entry in the window menu where you can find the lights for each system.

ChaserLightSystem.cs is where the core functionality lies.  This script is designed to be a standalone system that could be easily incorporated into any project.  Its efficient, in that it uses mobile shaders, has an option for specularity and generates no garbage at runtime.

It can be set on and to randomly change, the key parameters are all clear and available in the inspector.  It then has a a few key public functions;

  • RunLightsTimed which allows you to select a pattern, running time and optional speed,
  • RunRandomPatterns, either timed or ongoing
  • SetSpeed,
  • StopLights.

The key things in these scripts which may be valuable to learn are;

  • Never use foreach loops, they generate garbage and for something like this which is in update there can be major consequences to performance
  • The % shows gives you the remainder of a division which can be very useful for systems like this which need to count, but which would be irritating to reset.
  • SetBaseMaterials shows how to generate materials and textures on the fly.  For a full project, I would likely premake the textures and materials, just to squeeze out every little bit of performance.

Hopefully you can get some value out of the coding practises presented, and you are welcome to use the system in any projects you like, the download link is below the video.  If you do use it, let us know!

 

Download Chaser Light Package

0 Comments to "Tutorial: Chaser Lights in Unity"

Leave a Reply