- Home /
ScriptableObject Drop-Down Menu with added variables?
I'm looking to create a Scriptable Object for an item database, that way I can store the information my items require. (such as name, info etc.)
I've declared an Enum in my item script that allows me to declare what this item is used for, (combat, resource, Consumable).
My issue is I do not want items to have access to certain variables unless they are of the type required by it. Such as a Loaf of bread shouldn't have access to AttackPower, if it is a consumable.
I originally had all my item requirements listed, but there are some I would only want to be available if the dependency is selected for a cleaner editor look mostly. Note that the protected void Item Dependency was me fooling around with trying to get it to work with passing in variables into the If Statement. As you can tell from me being here it didn't work.
Here is my code.
using UnityEngine;
[CreateAssetMenu(fileName = "Item", menuName = "Items/Create New Item")]
public class Items : ScriptableObject
{
//Basic Needed Info
new public string name;
//Special Attributes Combat
//Image
public Sprite icon = null;
//Item Characteristic
public enum ItemCharacteristic
{
Consumable,
Combat,
Resource,
};
public ItemCharacteristic itemCharacteristic;
protected virtual void ItemDependency()
{
if (itemCharacteristic == ItemCharacteristic.Combat)
{
}
}
}
Answer by melsy · Mar 29, 2018 at 04:01 AM
It's been a long day so I'm kinda slow in what you are asking. From what I can gather, You want a SO that only displays the variables for the enum object that you are selecting. Custom editor is the way to go there. I wrote a very basic editor to do a similar thing for my weapon script so if the gun has a launcher it exposes the launcher variables. Here it is. using UnityEngine; using UnityEditor;
[CustomEditor(typeof(WeaponScript)), CanEditMultipleObjects]
public class WeaponScriptCleaner : Editor
{
public override void OnInspectorGUI()
{
var weaponsScript = target as WeaponScript;
weaponsScript.withLauncher = GUILayout.Toggle(weaponsScript.withLauncher, "Does it have a launcher");
if (weaponsScript.withLauncher)
{
weaponsScript.projectileSpeed = EditorGUILayout.FloatField("Projectile Speed", weaponsScript.projectileSpeed);
weaponsScript.projectileGravity = EditorGUILayout.FloatField("Projectile Gravity", weaponsScript.projectileGravity);
weaponsScript.projectiles = EditorGUILayout.IntField("Projectiles", weaponsScript.projectiles);
}
base.OnInspectorGUI();
}
}
This creates a bool that when checked, exposes the float and int fields above names the "" and using the var field afterwards. Not to complicated but not using enums. I have not dived very deep in custom inspectors so there may be a better way.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155603im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
DropDownList with string array in Editor Inspector 5 Answers
How can i change the camera view when clicking a button ? 1 Answer
Sorting a list based of a variable 1 Answer