Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Undo Code For Asset Changes

Discussion in 'Editor & General Support' started by BitGamey, Aug 23, 2020.

  1. BitGamey

    BitGamey

    Joined:
    Jun 11, 2016
    Posts:
    52
    Hi!

    After testing and searching for longer than is healthy(!) I am unable to resolve this issue.

    In Unity 2018.4, I have a custom editor script to change a sprite Texture Type from 'Default' to 'Sprite'. I have that part working but no matter what I try, I am unable to get the UNDO function to work.

    The undo option does appear correctly on the menu but neither of the commented-out 'Undo.' lines work in the code below.

    Is there a trick I am unaware of to undo changes to objects in the Assets folder instead of the Hierarchy?

    Thanks in advance,
    Lee

    Code (CSharp):
    1.  
    2. if (Selection.activeObject.GetType() != typeof(DefaultAsset))
    3. {
    4.     for (int CurrentObject = 0; CurrentObject < Selection.objects.Length; CurrentObject++)
    5.     {
    6.         //Undo.RecordObject(Selection.objects[CurrentObject], "Change Texture Type");
    7.         //Undo.RegisterCompleteObjectUndo(Selection.objects[CurrentObject], "Change Texture Type");
    8.         string path = AssetDatabase.GetAssetPath(Selection.objects[CurrentObject]);
    9.         TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
    10.          textureImporter.textureType = newType;
    11.          AssetDatabase.ImportAsset(path);
    12.     }
    13. }
    14.  
     
    Last edited: Aug 23, 2020
    RaveBox likes this.
  2. BitGamey

    BitGamey

    Joined:
    Jun 11, 2016
    Posts:
    52
    Yikes, guess I'm not the only one struggling with this.
     
  3. BitGamey

    BitGamey

    Joined:
    Jun 11, 2016
    Posts:
    52
    I guess there is no undo option in the engine, even when you change objects manually so nothing to see here...