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
4
Question by Filippopotamus · Aug 02, 2012 at 07:40 PM · inspectorencapsulation

Force inspector to use getter/setter?

I need to expose a variable on the inspector. But when I change that variable through the inspector, I need other things to happen. So I tried making a setter for that variable. But when I change that exposed variable, it gets changed directly and the inspector bypasses the setter. Is there a way to force the inspector to use the setter? Or is there another way to force things to happen once a variable gets changed in the inspector? Thanks in advance!

Cheers!

Comment
Add comment · Show 1
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 badweasel · Jan 16, 2015 at 04:36 AM 0
Share

It's good to know that the inspector doesn't use them. Cause I've been scratching my head like I was doing something wrong.

2 Replies

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

Answer by $$anonymous$$ · Aug 04, 2012 at 01:09 AM

I would write a custom inspector for this problem. To start off you would write your normal Monobehavior script like the following and attach it to a gameObject where you will be able to see the public property visible. But when you get/set the value the Debug.Logs inside Setter/Getter would not print.

 using UnityEngine;    
 using System.Collections;
 
 public class UseGetterSetter : MonoBehaviour 
 {
  // This will be displayed in the inspector as usual
  public int publicVariable;
  
  // public property of the variable
  public int PublicVariableProperty
  {
  get 
  { 
  Debug.Log("Getter!");
  return publicVariable; 
  }
  set 
  { 
  Debug.Log("Setter!");
  publicVariable = value; 
  }
  }
 }


Now you have to create a custom inspector like such to take advantage of your getter/setter.

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(UseGetterSetter))]
 public class CustomInspector : Editor {
  public override void OnInspectorGUI () 
  {
  base.OnInspectorGUI();
  
  // Take out this if statement to set the value using setter when ever you change it in the inspector.
  // But then it gets called a couple of times when ever inspector updates
  // By having a button, you can control when the value goes through the setter and getter, your self.
         if (GUILayout.Button("Use setters/getters"))
  {
  if(target.GetType() == typeof(UseGetterSetter))
  {
  UseGetterSetter getterSetter = (UseGetterSetter)target;
  getterSetter.PublicVariableProperty = getterSetter.publicVariable;
  Debug.Log(getterSetter.PublicVariableProperty);
  }
  }
  }
 }


like I explained in the comment, you could take out the 'button' line in the inspector code (`if (GUILayout.Button("Use setters/getters"))`) so that what ever you do in the editor to that variable will be reflected back on it through our getter/setter. But it gets called many times. If you don't wish that. You could simply set the value and press the button when ever you want to take advantage of using your getter/setter.

Comment
Add comment · Show 3 · 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 Filippopotamus · Aug 04, 2012 at 01:17 AM 0
Share

Very neat! I really appreciate the help. Thanks for taking the time! :)

avatar image $$anonymous$$ · Aug 04, 2012 at 01:29 AM 0
Share

No worries. Glad to be of help.

avatar image nicloay · Oct 10, 2012 at 10:25 AM 1
Share

for publicVariable property in first class you need to include [SerializeField] attribute on it

avatar image
-1

Answer by Xorxor · Jan 04, 2016 at 02:46 PM

Try this:

 [SerializeField]
 private string _titleDisplayString;
 public string titleDisplayString
 {
     get { return _titleDisplayString; }
     set
     {
         _titleDisplayString = value;
         // Now do awesome things
         AndAwayWeGo(value);
     }
 }
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 callen · Jan 04, 2016 at 02:57 PM 0
Share

This code won't do what he's asking. In fact, all it does is expose the private var, making it identical to this, inspector-wise:

  [SerializeField]
  private string _titleDisplayString;
avatar image KeithKong · Sep 21, 2016 at 07:30 PM 0
Share

This solution works if the initial value doesn't need the setter logic but code access does (or if the Awake function takes care of calling the custom setter). Rather handy for my purposes, thanks!

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

12 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

Related Questions

Is it possible to dynamically disable or validate properties in the inspector? 3 Answers

Can I expose public field with getter-setter? 3 Answers

Resources loading vs public linking in inspector 1 Answer

How do i NOT break encapsulation with Unity? 2 Answers

Array of abstract class in inspector 2 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