Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Scriptable Object in the Unity Editor

Tags:

c#

unity3d

So apparently i suck at listening at my university, because i can't figure this out, not even with google... How do you create a scriptable object in the editor? I have the project open, it looks like this:

enter image description here

Click the Create button as if you wanted to create a folder or C# script or anything.

enter image description here

Select the ScriptableObject from the popup menu.

enter image description here

Get this panel and finalize the object after selecting the script for it.

The problem is: i don't have the ScriptableObject button. I have a script that is a scriptable object (to make sure i even copied the one from the project of the university). I restarted Unity, i checked if there were any packages installed (there werent) and i googled quite a bit. But i just can't seem to get this working...

Is there anything i have to install or add first? Thanks in advance!

like image 287
Yiasmat Avatar asked May 28 '18 10:05

Yiasmat


People also ask

What are scriptable objects used for Unity?

What are ScriptableObjects? ScriptableObject is a serializable Unity class that allows you to store large quantities of shared data independent from script instances. Using ScriptableObjects makes it easier to manage changes and debugging.


2 Answers

You need another script to add the button which will create an instance from that scriptable object. something like that

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeScriptableObject {
    [MenuItem("Assets/Create/My Scriptable Object")]
    public static void CreateMyAsset()
    {
        MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();

        AssetDatabase.CreateAsset(asset, "Assets/NewScripableObject.asset");
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = asset;
    }
}

You can check this Introduction to Scriptable Objects tutorial on unity website.

like image 56
WaleedYaser Avatar answered Oct 19 '22 10:10

WaleedYaser


I can't comment so just place it like answer: Don't forget to use UnityEditor.AssetDatabase.GenerateUniqueAssetPath coz simple AssetDatabase.CreateAsset can erase your data:

using UnityEngine;
using System.Collections;
using UnityEditor;

public class MakeScriptableObject
{
    [MenuItem("Assets/Create/My Scriptable Object")]
    public static void CreateMyAsset()
    {
        MyScriptableObjectClass asset = ScriptableObject.CreateInstance<MyScriptableObjectClass>();

        string name = UnityEditor.AssetDatabase.GenerateUniqueAssetPath("Assets/NewScripableObject.asset");
        AssetDatabase.CreateAsset(asset, name);
        AssetDatabase.SaveAssets();

        EditorUtility.FocusProjectWindow();

        Selection.activeObject = asset;
    }
}
like image 6
Toshino Kyoko Avatar answered Oct 19 '22 09:10

Toshino Kyoko