Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DJ-Coco · Apr 21, 2018 at 05:18 PM · unityeditorundo

Undo Class works inconsistently

I am absolutely stumped by Unity's Undo class. Everytime I use it, it seems pretty straight forward at first only to realize that it never works as expected later on. This time it's especially weird because occassionally it seems to work just as it should, but it also fails to undo stuff correctly half of the time. Namely, the _tiles variable of type List seems to mess up a lot. Half the time, when I destroy tiles using the DestroyTile function and undo it afterwards, the list is recreated so it has the same amount of entries it had previously, but all of them sans one now contain the Vector2 (0, 0) instead of the value it had. Both List and Vector2Int should be serialize, but since I wasn't sure about Vector2Int I also tried with the vanilla Vector2 with identical results. Any help would be appreciated. I also tried using Undo.RecordObject in the Create/Destroy functions themselves, but it didn't make a difference.

StageCreatorEditor:

 private void OnSceneGUI() {
     var e = Event.current;
     var stageCreator = (StageCreator)target;
 
     //Get control ID
     var id = GUIUtility.GetControlID(6, FocusType.Passive);
 
     //Place and remove tiles
     if (e.type == EventType.MouseDown) {
         GUIUtility.hotControl = id;
         Undo.RegisterCompleteObjectUndo(stageCreator, "Stage Creator Change");
         Undo.FlushUndoRecordObjects();
         e.Use();
     }
     if (e.type == EventType.MouseDrag && GUIUtility.hotControl == id) {
         switch (e.button) {
             case 0:
                 stageCreator.CreateTile(cursorPosition);
                 e.Use();
                 break;
             case 1:
                 stageCreator.DestroyTile(cursorPosition);
                 e.Use();
                 break;
         }
     }
     if (e.type == EventType.MouseUp && GUIUtility.hotControl == id) {
         GUIUtility.hotControl = 0;
         e.Use();
     }
 }

StageCreator:

 public List<Vector2Int> _tiles = new List<Vector2Int>();
 
 //Create a tile
 public void CreateTile(Vector2Int position) {
     if (!_tiles.Contains(position) && CursorIsInBounds() && _selectedTile != null) {
         _tiles.Add(position);
     }
 }
 
 //Remove a tile
 public void DestroyTile(Vector2Int position) {
     if (_tiles.Contains(position) && CursorIsInBounds()) {
         _tiles.Remove(position);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

78 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

Related Questions

Undo has an interval within which it won't be registered 0 Answers

How to deal with object deletion Undo states and variables referencing the objects involved? 2 Answers

Why does UWP Package size getting so big? 1 Answer

Why Unity says my GPU is DX10 when it is DX12? 0 Answers

【LuminSDK Unity】Deploy to Magic Leap One and MLDB error: API level requested does not support 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