About createdelegate in b4?

Hello, the following code below is working fine 2021.1 branch while on 2021.2b4 it failed at getRecordsInternalDelegate line 18:

        public static void EnableUndoPro()
        {
            enabled = true;

            // Assure it is subscribed to all necessary events for undo/redo recognition
            Undo.undoRedoPerformed -= UndoRedoPerformed;
            Undo.undoRedoPerformed += UndoRedoPerformed;
            EditorApplication.update -= Update;
            EditorApplication.update += Update;
            EditorApplication.playModeStateChanged -= PlaymodeStateChange;
            EditorApplication.playModeStateChanged += PlaymodeStateChange;

            // Fetch Reflection members for Undo interaction
            Assembly UnityEditorAsssembly = Assembly.GetAssembly(typeof(UnityEditor.Editor));
            Type undoType = UnityEditorAsssembly.GetType("UnityEditor.Undo");
            MethodInfo getRecordsInternal = undoType.GetMethod("GetRecordsInternal", BindingFlags.NonPublic | BindingFlags.Static);
            getRecordsInternalDelegate = (Action<object, object>)Delegate.CreateDelegate(typeof(Action<object, object>), getRecordsInternal);

            // Create dummy object
            if (dummyObject == null)
                dummyObject = new Texture2D(8, 8);

            // Setup default undo state and record
            AssureRecords();
        }

Anyone can point me at right direction? is it a bug or plain new C# standard?

The full asset located in here.

It’s using private Unity APIs using reflection. Those APIs can change at any point and break code that’s relying on them. The asset will have to be updated/adapted to work with the latest beta.

2 Likes

Hey there,

It appears that Unity has renamed their internal method ‘GetRecordsInternal’ to ‘GetTimelineRecordsInternal’. Upon changing this, I’ve found those errors are non existent and UndoPro is able to obtain the correct records.

However, it has caused a new issue to un-earth itself. If you perform an Undo operation using Undo Pro, it now causes this error to occur:
7378328--900071--upload_2021-8-1_11-7-49.png

I’m still looking into why this is happening but it’s only a partial fix for now.

Okay great news!

This bug ended up being divided into two totally unrelated issues.
1). Unity has moved their internal method ‘GetRecordsInternal’ to ‘GetTimelineRecordsInternal’.
To fix this, I simply put a version-specific wrapper around the effected area like so:

I ended up making a Pull Request to the repo for the developer so you can find the full code there.

2). The ‘CheckConsistency: Restored Transform child parent pointer from NULL’ error.
After a long time of diangosing, I finally figured out what was causing this issue. It appears to only happen if users call ‘Undo.RegisterCreatedObjectUndo’ in any events other than EditorApplication calls. This is because Unity has restricted users to only call it during EditorApplication methods.

2 Likes