CreateAssetMenu Attribute: multiple sub-menu ?
Just learning how to use scriptableObjects. At this point I have an entry in the Create menu that lets me create new assets of this type :
[CreateAssetMenu(fileName = "New Carousel", menuName = "Sprite Carousel/Empty")]
public class Carousel : ScriptableObject{
/*somecode*/
}
As you can maybe tell, I am willing to create multiple sub-entries in the "Sprite Carousel" menu, which would each create a different pre-configurated asset.
My problem is: I don't know how to create multiple sub-entries (using the CreateAssetMenu attribute multiple times throws an error), and I don't know how I would make each entry generate a different preconfigure the asset in a specific way.
Answer by Estecka · May 07, 2017 at 03:48 PM
Turns out this attribute is not the tool you want to use to create advanced sub-menus. This is just a quick-hand to get a quick integration.
What you'll want to do is create an editor class that'll contain a method for each sub-entry, and give them the MenuItem
attribute.
using UnityEngine;
using UnityEditor;
static class CarouselContextMenu{
[MenuItem("Assets/Create/Sprite Carousel/8-way")]
static void eightway(){
var asset = ScriptableObject.CreateInstance<Carousel> ();
/*code to preconfigure your asset*/
var path = AssetDatabase.GetAssetPath (Selection.activeObject);
path +="/My New Carousel.asset";
ProjectWindowUtil.CreateAsset (asset, path);
}
}
ScriptableObject.CreateInstance<T>()
must be used instead of new T()
to instantiate a sciptable object.
AssetDatabase.GetAssetPath (Selection.activeObject)
returns the path where the context menu was invoked.
ProjectWindowUtil.CreateAsset(asset, path)
will effectively spawn the asset in the project folders. You don't want to use AssetDataBase.CreateAsset()
as this one will overwrite files and folder without prompting...
Your answer
![](https://koobas.hobune.stream/wayback/20220612121436im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Does loading a ScriptableObject on runtime and modifying it affect the ScriptableObject in file? 1 Answer
Scripting Movement Fails for Instantiated Prefab 2 Answers
ScriptableObject not saving to Asset properly 0 Answers
Attaching method to scriptableobject 1 Answer
Can I instantiate an already created ScriptableObject? 0 Answers