- Home /
Show scriptable objects parameter into an array
Hi guys, I was tring to make a custom inspector for my project. but i encounter a problem in disposition of the fields.
What i have :
What i wanted : I have an array with scriptable objects, each of them have propeties, and i whant to show them just below the array field. Just like that : Sry for the draw it's not verry pretty but i hope you will understand what i've in my mind.
Thanks for help.
Here's the code of the build manager and the editor
Build manager :
public GameObject shopSlot;
public Plant[] plants;
public Transform slotsContent;
public Plant plantToPlant;
private void Start()
{
for (int i = 0; i < plants.Length; i++)
{
GameObject slot = Instantiate(shopSlot, slotsContent.position, Quaternion.identity);
slot.transform.SetParent(slotsContent);
slot.GetComponent<ShopSlot>().Setup(plants[i]);
}
}
and for the editor : public override void OnInspectorGUI() { base.OnInspectorGUI();
Build$$anonymous$$anager build$$anonymous$$anager = (Build$$anonymous$$anager)target;
for (int i = 0; i < build$$anonymous$$anager.plants.Length; i++)
{
CreateEditor(build$$anonymous$$anager.plants[i]).OnInspectorGUI();
}
}
For scriptableObject :
public class Plant : ScriptableObject
{
public string nameLabel;
public int cost;
public Sprite icon;
public GameObject plantPrefab;
}
Answer by xxmariofer · Aug 02, 2020 at 08:29 PM
create an struct with the parameters and serialize that struct in the inspector and reference the struct from code
first you create your scriptable object classs
[Serializable]
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/SpawnManagerScriptableObject", order = 1)]
public class MyPlantScriptable : ScriptableObject
{
//fill your scriptableobject
}
then your struct with the data and that scriptable object
[System.Serializable]
public struct Plant
{
public MyPlantScriptable plant;
public string name;
public int number;
}
the final result
How can i do that i've never used struct.
public struct _plant
{
public string _nameLabel;
public Sprite _icon;
}
Somethig like that i suppose but how can i reference it ?
Added my code in comment of the question.
add the labble in the top
[System.Serializable]
public struct _plant
{
public string _nameLabel;
public Sprite _icon;
}
and add a public _plant variable in your inspector manager
And i should use the struct instead the scriptable object ?
Yup, it's done but i don't have the result i wanted.
is there a way to have each struc below the fiels in the array (it's not very important but better for lisibility) ?
no, not like that, ill post it as an answer check it out, ill edit the first answer
Thank you so much fro help. i'm a bit annoying but is there a way to show the data stored in the scriptable object attached to the struct just below ?
if not don't care, you helped me a lot :)
Answer by Mrintoxx · Aug 03, 2020 at 03:07 PM
Following the awnser of @xxmariofer i've made a struct who contains the scriptable object and then created a custom editor to show the properties of the scriptable object inside the array. But i can't access to the properties inside the scriptable object.
my editor script :
public override void OnInspectorGUI()
{
// base.OnInspectorGUI();
serializedObject.Update();
BuildManager buildManager = (BuildManager)target;
var shopSlotP = serializedObject.FindProperty("shopSlot");
shopSlotP.objectReferenceValue = EditorGUILayout.ObjectField("Shop slot prefab", shopSlotP.objectReferenceValue, typeof(object), true);
var slotContent = serializedObject.FindProperty("slotsContent");
slotContent.objectReferenceValue = EditorGUILayout.ObjectField("Shop slot content", slotContent.objectReferenceValue, typeof(object), true);
var plantsData = serializedObject.FindProperty("plantsData");
EditorGUILayout.PropertyField(plantsData, true);
for (int i = 0; i < plantsData.arraySize; i++)
{
var cost = serializedObject.FindProperty("plantsData").GetArrayElementAtIndex(i).FindPropertyRelative("cost");
EditorGUILayout.PropertyField(cost);
}
serializedObject.ApplyModifiedProperties();
//base.DrawDefaultInspector();
}