Layers and Cursors using ScriptableObjects

Hi all,

Just thought I’d share a different approach. My motivation is that I was not keen on having to maintain an enum in code that had to match the order of the Layers defined by a designer. That feels to me like a ‘code smell’, although I agree that the issue here is that Unity’s treatment of Layers is a bit naff. :smiley:

I’m using a ScriptableObject derived class for CameraRaycast, which means that my Raycaster and Cursor logic are not linked other than both accessing the same SO asset. I then create a CursorType SO that allows me to define the Texture2D, hotspot and Layer two show the cursor for.

CursorType

I then have a CursorController prefab that ‘observes’ the Raycast SO and holds an array of CursorTypes, which are easy to show based on the Raycast result.

CursorAffordance

This approach allowed me (with my designer’s hat and dodgy Aussie accent) to add a new Layer (NPC), and Cursor (Talk), without changing a single line of code. :smiley: The Utility script has been deleted, and the enum does not exist at all in my project, as I never have to hardcode value of a Layer in code… the code only works with LayerMasks and Layer int’s read from the SO’s.

Just about to start the Observer pattern lectures, so I may end up refining this approach, but so far I am liking the ScriptableObject flexibility.

Cheers,
Wayne

Privacy & Terms