1
\$\begingroup\$

I am trying to learn how to use ScriptableObjects. I have an ObjectField:

Foo myFoo = null;
myFoo = EditorGUILayout.ObjectField("My Foo", myFoo, typeof(Foo), true) as Foo;

I have a Foo in my Scene and in my Assets folder. In the editor, I can set myFoo to be the Foo in my Scene, but not my Assets folder. How can I get the prefab from my Assets folder?

(I have tried setting the allow scene objects to false, but I still can't find myFoo in the Assets folder.)

It allows me to drag and drop myFoo in from my Assets folder, but it doesn't stay there. If I use the Scene one, it does. But when I search for it in Assets, it is not there.

\$\endgroup\$
1
\$\begingroup\$

The "true" parameter that you have set is meant for allowSceneObjects, so you will be able to drag a scene object into that field, however I am unsure if passing your own type will always work.

From what I've seen it is only able to use type that Unity recognize, Transform, GameObject, TextAsset (which could be .txt, .cs, .ja), etc.

Also, your object in the Scene doesn't necessary share the same type of the one in your asset folder.

For instance, your could specify a MonoBehaviour attatched to a GameObject

MonoA myMonoA = null;
myMonoA = EditorGUILayout.ObjectField("My Mono A", myMonoA, typeof(MonoA), true) as MonoA;

and be able to drag it from the scene with a GameObject on which it is attached to.

However, if you drag and drop the same "MonoBehaviour" from your Asset folder MonoA.cs well, your out of luck, because in that case it is a TextAsset. So as you can see, the type don't match.

Edit 1

For a prefab though, I was able to drag and drop using the GameObject or the Object. I guess you could want to drap and drop a prefab as an Object and later do GameObject.Instantiate(thatObject);

But even there, I'm not sure how you would detect the "real" type of the object dragged in with Unity. With Object I was able to drag a folder from the Project view, but his type seems to be DefaultAsset, so at that point it might be hard to know if what you've got is a folder or a prefab.

Edit 2

Finding more way to do this, since I currently need something similar, there is the AssetDatabase.GetAssetPath(myFoo) function which returns you the full path of the Asset. I doubled checked, and it does returns the extension as well.

So if the object pass is a prefab, it would return /The/Full/Path/myFoo.prefab, if it's a folder it would return /The/Full/Path/myFoo or a script /The/Full/Path/myFoo.cs.

You could write your own file detection here to handle what you want, and set back myFoo object to null with a user message if that type isn't supported by your scriptable object.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.