Search Unity

Is a Mouse Manager as a ScriptableObject viable?

Discussion in 'Scripting' started by elitegoliath13, Aug 3, 2020.

  1. elitegoliath13

    elitegoliath13

    Joined:
    Dec 3, 2017
    Posts:
    9
    I am trying to wrap my head around ScriptableObjects and their possibilities. My goal is to have a Mouse Manager that keeps track of the mouse position, objects it's hovering over, and other behaviors. I want this manager to be read by anything that wants to know whenever it wants to know it.

    I know I could make a MonoBehavior script that has ScriptableObject Variables (like a Vector3) that it keeps updated, but that requires me to add the manager to each scene manually. And then I would need to include each variable as a reference into any other asset that needs to read it.

    Is there a better way to do this? Have the manager as a ScriptableObject, each attribute also a ScriptableObject, and just reference the manager in whatever script needs access to it?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    I can see why you're attracted to ScriptableObject for a MouseManager. The problem with a MouseManager as a ScriptableObject though, is that ScriptableObjects don't receive Unity callbacks such as Start() and Update(). So in order to update the mouse manager with new mouse data, you would need a MonoBehaviour somewhere that is reading the mouse position in Update anyway.

    There are some ways to improve upon this. You can use a singleton pattern to make accessing the MouseManager from other scripts easier(Google for "Unity Singleton pattern" and you will find hundreds of examples).

    You can also call DontDestroyOnLoad on the MouseManager's Start method. Then you would only need to include it in your first scene and it would persist between scene loads.
     
    elitegoliath13 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Think of ScriptableObjects as "predefined bags of data" for your game. Mouse input is dynamic. Perhaps you could have ScriptableObjects that configure the sensitivity of your mouse, but it would be a bit misleading to put dynamic information into a ScriptableObject at runtime.
     
    elitegoliath13 likes this.
  4. elitegoliath13

    elitegoliath13

    Joined:
    Dec 3, 2017
    Posts:
    9
    Thanks guys, that helped!