- Home /
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);
}
}
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).
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?
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.
Just found out about SerializeField and updated the answer - this should do it for you ;-)
Your answer
Follow this Question
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