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 Rickenbaker · Feb 21, 2017 at 02:09 PM · custom editorundo

Unity 5.5.1 - Custom editor button Undo query

Hello all,

I am making a custom editor, and one of the functionalities will be to spawn multiple prefabs at a specific positions, as well as to undo the spawn action(s). So far I have been able to spawn a single object when the "Place object" button is clicked, but now I can't get my head around on how to Undo the spawn action with an "Undo" button click. I have browsed through Unity's scripting API, many posts on this forum, as well as other, but the further I read the more confused I get.

The script I have so far is:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 using System.Collections.Generic;
  
 [CustomEditor(typeof(ObjectSpawn))]
  
 public class ObjectSpawnEditor : Editor
 {
  
 int objectSelectionToolbar = 0;
  
 bool chooseOption = false;
 bool objectSelectionFoldout = false;
  
 public override void OnInspectorGUI()
     {
         DrawDefaultInspector();
  
         chooseOption = EditorGUILayout.Foldout(chooseOption, "Choose a part to add:");
  
         if (chooseOption)
         {
  
             //Check Station selection button
  
             EditorGUILayout.BeginVertical();
  
             objectSelectionFoldout = GUILayout.Toggle(objectSelectionFoldout, "" + (objectSelectionFoldout ? "▼ Object selection ▼" : "► Object selection ◄"), "Button", GUILayout.MaxWidth(Screen.width), GUILayout.Height(25));
  
             if (objectSelectionFoldout)
             {
  
                 GUILayout.Space(5);     //Space before a text box
  
                 GUILayout.Box("Select lenght:");
  
                 GUILayout.Space(5);     //Space after a text box and before a toolbox
  
                 string[] stationSelectionToolbarOptions = new string[] { "Object A", "Object B", "ObjectC" };
  
                 objectSelectionToolbar = GUILayout.Toolbar(objectSelectionToolbar, stationSelectionToolbarOptions, GUILayout.MinWidth(Screen.width), GUILayout.Height(50));
  
                 GUILayout.Space(5);
  
                 switch (objectSelectionToolbar)
                 {
                     case 0:
  
                         GUILayout.BeginHorizontal();
  
                         if (GUILayout.Button("Place object", GUILayout.Height(25)))
                         {
                        
                             ObjectSpawn myScript = (ObjectSpawn)target;
                             myScript.AttachObject();
                      
                         }
  
                         if (GUILayout.Button("Undo", GUILayout.Height(25)))
                         {
                        
  
                         }
                         GUILayout.EndHorizontal();
  
                         GUILayout.Space(5);
  
                         GUILayout.Box("Information:", GUILayout.MaxWidth(Screen.width), GUILayout.Height(50));
  
                         GUILayout.Space(5);
  
                         break;
                 }
  
             }
  
             EditorGUILayout.EndVertical();
          }
       }
 }

And the Monobehaviour script to attach the object, as well as to parent the spawned object to the object that has the script attached to:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class ObjectSpawn : MonoBehaviour {
  
     public void AttachObject()
     {
         GameObject Object_A = Resources.Load("Prefab") as GameObject;
         Instantiate(Object_A, this.transform.position, this.transform.rotation, this.transform);
     }
 }

In Unity it is showing as bellow:

alt text

"Start Point" is just an empty game object that has the script "ObjectSpawn" attached to.

Could anyone provide any ideas or steps for the solution? Any suggestions are welcome! Thanks

capture.jpg (21.3 kB)
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Custom Inspector Undo Functionality when directly changing SerializedProperties 1 Answer

CustomPropertyDrawer undoable properties 1 Answer

How to Undo SharedMaterial set in CustomEditor? 1 Answer

Undo.RegisterFullObjectHierarchyUndo not working 1 Answer

Custom Editor serializing data in prefab instance. 0 Answers


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