Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by ronronmx · Aug 07, 2012 at 08:06 AM · editorobjecteditorwindow

Limiting EditorGUI.ObjectField() to Prefabs only?

Hey all, I'm making a EditorWindow with a few ObjectFields to let the user assign different GameObjects to each field. Some fields only allow you to assign non-scene objects, while the others can use any GameObject...that's the problem though, I want to restrict those GameObject fields to only Prefabs, that way you can assign a non-Prefab object to them.

I need to restrict those fields to only allow PrefabType.Prefab objects and not PrefabType.ModelPrefab objects. How can I get this to work? Can I do something like 'typeof(PrefabType.Prefab)' (I tried that and it doesn't like it...).

Any help would be greatly appreciated as always :)

Thanks for your time... Stephane

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Kryptos · Aug 07, 2012 at 08:16 AM

Assuming your field is obj. You can check for the type before assigning it to the field.

 GameObject pObj = EditorGUI.ObjectField(new Rect(3,3,position.width - 6, 20),
             "Blablabla",
             obj,
             typeof(GameObject));
 if (pObj != null && PrefabUtility.GetPrefabType(pObj) == PrefabType.Prefab)
 {
     obj = pObj
 }
 else
 {
     obj = null;
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · Aug 07, 2012 at 08:23 AM 0
Share

Awesome, exactly what I needed! Thanks a lot for the quick help Nicolas!

avatar image Kryptos · Aug 07, 2012 at 08:37 AM 0
Share

You're welcome. Don't forget to tick the answer to keep the board clean (and up-vote if you like ;) )

avatar image ronronmx · Aug 07, 2012 at 05:17 PM 0
Share

Sorry about the late vote and accepted answer, I went on and implemented your solution and forgot to come back and accept your answer ;P

If you don't $$anonymous$$d, I have one more question on the same subject: Is it possible to use the Event system to detect the type of Prefab you're dragging onto the ObjectField so that you can block it before even assigning it? With your solution above, I need to create a message box to let the user know that the object which was dragged wasn't the correct type, but if you can't assign it in the first place, then there's no need to set obj = null afterwards. Does that make sense?

avatar image Kryptos · Aug 07, 2012 at 07:47 PM 0
Share

You can only filter by real type (i.e. $$anonymous$$onoBehaviour, GameObject) but the PrefabType is more a flag than a type. So I don't think that there is better solution to it.

You could popup a dialog window (modal) but that will be very intrusive.

avatar image ronronmx · Aug 07, 2012 at 10:35 PM 0
Share

That's what I though, that's too bad...but your solution is the best next choice and it will work perfectly fine :)

Thanks again!

avatar image
3

Answer by bellicapax · Jun 20, 2017 at 09:35 PM

Just chiming in to say you can replace this now with:

 _prefabToSearchFor = EditorGUILayout.ObjectField("Prefab to search for", _prefabToSearchFor, typeof(GameObject), false) as GameObject;

The "false" makes sure that you can't assign a scene object, so it must be a Prefab.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Saving class fields in the Asset 0 Answers

How can i get the center of the entire editor window 0 Answers

ScriptableObject problem 1 Answer

Editor window script only works when window is open? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges