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
2
Question by vexe · Feb 12, 2014 at 08:12 AM · editorobjectfield

[CanEditMultipleObjects] doesn't actually work for an ObjectField?

If I had two GameObjects GO_1 and GO_2 both with a MonoBehaviour Test. In Test there's just a GameObject reference.

 public class Test : MonoBehaviour
 {
    public GameObject testGo;
 }

Now if I select the two GOs I'll get a "Doesn't support multi editing" for the "Test" component. OK, so I make a custom editor for MB, and mock it with [CanEditMultipleObjects] - And in OnInspectorGUI, I just call the base. Now, I can select the two objects, and when I assign a GO to testGo, the object will gets assigned to both the references, cool!

But, if I override OnInspectorGUI and make an ObjectField for testGO, suddenly when I select the two objects and try to assign something to 'testGo' - it will only get assigned to the last object I selected, not both!

Why is this happening? and how to get multi editor support in this case? am I missing something? is there something else I should do?

Thanks!

EDIT: And please, don't reply to me with something related to SerializedProperties - My question is about the CanEditMultipleObjects attribute.

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

Answer by Jamora · Feb 12, 2014 at 12:11 PM

It would seem you need to manually assign the changed value in one script to all other selected scripts.

 [CanEditMultipleObjects]
 [CustomEditor(typeof(listScript))]
 public class ListEditor : Editor {
 
     listScript _target;
     MonoBehaviour s = null;
 
     void OnEnable(){
         _target = (listScript)target;
         s = _target.script;
     }
 
     public override void OnInspectorGUI(){
         EditorGUI.BeginChangeCheck();
         s = (MonoBehaviour)EditorGUILayout.ObjectField(_target.script,typeof(MonoBehaviour),true);
         if(EditorGUI.EndChangeCheck()){

         /*manual assignment here, remember to check that the selected objects
           are in fact of the appropriate type.*/
         foreach(Object obj in targets){ 
                 ((listScript)obj).script = s;
             }
         }
     }
 }

This will not show the dotted line whenever objects have differing values, like SerializedProperties would. This just overrides the old value in all objects.

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 keenanwoodall · Oct 01, 2018 at 12:41 AM 0
Share

I know I'm a little late to the party, but how do you support undoing change to multiple objects? I tried this:

 EditorGUI.show$$anonymous$$ixedValue = !targetsHaveSameUpdate$$anonymous$$ode;
 EditorGUI.BeginChangeCheck ();
 var newUpdate$$anonymous$$ode = (Update$$anonymous$$ode)EditorGUILayout.EnumPopup (update$$anonymous$$odeContent, firstUpdate$$anonymous$$ode);
 if (EditorGUI.EndChangeCheck ())
 {
     Undo.RecordObjects (targets, "Changed Update $$anonymous$$ode");
     foreach (var t in targets)
     {
         var deformable = t as Deformable;
         deformable.Update$$anonymous$$ode = newUpdate$$anonymous$$ode;
     }
 }

Setting the update mode of multiple objects works great, but nothing happens when I undo the change. I tried this as well:

 EditorGUI.show$$anonymous$$ixedValue = !targetsHaveSameUpdate$$anonymous$$ode;
 EditorGUI.BeginChangeCheck ();
 var newUpdate$$anonymous$$ode = (Update$$anonymous$$ode)EditorGUILayout.EnumPopup (update$$anonymous$$odeContent, firstUpdate$$anonymous$$ode);
 if (EditorGUI.EndChangeCheck ())
 {
     foreach (var t in targets)
     {
         var deformable = t as Deformable;
         Undo.RecordObject (deformable, "Changed Update $$anonymous$$ode");
         deformable.Update$$anonymous$$ode = newUpdate$$anonymous$$ode;
     }
 
     Undo.CollapseUndoOperations (targets.Length - 1);
 }

but had no luck.

avatar image keenanwoodall keenanwoodall · Oct 01, 2018 at 01:01 AM 0
Share

I'm an idiot. It didn't work because I was changing properties and the private fields that the properties were restricting access to weren't serialized.

avatar image
6

Answer by Darkgaze · Feb 27, 2020 at 04:43 PM

This is not explained in the documentation and I think it is quite important.

If you have your own properties, not using the automatically managed Serialized Properties, then you have to use "targets" instead of "target". I noted that if you select different types of objects with no shared script, it doesn't show shared properties, so we don't need to check if the targets are all the same type. Then you do whatever you want to do by hand with each of them.

Here's a working example of the contents of OnInspectorGUI method inside an Editor class with a simple checkbox that is changed across several scripts. Hope it helps.

 var myScript = (UnityTerrainWrapper)target;
 var allSelectedScripts = targets;
 
 EditorGUI.BeginChangeCheck();
 var value = GUILayout.Toggle(myScript.ShowNativeTerrain, "Draw Unity Terrain");
 if (EditorGUI.EndChangeCheck())
 {
       foreach (var script in allSelectedScripts)
            ((UnityTerrainWrapper)script).ShowNativeTerrain = value;
      SceneView.RepaintAll();
 }

 DrawDefaultInspector();


Comment
Add comment · Show 1 · 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 PixelFireXY · Jun 23, 2021 at 09:23 AM 0
Share

Thank you very much, you save my day! :)

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

21 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

Related Questions

Is there any way to associate a "custom asset" with a particular file type? 1 Answer

Filter "Select XXX" dialog in Editor with custom code? 1 Answer

How can I restrict the object selection to prefabs only? 1 Answer

EditorGuiLayout - Objectfield 1 Answer

Are there any Editor fields which are Drag and Drop? 0 Answers


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