python - Setting a Unity SerializedProperty value correctly -
i'm using ironpython (editor only) cleanup work within unity3d project. works great except 1 irritating gotcha.
i've got python wrapper class hides c# verbosity of using serializedproperties:
class propertyproxy(object): proptypes = { serializedpropertytype.integer:"intvalue", serializedpropertytype.boolean:"boolvalue", serializedpropertytype.float:"floatvalue", serializedpropertytype.string:"stringvalue", serializedpropertytype.color:"colorvalue", serializedpropertytype.objectreference:"objectreferencevalue", serializedpropertytype.layermask:"objectreferencevalue", serializedpropertytype.enum:"enumvalueindex", serializedpropertytype.vector2:"vector2value", serializedpropertytype.vector3:"vector3value", serializedpropertytype.vector4:"vector4dvalue", serializedpropertytype.rect:"rectvalue", serializedpropertytype.arraysize:"arraysize", serializedpropertytype.character:"objectreferencevalue", serializedpropertytype.animationcurve:"animationcurvevalue", serializedpropertytype.bounds:"boundsvalue", serializedpropertytype.gradient:"objectreferencevalue", serializedpropertytype.generic:"objectreferencevalue" } def __init__(self, owner): self.owner = owner self.prop_path = self.proptypes[owner.propertytype] def __repr__(self): homecoming self.owner.serializedobject.targetobject.name +"." + self.owner.propertypath @property def value(self): if self.owner.isarray: homecoming [] else: homecoming getattr(self.owner, self.prop_path) @value.setter def set_value(self, val): setattr(self.owner, self.prop_path, val) self.owner.serializedobject.applymodifiedproperties()
the get
functionality works fine, , returns right values. setter, however, complains. if seek set value of propertyproxy
error claiming i'm trying set read-only attribute. isn't literally true - can this:
mypropertyproxy.owner.floatvalue = 2.0
with no problem, when setter trying
setattr (mypropertyproxy.owner, 'floatvalue', 2.0)
i error.
i assume kind of issue ironpython beingness unable right setter property on unity side, i'm guessing. have more info on what's going on here?
update
issuing
setattr(myprop.owner, 'floatvalue', 2.0) myprop.owner.serializedobject.applymodifiedproperties()
directly - outside class - works. grrr.
update 2 value
, set_value
functions work if phone call them without property decorator :(
i'm not sure understand trying do, anyway speaking serializedproperty.
when want set value of serializedproperty
(typically writing custom inspectors code) have several ways:
serializedproperty
, deserialize serializedobject(like approach) wrap serializedproperty access using editorgui.[begin|end]property access straight serialized field. now, every approach depends on specific utilize case , has drawbacks. main thing maintain in mind [de]serialization process in handled different thread need know serializedproperty
has been modified before deserialize back.
so in case (if understood correctly) need create @ to the lowest degree 2 functions phone call in order create work in every situation:
call update (or updateifdirtyorscript depending on case) before modifying property. ensure referring consistentserializedobject
state. modify serializedproperty
(like doing) call serializedobject.applymodifiedproperties
(like in update) i assume kind of issue ironpython beingness unable right setter property on unity side, i'm guessing. have more info on what's going on here?
i don't know much python don't have clear reply related fact floatvalue setter referes , external function (probably in c++ side of engine):
public extern float floatvalue { [wrapperlessicall] [methodimpl(methodimploptions.internalcall)] get; [wrapperlessicall] [methodimpl(methodimploptions.internalcall)] set; }
python unity3d ironpython
No comments:
Post a Comment