Page 1 of 1

Inspector values not registering while playing

Posted: Sun Aug 11, 2013 6:22 pm
by Roland
I figure this is already a known issue, but just in case:

Once you hit play and adjust a Chipmunk object in the inspector, the change will not actually take effect in the simulation even though it'll update the value in the inspector. This is due to using Unity's default inspector which only exposes public fields and not properties - the property accessors being the ones that take care of registering the values in Chipmunk's simulation. I figure this is a known issue since the property backing fields are being exposed publicly, which seemed weird to me.

Anyways, all that is needed is a custom inspector to access an object's properties explicitly. Like so for a ChipmunkBody:

Code: Select all

using UnityEditor;

[CustomEditor(typeof(ChipmunkBody))]
public class MyChipmunkBodyEditor : Editor
{
    public override void OnInspectorGUI()
    {
        var newMass = EditorGUILayout.FloatField("Mass", _body.mass);
        if(_body.mass != newMass)
        {
            _body.mass = newMass;
        }

        var newMomentCalculation = (ChipmunkBodyMomentMode) EditorGUILayout.EnumPopup("Moment Calculation", _body.momentCalculationMode);
        if(_body.momentCalculationMode != newMomentCalculation)
        {
            _body.momentCalculationMode = newMomentCalculation;
        }

        var newCustomMoment = EditorGUILayout.FloatField("Custom Moment", _body.moment);
        if(_body.moment != newCustomMoment)
        {
            _body.moment = newCustomMoment;
        }

        var newIsKinematic = EditorGUILayout.Toggle("Is Kinematic", _body.isKinematic);
        if(_body.isKinematic != newIsKinematic)
        {
            _body.isKinematic = newIsKinematic;
        }

        var newInterpolationMode = (ChipmunkBodyInterpolationMode) EditorGUILayout.EnumPopup("Interpolation Mode", _body.interpolationMode);
        if(_body.interpolationMode != newInterpolationMode)
        {
            _body.interpolationMode = newInterpolationMode;
        }
    }

    private ChipmunkBody _body;

    private void OnEnable()
    {
        _body = (ChipmunkBody) target;
    }
}
But then modifying some properties, like isKinematic, while playing will break the simulation. Any plans on addressing this soon or should I go about implementing my own inspectors?
Thanks

Re: Inspector values not registering while playing

Posted: Sun Aug 11, 2013 11:31 pm
by slembcke
Actually that's really good to know. That was one of the post-1.0 things I wanted to look into was if we could get around that exact issue using a custom inspector. I guess they are easier to make than I figured. That ended up being pushed off the 1.0 list (had to draw the line somewhere). Will definitely look into this though. Thanks!

Re: Inspector values not registering while playing

Posted: Mon Aug 12, 2013 10:22 pm
by Roland
Sweet, I have contributed!
Yeah, generic inspectors that basically modify values or execute something with limited dependencies aren't hard at all to implement - but the more... specific, I guess, things need to be, the harder it is to make Unity bend to your will. Good luck with your post release stuff, really looking forward to it. Already getting some good use of Chipmunk over PhysX.

Yep.