c# – Methods to allow undo performance customized editor – unity

c# – Methods to allow undo performance customized editor – unity

[ad_1]

I am engaged on a customized inspector/editor for my participant controller, the difficulty is each time I alter one thing and click on undo, the undo simply undoes clicking on the thing not modifying the worth. After some analysis I attempted Undo.RecordObject(serializedObject, "Modified Participant Controller"); but it surely was searching for an object not a GameObject. I additionally appeared into EditorUtility.SetDirty() however plainly does not truly replace the Undo. That is fairly particular to my use case as a result of I’ve completed issues barely in a different way, I did not perceive SerializedProperty when making the script (and nonetheless dont) so I principally get all of the values after which set them, which I do know is a trouble but it surely labored wonderful so there was no purpose to not do it. I additionally added serializedObject.ApplyModifiedProperties, however that didnt appear to do something.

Right here is my code, you possibly can ignore a lot of the precise UI:

public override void OnInspectorGUI()
{
    var playerController = (PlayerController)goal;
    if (playerController == null) return;

    // GET VALUES
    // extras
    _jumpCountIndex = playerController.jumpIndex;
    _groundCheckType = playerController.groundCheckType;
    _obj = playerController.groundCheckObj;
    
    // motion vars
    _movementSpeed = playerController.moveSpeed;
    _maxFallSpeedEnabled = playerController.maxFallSpeedEnabled;
    _maxFallSpeed = playerController.maxFallSpeed;

    // soar vars
    _jumpForce = playerController.jumpForce;
    _maxJumpTime = playerController.maxJumpTime;
    _jumpCount = playerController.jumpCount;
    _coyoteTime = playerController.coyoteTime;
    _groundCheckScale = playerController.groundCheckScale;
    _groundCheckPosition = playerController.groundCheckPosition;
    _groundLayer = playerController.groundLayer;

    // sprint vars
    _forceOverTime = playerController.forceOverTime;
    _dashTime = playerController.dashTime;
    _dashCooldown = playerController.dashCooldown;
    _dashOnGround = playerController.dashOnGround;

    // align vars
    _alignmentEnabled = playerController.alignmentEnabled;
    _maxAngle = playerController.maxAngle;
    _floorCheckDistance = playerController.floorCheckDistance;

    if (Utility.isPlaying) {
        serializedObject.Replace();
    }   

    EditorGUI.indentLevel = 0;

    GUIStyle headerStyle = new GUIStyle(EditorStyles.boldLabel);
    headerStyle.regular.background = Texture2D.whiteTexture;
    headerStyle.fontSize = 16;
    headerStyle.fontStyle = FontStyle.Daring;
    headerStyle.alignment = TextAnchor.MiddleCenter;
    headerStyle.regular.textColor = Coloration.black;

    GUIStyle foldoutStyle = new GUIStyle(EditorStyles.foldoutHeader);

    EditorGUILayout.LabelField("Participant Controller +", headerStyle, GUILayout.MinHeight(25));

    isMovementDropDown = EditorGUILayout.Foldout(isMovementDropDown, "Motion Settings", true, foldoutStyle);
    if (isMovementDropDown) {
        EditorGUI.indentLevel++;
        // motion velocity
        _movementSpeed = EditorGUILayout.FloatField("Motion Pace", _movementSpeed, GUILayout.MinWidth(50));
        
        // max falling velocity
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Max Fall Pace", GUILayout.MaxWidth(140));
        _maxFallSpeedEnabled = EditorGUILayout.Toggle(_maxFallSpeedEnabled, GUILayout.MaxWidth(30));
        EditorGUI.BeginDisabledGroup(!_maxFallSpeedEnabled);
        _maxFallSpeed = EditorGUILayout.FloatField(_maxFallSpeed);
        EditorGUI.EndDisabledGroup();
        EditorGUILayout.EndHorizontal();
        EditorGUI.indentLevel--;
    }
    isJumpDropDown = EditorGUILayout.Foldout(isJumpDropDown, "Bounce Settings", true, foldoutStyle);
    if (isJumpDropDown) {
        EditorGUI.indentLevel++;
        // soar power & max time
        _jumpForce = EditorGUILayout.FloatField("Bounce Pressure", _jumpForce);
        _maxJumpTime = EditorGUILayout.FloatField("Max Bounce Time", _maxJumpTime);

        // soar depend
        string[] names = {"Single", "Double", "Triple", "Customized"};
        int[] quantities = {1, 2, 3, -1};
        _jumpCountIndex = EditorGUILayout.IntPopup("Bounce Rely", _jumpCountIndex, names, quantities);
        if (_jumpCountIndex == -1) {
            EditorGUI.indentLevel++;
            _jumpCount = Mathf.Abs(EditorGUILayout.IntField("Customized Quantity", _jumpCount));
            EditorGUI.indentLevel--;
        }
        else {
            if (_jumpCountIndex == 0) _jumpCountIndex = 1;
            _jumpCount = _jumpCountIndex;
        }
        playerController.jumpCount = _jumpCount;
        _coyoteTime = EditorGUILayout.Slider("Coyote Time", _coyoteTime, 0, 1);

        groundCheckDropDown = EditorGUILayout.Foldout(groundCheckDropDown, "Floor Verify Settings", true);
        if (groundCheckDropDown) {
            EditorGUI.indentLevel++;
            string[] typeNames = {"Object Remodel", "Vector 2"};
            int[] typeValues = {0, 1};
            _groundCheckType = EditorGUILayout.IntPopup("Floor Verify Kind", _groundCheckType, typeNames, typeValues);
            swap (_groundCheckType) {
                case 0:
                    _obj = EditorGUILayout.ObjectField("Object", _obj, typeof(GameObject), true) as GameObject;
                    if (_obj != null) {
                        _groundCheckPosition = _obj.rework.place;
                        _groundCheckScale = _obj.rework.localScale;
                    }
                    else {
                        if (Utility.isPlaying)
                            Debug.LogWarning("GroundCheck Object is unassigned. Floor checking might not work accurately.");
                        _groundCheckPosition = new Vector2(0, 0);
                        _groundCheckScale = new Vector2(0, 0);
                    }
                    break;
                case 1:
                    _groundCheckPosition = EditorGUILayout.Vector2Field("Relative Place", _groundCheckPosition);
                    _groundCheckScale = EditorGUILayout.Vector2Field("Relative Scale", _groundCheckScale);
                    break;
            }

            _groundLayer = EditorGUILayout.LayerField("Floor Layer", _groundLayer);
            EditorGUI.indentLevel--;
        }
        EditorGUI.indentLevel--;
    }

    isDashDropDown = EditorGUILayout.Foldout(isDashDropDown, "Sprint Settings", true, foldoutStyle);
    if (isDashDropDown) {
        EditorGUI.indentLevel++;
        GUILayout.BeginHorizontal();
        _forceOverTime = EditorGUILayout.CurveField("Sprint Pressure over Time", _forceOverTime, Coloration.inexperienced, new Rect(0, 0, 1, 30));
        if (GUILayout.Button("Reset", GUILayout.MaxWidth(80))) ResetCurve(ref _forceOverTime);
        GUILayout.EndHorizontal();
        _dashTime = EditorGUILayout.FloatField("Sprint Time", _dashTime);
        _dashCooldown = EditorGUILayout.FloatField("Sprint Cooldown", _dashCooldown);
        _dashOnGround = EditorGUILayout.Toggle("Sprint On Floor", _dashOnGround);
        EditorGUI.indentLevel--;
    }

    alignmentDropDown = EditorGUILayout.Foldout(alignmentDropDown, "World Align Settings", true);

    isOtherDropdown = EditorGUILayout.Foldout(isOtherDropdown, "Superior", true, foldoutStyle);
    if (isOtherDropdown) {
        EditorGUI.indentLevel++;
        showDefaultInspector = EditorGUILayout.Toggle("Present Default Inspector", showDefaultInspector);
        EditorGUI.indentLevel--;
    }


    EditorGUILayout.BeginVertical();
    GUILayout.House(10);
    GUILayout.FlexibleSpace();
    EditorGUILayout.BeginHorizontal();
    GUILayout.FlexibleSpace();
    if (GUILayout.Button("Open Character Creator", EditorStyles.miniButtonLeft, GUILayout.MinWidth(180))) {
        CharacterCreator.ShowCharacterCreator();
    }
    if (GUILayout.Button("Add Debugger", EditorStyles.miniButtonMid, GUILayout.MinWidth(130))) {
        goal.AddComponent<PlayerDebugger>();
    }
    if (GUILayout.Button("Assist", EditorStyles.miniButtonRight, GUILayout.MinWidth(70))) {

    }
    EditorGUILayout.EndHorizontal();
    GUILayout.EndVertical();

    GUILayout.House(10);

    if (showDefaultInspector) DrawDefaultInspector();

    // SETTING VARS
    // ------------
    // extras
    playerController.jumpIndex = _jumpCountIndex;
    playerController.groundCheckType = _groundCheckType;
    playerController.groundCheckObj = _obj;

    // motion vars
    playerController.moveSpeed = _movementSpeed;
    playerController.maxFallSpeedEnabled = _maxFallSpeedEnabled;
    playerController.maxFallSpeed = _maxFallSpeed;

    // soar vars
    playerController.jumpForce = _jumpForce;
    playerController.maxJumpTime = _maxJumpTime;
    playerController.jumpCount = _jumpCount;
    playerController.coyoteTime = _coyoteTime;
    playerController.groundCheckScale = _groundCheckScale;
    playerController.groundCheckPosition = _groundCheckPosition;
    playerController.groundLayer = _groundLayer;

    // sprint vars
    playerController.forceOverTime = _forceOverTime;
    playerController.dashTime = _dashTime;
    playerController.dashCooldown = _dashCooldown;
    playerController.dashOnGround = _dashOnGround;

    serializedObject.ApplyModifiedProperties();
}

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply