Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by vsemenyakin · Jun 02, 2019 at 11:51 AM · editorprefabprefabseditorguiarchitecture

Is there any way to hide script fields outside of prefab that uses it?

Script fields (marked with [SerializeField] attribute) is popular way to setup script dependecies. Other alternative - using FindComponents or FindObject - look less controlable.
.
Main drawbacks of [SerializeField] setup is flooding script GUI inspector with internal prefab dependices. .
For example, I have Car prefab with CarControlComponent script, that needs referencies to four car wheels. This script may contain some fields that are usable for prefab setup (like Max/Min speed, Rotation speed, et.c.) and that fields should be accassable by prefab user - but wheel references are constructed for in-prefab needs only and should be "incapsulated".
.
So, the question: is there any way to hide script inspector fields outside of prefab if script is used in prefab object?

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
2
Best Answer

Answer by Hellium · Jun 02, 2019 at 12:18 PM

If I understand you correctly, you want to show some serialized fields only if you are in prefab mode (>= Unity 2018.3)?


If so, you can achieve this using an attribute and a property drawer:


 // PrefabModeOnlyAttribute.cs, located in your script files
 public class PrefabModeOnlyAttribute : UnityEngine.PropertyAttribute { }


 //PrefabModeOnlyDrawer.cs, located in an Editor folder

 using UnityEngine;
 using UnityEditor;
 using UnityEditor.Experimental.SceneManagement;
 
 [CustomPropertyDrawer( typeof( PrefabModeOnly ) )]
 public class PrefabModeOnlyDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight( SerializedProperty property, GUIContent label )
     {
         return ( PrefabStageUtility.GetCurrentPrefabStage() == null ) ? 0 : EditorGUI.GetPropertyHeight( property, label );
     }
     public override void OnGUI( Rect position, SerializedProperty property, GUIContent label )
     {
         if ( PrefabStageUtility.GetCurrentPrefabStage() == null ) return;
         EditorGUI.PropertyField( position, property, label );
     }
 }


Then, in your CarControlComponent script:


 [SerializeField, PrefabModeOnly]
 private GameObject[] wheels;
Comment
Add comment · Show 2 · 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 vsemenyakin · Jun 02, 2019 at 01:40 PM 0
Share

Thank you for answer. I'll try your solution as soon as possible.
.
Code looks rather verbose - but if it works, it's possible to hide details under some classes/attributes.

avatar image vsemenyakin · Jun 02, 2019 at 10:08 PM 0
Share

Thanks a lot, man. It works! Now API of prefab scrips will be much more clean

avatar image
0

Answer by lucbloom · Jan 20 at 02:00 PM

@Hellium A good start, however this makes the fields visible in other Prefabs as well. Also there is no support for hiding fields that only make sense outside of the Prefab. (Like: a specific Sprite, Manager, Handler, System, Position, Unique name...)

My solution would be:

PrefabOnlyAttribute.cs:

 public class PrefabOnlyAttribute : PropertyAttribute { }
 public class InstanceOnlyAttribute : PropertyAttribute { }

Editor/PrefabOnlyDrawer.cs:

 [CustomPropertyDrawer(typeof(PrefabOnlyAttribute))]
 public class PrefabOnlyDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return property.isInstantiatedPrefab ? 0 : EditorGUI.GetPropertyHeight(property, label);
     }
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         if (property.isInstantiatedPrefab) return;
         EditorGUI.PropertyField(position, property, label);
     }
 }
 
 [CustomPropertyDrawer(typeof(InstanceOnlyAttribute))]
 public class InstanceOnlyDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return !property.isInstantiatedPrefab ? 0 : EditorGUI.GetPropertyHeight(property, label);
     }
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         if (!property.isInstantiatedPrefab) return;
         EditorGUI.PropertyField(position, property, label);
     }
 }

MyCode.cs:

   // Only needed when editing THIS prefab.
   [SerializeField, PrefabOnly] private GameObject[] wheels;

   // Always overwritten, never needed when editing THIS prefab.
   [SerializeField, InstanceOnly] private string driverName; 

Again, thanks to @Hellium for setting me on the right path!

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

168 People are following this question.

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

Related Questions

Prefabs shown as blue boxes and not previews 0 Answers

How to execute a script each time I add a certain prefab to the scene? 2 Answers

Prefab mode doesn't show prefab, still shows old scene. 7 Answers

Hiding gameobjects in hierarchy make it less expensive ? 0 Answers

Initialising List array for use in a custom Editor 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