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
1
Question by zee_ola05 · Feb 05, 2014 at 09:31 AM · c#inspectorgetter

Can I expose public field with getter-setter?

I want to expose bonus variable in the inspector. This is my code:

 public float bonus                                // in percent [0,1]
 {
     get { return bonus; }
     set
     {
         if (value < 0f && value > 1f)
         {
             Debug.Log("bonus should be [0,1], clamping...");
         }
         bonus = Mathf.Clamp01(value);
     }
 }

Is there a way to achieve this without doing Custom Inspector code? I also tried putting [SerializeField] on top of it but it didn't work.

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

3 Replies

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

Answer by getyour411 · Feb 05, 2014 at 09:36 AM

Not without some voodoo (and I think the terms is property, not field)

This

http://answers.unity3d.com/questions/14985/how-do-i-expose-class-properties-not-fields-in-c.html

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 zee_ola05 · Feb 05, 2014 at 09:44 AM 0
Share

Thank you. How silly of me, I actually came across that but didn't read through when I read "class" property. I thought it was different. Now I know the difference between field and property.

avatar image
2

Answer by sarahnorthway · Jun 01, 2018 at 02:42 AM

It can be done with a custom PropertyAttribute - I posted code for one at this solution:

http://answers.unity.com/answers/1513032/view.html

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
0

Answer by Vladerx · Apr 26, 2019 at 11:05 PM

You could use custom editor, i needed to serialize a getter/setter as well, this here is an example of accessing data outside of the class scope to a different component ( text color ) and an local variable ( aim position ). This potentially allows serialization of virtually anything you want ( assuming your getter and setter won't cause infinite recursion :P )

 // Scripts folder 

 public class MyObject : MonoBehaviour 
 {
     // ...

     [SerializeField] Color s_color; public Color color 
     { 
         get 
         { 
             return GetComponent<Text>().color; 
         } 
         set 
         { 
             GetComponent<Text>().color = value; 
             this.s_color = value;
         } 
     }
     
     [SerializeField] Vector2 s_aim; public Vector2 aim
     {
         get 
         {
             return s_aim;
         }
         set
         {
             this.s_aim = value;
         }
     }
     
     public void Serialize()
     {
         color = s_color;
         aim = s_aim;
     }
 }

 // Editor folder

 [CustomEditor( typeof( MyObject ) )]

 [CanEditMultipleObjects]

 public class MyObjectEditor : Editor
 {
     SerializedProperty s_color;
     SerializedProperty s_aim;

     void FindProperties()
     {
         s_color = serializedObject.FindProperty("s_color");
         s_aim = serializedObject.FindProperty("s_aim");
     }

     void DrawGUI()
     {
         EditorGUILayout.PropertyField( s_color, new GUIContent("Color") );
         EditorGUILayout.PropertyField( s_aim, new GUIContent("Aim") );
     }

     void OnEnable()
     {
         CastTargets();
         FindProperties();
     }

     MyObject m_target;
     List<MyObject> m_targets;

     void CastTargets()
     {
         m_target = ( MyObject ) target;
         m_targets = new List<MyObject>();
         foreach (var t in targets) 
         {
             MyObject obj = ( MyObject ) t;
             m_targets.Add( obj );
             obj.Initialize();
         }
     }

     public override void OnInspectorGUI()
     {
         serializedObject.Update();

         DrawGUI();

         serializedObject.ApplyModifiedProperties();

         m_targets.ForEach( obj => obj.Serialize() );
     }
 }
 

Edit: just read @sarahnorthway source code, looks like this one is easier to use +1

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

20 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

Related Questions

Multiple Cars not working 1 Answer

C# custom getter and setter question 1 Answer

Distribute terrain in zones 3 Answers

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

C# public - dropdown selection? 3 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