Inspector values not registering while playing

Chipmunk2D Bindings for the Unity3D engine
Post Reply
User avatar
Roland
Posts: 16
Joined: Thu Aug 08, 2013 3:32 pm
Contact:

Inspector values not registering while playing

Post 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
Last edited by Roland on Mon Aug 12, 2013 12:20 pm, edited 1 time in total.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Inspector values not registering while playing

Post 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!
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
Roland
Posts: 16
Joined: Thu Aug 08, 2013 3:32 pm
Contact:

Re: Inspector values not registering while playing

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests