简体   繁体   中英

Unity3D Basic Undo scripting

Currently, I'm trying to setup a basic undo function in my game. In my setup, I'm just trying to set the gameobject to active from false to true. I have added the undo.recordobject so that unity can keep track of the changes, but what do I need to do afterward? I wanted to create a button that would allow the end-users to undo the previous actions. What steps I'm currently missing in my codes to get my software to perform an undo options?

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEditor;

public class LoadAssets : MonoBehaviour
{    
    public GameObject Mud;
    public GameObject DrillFormationMain;
    public GameObject DrillFormation1;
    public GameObject DrillFormation2;
    public GameObject DrillFormation3;

    int count = 0;

    public void DrillHole()
    {
        if (count == 0)
        {
            Mud.SetActive(true);
            DrillFormationMain.SetActive(false);
            DrillFormation1.SetActive(true);

            Undo.RecordObject(DrillFormationMain, "Change");
            DrillFormationMain.transform.position = Vector3.zero;
        }

        else if (count == 1)
        {
            DrillFormation1.SetActive(false);
            DrillFormation2.SetActive(true);

            Undo.RecordObject(DrillFormation1, "Change1");
            DrillFormation1.transform.position = Vector3.zero;
        }  
        count++;
    }   
}

Undo is part of the UnityEditor namespace.

This namespace (thus the name) only exists within the Unity editor itself and is entirely stripped of in a build

=> You can't use anything from it including ofcourseUndo.RecordObject for actual runtime code but only for custom Editor scripts.


You will have to come up with another solution for your undo/redo system.

One very simplified example might look like eg

public class UndoableAction
{
    public UndoableActionType Type;
    public UnityEngine.Object target;
    public object from;
    public object to;

    public UndoableAction(UndoableActionType type, UnityEngine.Object target, object from, object to)
    {
        Type = type;
        Target = target;
        From = from;
        To = to;
    }
}

public enum UndoableActionType
{
    Enable,
    SetActive,
    Position,

    // ... according to your needs
}

public class UndoRedoSystem : MonoBehaviour
{
    private Stack<UndoableAction> availableUndos = new Stack<UndoableAction>();
    private Stack<UndoableAction> availableRedos = new Stack<UndoableAction>();

    public void TrackChangeAction(UndoableActionType type, UnityEngine.Object target, object from, object to)
    {
        // if you change something redo is cleared
        availableRedos.Clear();

        // Add this action do undoable actions
        availableUndos.Push(new UndoableAction(type, target, from, to));
    }

    public void Redo()
    {
        if(availableRedos.Count == 0) return;

        // get latest entry added to available Redos
        var redo = availableRedos.Pop();

        switch(redo.Type)
        {
            case UndoableActionType.Enable:
                ((Component) redo.target).enabled = (bool)redo.To;
                break;

            case UndoableActionType.SetActive:
                ((GameObject) redo.target).SetActive((bool) redo.To);
                break;

            case UndoableActionType.Position:
                ((Transform) redo.target).position = (Vector3) redo.To;
                break;

            // ... According to your needs 
        }

        // finally this is now a new undoable action
        availableUndos.Push(redo);
    }

    public void Undo()
    {
        if(availableUndos.Count == 0) return;

        // get latest entry added to available Undo
        var undo = availableUndos.Pop();

        switch(undo.Type)
        {
            case UndoableActionType.Enable:
                ((Component) undo.target).enabled = (bool)undo.From;
                break;

            case UndoableActionType.SetActive:
                ((GameObject) undo.target).SetActive((bool) undo.From);
                break;

            case UndoableActionType.Position:
                ((Transform) undo.target).position = (Vector3) undo.From;
                break;

            // ... According to your needs 
        }

        // finally this is now a new  redoable action
        availableRedos.Push(undo);
    }
}

Now you could do something like eg

SomeGameObject.SetActive(true);

undoSystemReference.TrackChange((UnityEngine.Object)SomeGameObject, (object)false, (object)true);

And later somewhere

undoSystemReference.Undo();

This is ofcourse quite simplified and error prone.


Note Typed on smartphone so no warranty but I hope the idea gets clear

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM