Skip to content

Unity APIs that may be useful but not mentioned in the documentation.

License

Notifications You must be signed in to change notification settings

SolarianZ/What-Unity-Does-Not-Tell-You

Repository files navigation

What-Unity-Does-Not-Tell-You

Unity APIs that may be useful but not mentioned in the documentation.

Full lists of undocumented APIs

Tip: Press Ctrl+F on the list page to search for the keyword of the content you are looking for.

Frequently Used APIs


Draw buttons on the header area of the EditorWindow:

// Unity message method
// Put in your EditorWindow class
void ShowButton(Rect position)

Sample Code

Header Button


Open asset in a custom EditorWindow when double-clicking:

[CreateAssetMenu(fileName = nameof(MyCustomAsset), menuName = "Tests/My Custom Asset")]
public class MyCustomAsset : ScriptableObject { }

public class MyCustomAssetEditorWindow : EditorWindow
{
    [OnOpenAsset]
    static bool OnOpenAsset(int instanceID, int line, int column)
    {
        if (EditorUtility.InstanceIDToObject(instanceID) is MyCustomAsset myAsset)
        {
            var window = GetWindow<MyCustomAssetEditorWindow>();
            window.SetTarget(myAsset);
            window.Focus();

            // Returning true indicates that the open operation has been processed
            // and the callback is not continued.
            return true;
        }

        // Returning false indicates that the open operation cannot be handled here,
        // and other methods in the callback should be continued.
        return false;
    }

    public void SetTarget(MyCustomAsset target)
    {
        // Do anything you want here.
        titleContent = new GUIContent(target.name);
        Debug.Log($"Open {target}.");
    }
}

Modify menu items dynamically, no need for MenuItemAttribute:

The UnityEditor.Menu class provides some methods for dynamically managing menu items, but these methods are internal and can be called through reflection.

Here is a utility class that simplifies reflection: DynamicMenuItem.cs.


Keep serialized data after changing name or namespace of type:

Similar to UnityEngine.Serialization.FormerlySerializedAsAttribute .

UnityEngine.Scripting.APIUpdating.MovedFromAttribute

Read/Write assets in Library/ProjectSettings/UserSettings folder:

UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget()

UnityEditorInternal.InternalEditorUtility.SaveToSerializedFileAndForget()

Usually, the UnityEditor.ScriptableSingleton<T> type can be used to create a singleton configuration asset stored in the Library folder, eliminating the need to use the above two methods.


Many helpful editor utility functions:

UnityEditorInternal.InternalEditorUtility

To be continued...

About

Unity APIs that may be useful but not mentioned in the documentation.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages