// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using UnityEditor;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UI;
namespace Microsoft.MixedReality.GraphicsTools.Editor
{
///
/// General utility methods to help with inspector development.
///
public static class InspectorUtilities
{
///
/// Line 19 - 109 uses an implementation from MRTK.
/// https://github.com/Zee2/MixedRealityToolkit-Unity/blob/mrtk3/com.microsoft.mrtk.uxcomponents/Editor/CreateElementMenus.cs#L39
/// It automatically adds a configured canvas when a Graphics Tools
/// UI GameObject is added to the scene.
///
// Reflection into internal UGUI editor utilities.
private static System.Reflection.MethodInfo PlaceUIElementRoot = null;
///
/// Add canvas for a UI GameObject if none exists.
///
private static GameObject SetupCanvas(GameObject gameObject, MenuCommand menuCommand)
{
// This is evil :)
// UGUI contains plenty of helper utilities for spawning and managing new Canvas objects
// at edit-time. Unfortunately, they're all internal, so we have to use reflection to
// access them.
if (PlaceUIElementRoot == null)
{
// We're using SelectableEditor type here to grab the assembly instead of going
// and hunting down the assembly ourselves. It's a bit more convenient and durable.
PlaceUIElementRoot = typeof(SelectableEditor).Assembly.GetType("UnityEditor.UI.MenuOptions")?.GetMethod(
"PlaceUIElementRoot",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Static );
if (PlaceUIElementRoot == null)
{
Debug.LogError("Whoops! Looks like Unity changed the internals of their UGUI editor utilities. Please file a bug!");
// Return early; we can't do anything else.
return gameObject;
}
}
PlaceUIElementRoot.Invoke(null, new object[] { gameObject, menuCommand});
// The above call will create a new Canvas for us (if we don't have one),
// but it won't have optimal settings for MRTK UX. Let's fix that!
Canvas canvas = gameObject.GetComponentInParent