- Home /
Custom object field of List ?
Hey guys !
Trying to do my own custom editor.. I need to expose / access in the UI a list of float (but note that it could be a list of anything) Unity already supports that natively so you can add/remove/edit template list directly in the editor.. But the question is: Can you do that in your custom editor ??
So basicaly I've tried something like that :
List<float> Params;
Params = (List<float>) EditorGUILayout.ObjectField( "Parameters", (UnityEngine.Object)Params, typeof(List<float>) );
Unfortunately I have a "cannot convert type from List to UnityEngine.Object" compiler error so it looks this is not the correct way of doing that..
I'm pretty sure it's possible.. any help / hint / code / thing would be greatly appreciated ;-)
cheers
Answer by ckfinite · Jun 07, 2011 at 03:51 AM
Basically, use another data type for your object than a List. What is is saying is that there is no way of converting from a System.Collections.Generic.List to a UntiyEngine.Object, which is true.
What I would do is write a wrapper class for List that extends UnityEngine.Object, like this:
public class ListWrapper<T> : UnityEngine.Object {
public List<T> objects = new List<T>();
}
Then you should be able to use it like this:
ListWrapper<float> Params;
Params = (ListWrapper<float>) EditorGUILayout.ObjectField( "Parameters", (UnityEngine.Object)Params, typeof(ListWrapper<float>) );
Unity might not like the generics, so you might want to replace them if this does not work.
hum.. thanks for that but it doesn't seem to work.. in my custom editor I don't have the float list UI we have when using the default inspector.. it just says None then the type of the object (ListWrapper'1[System.Single]) looks like I'm supposed to select something.. I can't create the list of float right in place.. I'm not sure to be very clear here.. I will try to upload an image if this is possible but basically I have the UI you have when you try to set a GameObject for ex (small icon to use a gameobject in the project..)
Oh, okay. That code assumes that a ListWrapper object has to be dragged in, and since there is no native editor for somewhat obvious reasons, you don't get one. Your best chance would be (in my opinion) be to write a custom editor for ListWrapper. That array view only appears in the default inspector, and hardly anywhere else, so you will have to recreate it.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
ALWAYS SHOW DROPDOWN LIST 0 Answers
How to preset a filter on ObjectField's GameObject Selection box 0 Answers
Showing the editors of list elements 0 Answers
Error with GUI.ObjectFeild() 0 Answers