- Home /
Question by
Crozog · Nov 16, 2019 at 03:54 PM ·
unityeditorscriptableobjectdots
EntityArchetype and ScriptableObject
Hello,
I try to make an UnityEditor for a scriptable object that will be use to create an EntityArchetype. The goal is to define a list of ComponentType directly in the inspector.
I managed to get array to all ComponentType that use the interface IComponentData. With that I create a list of buttons to add the selected ComponentType to the list that will be use to create the EntityArchetype.
But I have a problem, when a try to add the selected type to a list I got the famous NullReferenceException here: componentTypes.Add(type);
Do you guys have any tips to solve this?
using Unity.Entities;
using UnityEngine;
[CreateAssetMenu(fileName = "New Entity Setting", menuName = "EntitySetting", order = 1)]
public class EntitySetting : ScriptableObject
{
public ComponentType[] types ;
}
using System;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(EntitySetting))]
public class EntitySettingEditor : Editor
{
public Type[] typesAvailable;
EntitySetting entitySetting;
public void OnEnable()
{
entitySetting = (EntitySetting) target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (entitySetting.types != null)
{
for (int i = 0; i < entitySetting.types.Length; i++)
{
GUILayout.TextArea(entitySetting.types[i].ToString());
}
}
typesAvailable = AppDomain.CurrentDomain.GetAllInterfaceOfType(typeof(IComponentData));
foreach (var type in typesAvailable)
{
if (GUILayout.Button(type.ToString()))
{
AddThisType(type);
}
}
}
public void AddThisType(Type type)
{
if (entitySetting.types == null)
{
entitySetting.types = new ComponentType[0];
}
List<ComponentType> componentTypes = new List<ComponentType>();
for (int i = 0; i < entitySetting.types.Length; i++)
{
componentTypes.Add(entitySetting.types[i]);
}
componentTypes.Add(type);
entitySetting.types = componentTypes.ToArray();
}
}
Comment