Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by nopesal · Feb 06, 2019 at 06:43 PM · vrgameobjectsoculusvirtualundo

How to make an undo redo system in runtime for GameObjects?

Hello,

I'm implementing a VR app where I have objects like cubes, prisms, and spheres around the scene. I can move, scale and rotate them in runtime using the controller pointer and some UI elements. I'm trying to make two UI buttons: one for undoing the latest action and another one for redoing the next action, in case there is one. For example, if I have a prism and I rotate it, and then I move the sphere to another place, if I press the undo button I want to set the sphere at the point it was before I moved it, and if I press undo another time, the prism will rotate as it was before I touched it.

I don't know how to implement this behavior. I suppose I need a List but I don't know how many, or if I need more than one or none at all.

I tried one thing and failed: I have a List of GameObjects where I store each shape I change in some kind of way, and I was expecting to destroy the latest GameObject in that array and instantiate the next one in case I press the undo button. The problem is that if I destroy a GameObject, the one in the List also destroys. I was thinking about having a Serializable with the properties of the shapes I have but I don't know how to reference shapes, knowing the latest actions, the previous states... I'm kind of lost here.

If someone knows how to make this AT RUNTIME (not in the Editor), I would appreciate an explanation about what data structure you recommend and how to make the impression of delete a shape and other one appears. I don't need literally code but some kind of guidance on how to achieve this behavior.

Thank you so much and I hope I explained myself well.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by misher · Feb 07, 2019 at 07:09 PM

Each interaction you do with an object you should write to a Stack (similar to the list but more suitable for this kind of situation). Therefore you should create some class for it, name it "interaction" for example ;) IThere can be some abstract class with a couple of methods or an interface. Then you have specific classes for different types of interactions, for example, translateInteraction, scaleInteraction, rotateInteraction, changeColorInteraction and so on. They all have a method called "Undo" for example, plus some state to which they should return, this state is registered before interaction occurs (there is an interaction manager or something, who trigger saving the state for each interaction). You can store interaction stack inside the interaction manager for all interactions or separately for each object to manipulate depending on your need. Again, each time you interact with an object, the interaction manager creates new interaction instance object of a given type and store it into the stack, once you activate "undo" function, you pop the last element from the stack and use it for undo. If you use the single stack for all objects in the scene, your interaction records should also have a pointer to the object for which to apply the undo. Hope this will not confuse you more than help :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

Answer by toddisarockstar · Feb 06, 2019 at 08:00 PM

i would use a custom class that holds everything about the state of your shape that can be changed. and use that class in a list. this a basic example to get you started but you would pry want to add color, size and whatever else.

also Dont Destroy your gameobects.... toggle whether they are active in the scene or not. then you can refer back to them when they are "gone".

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class undoit : MonoBehaviour {
 
     public class setting {
         public GameObject Obj;
         public Vector3 Pos;
         public Quaternion Rot;
         public bool Deleted;
         
         public void Restore(){
             Obj.transform.position = Pos;
             Obj.transform.rotation = Rot;
             Obj.SetActive (Deleted);
         }
         public setting(GameObject g){
             Obj = g;
             Pos = g.transform.position;
             Rot = g.transform.rotation;
             Deleted = g.activeSelf;
             
         }}
     
     public List<setting> UndoList;
     
     public void AddUndo (GameObject g){
         setting s = new setting (g);
         UndoList.Add (s);
     }
     
     public void Undo (){
         if (UndoList.Count > 0) {
             UndoList[UndoList.Count-1].Restore();
             UndoList.RemoveAt(UndoList.Count-1);
         }}
 
     void Start () {
         UndoList = new List<setting> ();
     }
     
 
 }
 
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nopesal · Feb 07, 2019 at 06:10 PM 0
Share

Thank you for your response! I have a doubt only: if I have a cube, and then move it to another position, and then press "Undo", how should I make the visible one dissapear and the previous one appear? Because it's the same cube. You said that I should hide and active the gameobjects, but if I move one cube, it's active and after I moved it it's still active, it hasn't been hided, right?

avatar image DeveloperJake nopesal · Dec 29, 2021 at 04:05 AM 0
Share

I have the same question

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Oculus Go - Everything looks zoomed in, Change FoV 0 Answers

Unity crash when using Oculus Rift and SteamVR 0 Answers

How to make an OVR Camera Rig LookAt a particular direction? 4 Answers

AutoWalk Oculus Rift 0 Answers

Virtual Reality Toolkit 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges