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 claytoncurmi · Aug 13, 2010 at 07:14 AM · editorvariablespropertiessettersgetters

Accessing class properties through getters & setters in editor mode

Hi!

I'm trying to set class variables through getters & setters. I wrote a custom editor that exposes these methods. The code works just fine, that is if I change values while the game is in play mode, these values are passed to the respective variables. However if I set the values in edit mode, the values are lost as soon as I click on the Play button (values are actually set to the default). I've added the @script ExecuteInEditMode() but to no avail! How can persist the changes I've made in the custom editor while in edit mode?

Thanks!

Code for class SpriteController:

@script ExecuteInEditMode()

private var columns : int;

//Getters & setters function SetColumns(value : int){ if(value < 0){ value = 0; }

 columns = value;

}

function GetColumns() : int{ return columns; }

Code for SpriteControllerEditor:

@CustomEditor (SpriteController)

class SpriteControllerEditor extends Editor {

function OnInspectorGUI () {

 target.SetColumns(EditorGUILayout.IntField("Columns", target.GetColumns()));
 if (GUI.changed)
     EditorUtility.SetDirty(target);

}

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by jashan · Aug 13, 2010 at 07:32 AM

[EDIT: Ooops, just learned something new - in fact, there's an attribute for that: SerializeField. So, you could do

@SerializeField
private var columns : int;

and it should work.]

Private member variables are not persisted by Unity [unless you use "SerializeFiled", see above]. So you'll have to write:

public var columns : int;

Personally, when I have public member variables that I only have for "technical reasons", I make sure I don't call them from outside the class where they are defined because it's lower case (and "lower-case" means "private" to me, upper-case means "real public properties").

Unfortunately, the way Unity is designed, you have to make any state variables that should be persistent public; so you need your own conventions to keep things clean and tidy (like the "lower case means private").

The reason "lower case means private" works very naturally for me is due to the C# background and the availability of "real properties":

public int Columns {
    get { return columns; }
    set {
        if (value < 0) {
            value = 0;
        }
        columns = value;
    }
}

Those are always uppercase (at least if you follow common conventions).

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 claytoncurmi · Aug 13, 2010 at 09:49 AM 0
Share

aaa yes it worked! but not the ideal solution actually! it kind of defeats the whole purpose of encapsulation! so there is no other solution?

avatar image jashan · Aug 13, 2010 at 04:31 PM 0
Share

I know, it's not quite ideal ... but on the other hand, it's pretty nice how Unity handles the whole aspect of persisting values for you. It would certainly be nice if we could also somehow mark actual properties as persistent, so if you add a request for that on Unity Feedback and let me know I'll be sure to give my vote ;-) ... at the moment, I don't see a better solution as Unity doesn't expose much of how it does it's serialization / deserialization.

avatar image jashan · Aug 16, 2010 at 11:24 AM 0
Share

Just found out about SerializeField and updated the answer - this should do it for you ;-)

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

No one has followed this question yet.

Related Questions

Update Variables from script to script? 1 Answer

Properties names, same as class okay or bad? 1 Answer

Proper use of c# properties? 1 Answer

Getter / Setter and Variable Scope Question? 1 Answer

Are properties variables? 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