Unity Editor does not use reflection to access editor internal methods

Q:

This question comes from the Game Framework official group (216332935) , a question from Liuyi .

Timeline refers to UnityEditor.TimeArea is an internal type

The code where Timeline is located can be accessed normally, but the editor class extended by itself cannot be accessed

This article refers to:

https://blog.csdn.net/akof1314/article/details/111773851

When using Unity Editor to make editor tools

It is often impossible to access internal classes and methods in the Editor

need to use reflection

The reflection method is cumbersome to write. If Unity upgrades the version and the internal interface changes, the reflection will fail

A:

Refer to the official code

https://github.com/Unity-Technologies/UnityCsReference/blob/master/Editor/Mono/AssemblyInfo/AssemblyInfo.cs

We can find that there are a lot of

[assembly: InternalsVisibleTo(“Unity.InternalAPIEditorBridge.00x”)]

This part is officially provided for us to extend our own editor class

practice

Create a new folder under Editor

The folder contains the following files:

TestTimeArea.csdocument

using UnityEditor;

public class TestTimeArea : EditorWindow
{
    
    
    private TimeArea m_timeArea = null;

    private void OnEnable()
    {
    
    
        if (this.m_timeArea == null)
        {
    
    
            this.m_timeArea = new TimeArea(true)
            {
    
    
                hRangeLocked = false,
                vRangeLocked = true,
                hSlider = true,
                vSlider = false,
                margin = 10f,
                scaleWithWindow = true,
                ignoreScrollWheelUntilClicked = true
            };
        }
    }
}

TestTimeArea.asmdefdocument

{
    
    
    "name": "Unity.InternalAPIEditorBridge.001",
    "references": [],
    "optionalUnityReferences": [],
    "includePlatforms": ["Editor"],
    "excludePlatforms": [],
    "allowUnsafeCode": false,
    "overrideReferences": false,
    "precompiledReferences": [],
    "autoReferenced": true,
    "defineConstraints": []
}

Now you can see that TestTimeArea.csthe file can be compiled normally, and it can also be used normallyUnityEditor.TimeArea

Guess you like

Origin blog.csdn.net/zxsean/article/details/118973229