Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Single instance of Scriptable Object without adding a menu entry

Discussion in 'Scripting' started by ledbetterMRI, Apr 22, 2020.

  1. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    25
    I have a class that inherits from scriptable object that has a few animation coroutines useful for fading UI components. This class is accessed via a singleton instance, allowing any UI script to easily start these coroutines for their UI components.

    I made this a scriptable object because I want to be able to inspect and serialize a few variables in the editor. Namely fade speed and the alpha value of of UI when faded out.

    Here's my predicament: as far as I can tell, the only way to create an inspect-able scriptable object is to create an instance of it by adding a right-click menu option to the editor. I don't want to clutter the right click menu in this case; I only need one instance of this scriptable object. In fact, the ability to add more than one would complicate/break the whole singleton setup I would suspect.

    Is there any way to get the Unity editor "see" the declared scriptable object .cs file as an asset instance as well? As I ask this, I realize how ridiculous that probably is. But I can't think of another way to do this and, at least on the surface, this seems like a proper use case for a scriptable object.

    Otherwise, is it possible to hand-create a single instance of this SO? I don't want designers creating more than one of these by mistake.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Just add the CreateAssetMenu attribute, make the thing, then comment that line out. I do it all the time.
     
    ledbetterMRI likes this.
  3. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    25
    Lol, I dunno why that didn't dawn on me. Definitely a practical workaround. Thanks Kurt.
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Another way is you can supply an optional argument to that attribute to make it bury itself in a folder, or discourage others from making extras:

    Code (csharp):
    1. [CreateAssetMenu( "OnlyOneOfEach/MyThingamabob")]
    Does not prohibit it from doing it, but it moves it out of sight in the right-click popup in any case.
     
    PraetorBlue and ledbetterMRI like this.
  5. ledbetterMRI

    ledbetterMRI

    Joined:
    May 29, 2019
    Posts:
    25
    This got me thinking that it probably wouldn't be too hard to write an editor script checking for asset instances in the project hierarchy. Not sure its necessary in my case, but would be useful if having multiple instances would be detrimental.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Automated checking like that, as long as it displays a clear meaningful message, can be very helpful years in the future when you violate an assumption.
     
  7. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,008
    Create and keep.
    Code (csharp):
    1.  
    2.  
    3. [CreateAssetMenu(fileName = nameof(DummyScriptableObject), menuName = "ScriptableObjects/"+nameof(DummyScriptableObject))]
    4. class DummyScriptableObject:ScriptableObject{
    5. }
    6.  
    7.  
    After creating the DummyScriptableObject through menu, select the asset, right click on inspector Tab select debug and swap the DummyScriptableObject script with the script of your choosing.

    And don't forget to change the asset name ;)
     
    Last edited: Apr 14, 2023