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 metroidsnes · Mar 25, 2015 at 07:05 PM · inspectorpropertyfield

How to check if inspector object field was changed

I draw in the inspector property to ScriptableObject like this:

 EditorGUILayout.PropertyField(myScriptableObject);

How can I detect when reference was added/removed in the inspector?

For eg. when I remove reference with DEL key, I would like to call an event or do sth.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
22

Answer by Kamilche_ · Apr 24, 2016 at 05:12 PM

@metroidsnes , you could do the following:

 EditorGUI.BeginChangeCheck();
 EditorGUI.PropertyField(r, sp, GUIContent.none);
 if (EditorGUI.EndChangeCheck())
 {
     // Do something when the property changes 
 }

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
avatar image
8

Answer by NotHalfBrad · Sep 16, 2015 at 12:29 PM

You can use MonoBehavior.OnValidate()

It runs whenever there is a change to a property of the component script it is in (I don't know how it behaves when interacting with custom inspectors). It works in both runtime and design-time.

From there, you could check if the value has changed to null, and I suspect that would be similar to checking for a deleted value like you want.

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 krooq · Mar 28, 2021 at 03:05 AM 0
Share

(I don't know how it behaves when interacting with custom inspectors)

I couldn't get OnValidate working for custom inspectors. But the following is effectively the same and does work :) EditorGUI.BeginChangeCheck(); EditorGUI.PropertyField(r, sp, GUIContent.none); if (EditorGUI.EndChangeCheck()) { ((MonoBehaviour)target).OnValidate(); }
avatar image
2

Answer by c8theino · May 04, 2021 at 11:01 AM

Here is a simple system which let's you keep track of changed variables and call functions if value is changed.

 using UnityEditor;
 
 [CustomEditor(typeof(Foo))]
 public class FooEditor : Editor {
 
     public SerializedProperty value;
 
     void OnEnable() {
         value = serializedObject.FindProperty("value");
     }
 
     public override void OnInspectorGUI() {
         // Get value before change
         int previousValue = value.intValue;
 
         // Make all the public and serialized fields visible in Inspector
         base.OnInspectorGUI();
 
         // Load changed values
         serializedObject.Update();
 
         // Check if value has changed
         if (previousValue != value.intValue) {
             // Do something...
             Foo foo = (Foo)target;
             foo.CallPublicFunction();
         }
 
         serializedObject.ApplyModifiedProperties();
     }
 }
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
avatar image
1

Answer by JigneshKoradiya · Mar 25, 2015 at 07:13 PM

you can check it with,check manual for more reference

if(GUI.changed) {

}

Comment
Add comment · Show 7 · 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 metroidsnes · Mar 25, 2015 at 07:37 PM 0
Share

GUI.changed will be true for any changed inspector control. I need to know when only the ScriptableObject field was changed.

avatar image JigneshKoradiya · Mar 25, 2015 at 07:42 PM 0
Share

write you script here so i can edit it for you

avatar image metroidsnes · Mar 26, 2015 at 05:41 AM 0
Share
 private void DrawPathDataAssetField(Action callback = null) {
             serializedObject.Update();
 
             // Remember current pata data field value.
             var prevPathData = pathData.objectReferenceValue;
 
             EditorGUILayout.PropertyField(
                 pathData,
                 new GUIContent(
                     "Path Asset",
                     "Asset containing all path data."));
 
             serializedObject.Apply$$anonymous$$odifiedProperties();
 
             Debug.Log("prev: " + prevPathData + " new: pathData.objectReferenceValue" + );
 
             // If path data was changed, execute callback.
             if (!ReferenceEquals(pathData.objectReferenceValue, prevPathData)) {
             if (callback != null) callback();
             }
         }

Callback never gets executed. When there's no reference assigned and I add one, this is the result:

alt text

log-window.png (21.6 kB)
avatar image metroidsnes · Mar 26, 2015 at 01:44 PM 0
Share

Problem solved, it was my fault. Ins$$anonymous$$d of changing the said field, I used a custom button to create new asset file, so I was checking something that didn't even change.

avatar image Ruhan-_- metroidsnes · Oct 24, 2018 at 08:02 AM 0
Share

@metroidsnes Could u elaborate on what the solution u found was? I'm quite close to finishing the property drawer I need with one hiccup:

When a value is placed in to the property field slot, I can get the reference But when the value is removed from the slot, it is as your screenshot showed, I can't get the reference back of the property that was just removed (because I need to update something on THAT removed object) Ins$$anonymous$$d I just get null for both Essentially I need a way to get a "cache" of the previous property value reference, it is the LAST missing piece of the puzzle I need to complete my drawer :P I'd be VERY grateful if you could help out! Thank you in advance :)

avatar image metroidsnes Ruhan-_- · Oct 25, 2018 at 06:17 AM 0
Share

Sorry, I don't remember anymore how this stuff works. I can only give you a link to my project, maybe you can find solution there.

Project: AnimationPath Animator. Editor class: PathAnimatorEditor.cs

Show more comments

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

26 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

Related Questions

Unable to draw propertyfield in inspector for some classes, findProperty returns null. 0 Answers

Why are the children of my Serialized Property not being drawn? 1 Answer

How to indent EditorGUILayout.BeginToggleGroup() 1 Answer

C# public - dropdown selection? 3 Answers

Making a character's idle animation loop. (model and animation created in blender) 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