using UnityEngine;
using UnityEditor;
using UnityEditor.Presets;
using UnityEditor.SceneManagement;
using UnityEditor.Experimental.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace TMPro.EditorUtilities
{
public static class TMPro_CreateObjectMenu
{
///
/// Create a TextMeshPro object that works with the Mesh Renderer
///
///
[MenuItem("GameObject/3D Object/Text - TextMeshPro", false, 30)]
static void CreateTextMeshProObjectPerform(MenuCommand command)
{
GameObject go = ObjectFactory.CreateGameObject("Text (TMP)");
// Add support for new prefab mode
StageUtility.PlaceGameObjectInCurrentStage(go);
TextMeshPro textComponent = ObjectFactory.AddComponent(go);
if (textComponent.m_isWaitingOnResourceLoad == false)
{
// Get reference to potential Presets for component
#if UNITY_2019_3_OR_NEWER
Preset[] presets = Preset.GetDefaultPresetsForObject(textComponent);
if (presets == null || presets.Length == 0)
{
textComponent.text = "Sample text";
textComponent.alignment = TextAlignmentOptions.TopLeft;
}
else
{
textComponent.renderer.sortingLayerID = textComponent._SortingLayerID;
textComponent.renderer.sortingOrder = textComponent._SortingOrder;
}
#else
if (Preset.GetDefaultForObject(textComponent) == null)
{
textComponent.text = "Sample text";
textComponent.alignment = TextAlignmentOptions.TopLeft;
}
else
{
textComponent.renderer.sortingLayerID = textComponent._SortingLayerID;
textComponent.renderer.sortingOrder = textComponent._SortingOrder;
}
#endif
if (TMP_Settings.autoSizeTextContainer)
{
Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
textComponent.rectTransform.sizeDelta = size;
}
else
{
textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProTextContainerSize;
}
}
else
{
textComponent.text = "Sample text";
textComponent.alignment = TextAlignmentOptions.TopLeft;
}
Undo.RegisterCreatedObjectUndo(go, "Create " + go.name);
GameObject contextObject = command.context as GameObject;
if (contextObject != null)
{
GameObjectUtility.SetParentAndAlign(go, contextObject);
Undo.SetTransformParent(go.transform, contextObject.transform, "Parent " + go.name);
}
Selection.activeGameObject = go;
}
///
/// Create a TextMeshPro object that works with the CanvasRenderer
///
///
[MenuItem("GameObject/UI/Text - TextMeshPro", false, 2001)]
static void CreateTextMeshProGuiObjectPerform(MenuCommand menuCommand)
{
GameObject go = TMP_DefaultControls.CreateText(GetStandardResources());
// Override text color and font size
TextMeshProUGUI textComponent = go.GetComponent();
if (textComponent.m_isWaitingOnResourceLoad == false)
{
// Get reference to potential Presets for component
#if UNITY_2019_3_OR_NEWER
Preset[] presets = Preset.GetDefaultPresetsForObject(textComponent);
if (presets == null || presets.Length == 0)
{
textComponent.fontSize = TMP_Settings.defaultFontSize;
textComponent.color = Color.white;
textComponent.text = "New Text";
}
#else
if (Preset.GetDefaultForObject(textComponent) == null)
{
textComponent.fontSize = TMP_Settings.defaultFontSize;
textComponent.color = Color.white;
textComponent.text = "New Text";
}
#endif
if (TMP_Settings.autoSizeTextContainer)
{
Vector2 size = textComponent.GetPreferredValues(TMP_Math.FLOAT_MAX, TMP_Math.FLOAT_MAX);
textComponent.rectTransform.sizeDelta = size;
}
else
{
textComponent.rectTransform.sizeDelta = TMP_Settings.defaultTextMeshProUITextContainerSize;
}
}
else
{
textComponent.fontSize = 36;
textComponent.color = Color.white;
textComponent.text = "New Text";
}
PlaceUIElementRoot(go, menuCommand);
}
[MenuItem("GameObject/UI/Button - TextMeshPro", false, 2031)]
public static void AddButton(MenuCommand menuCommand)
{
GameObject go = TMP_DefaultControls.CreateButton(GetStandardResources());
// Override font size
TMP_Text textComponent = go.GetComponentInChildren();
textComponent.fontSize = 24;
PlaceUIElementRoot(go, menuCommand);
}
[MenuItem("GameObject/UI/Input Field - TextMeshPro", false, 2037)]
static void AddTextMeshProInputField(MenuCommand menuCommand)
{
GameObject go = TMP_DefaultControls.CreateInputField(GetStandardResources());
PlaceUIElementRoot(go, menuCommand);
}
[MenuItem("GameObject/UI/Dropdown - TextMeshPro", false, 2036)]
public static void AddDropdown(MenuCommand menuCommand)
{
GameObject go = TMP_DefaultControls.CreateDropdown(GetStandardResources());
PlaceUIElementRoot(go, menuCommand);
}
private const string kUILayerName = "UI";
private const string kStandardSpritePath = "UI/Skin/UISprite.psd";
private const string kBackgroundSpritePath = "UI/Skin/Background.psd";
private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd";
private const string kKnobPath = "UI/Skin/Knob.psd";
private const string kCheckmarkPath = "UI/Skin/Checkmark.psd";
private const string kDropdownArrowPath = "UI/Skin/DropdownArrow.psd";
private const string kMaskPath = "UI/Skin/UIMask.psd";
private static TMP_DefaultControls.Resources s_StandardResources;
private static TMP_DefaultControls.Resources GetStandardResources()
{
if (s_StandardResources.standard == null)
{
s_StandardResources.standard = AssetDatabase.GetBuiltinExtraResource(kStandardSpritePath);
s_StandardResources.background = AssetDatabase.GetBuiltinExtraResource(kBackgroundSpritePath);
s_StandardResources.inputField = AssetDatabase.GetBuiltinExtraResource(kInputFieldBackgroundPath);
s_StandardResources.knob = AssetDatabase.GetBuiltinExtraResource(kKnobPath);
s_StandardResources.checkmark = AssetDatabase.GetBuiltinExtraResource(kCheckmarkPath);
s_StandardResources.dropdown = AssetDatabase.GetBuiltinExtraResource(kDropdownArrowPath);
s_StandardResources.mask = AssetDatabase.GetBuiltinExtraResource(kMaskPath);
}
return s_StandardResources;
}
private static void SetPositionVisibleinSceneView(RectTransform canvasRTransform, RectTransform itemTransform)
{
// Find the best scene view
SceneView sceneView = SceneView.lastActiveSceneView;
if (sceneView == null && SceneView.sceneViews.Count > 0)
sceneView = SceneView.sceneViews[0] as SceneView;
// Couldn't find a SceneView. Don't set position.
if (sceneView == null || sceneView.camera == null)
return;
// Create world space Plane from canvas position.
Camera camera = sceneView.camera;
Vector3 position = Vector3.zero;
Vector2 localPlanePosition;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRTransform, new Vector2(camera.pixelWidth / 2, camera.pixelHeight / 2), camera, out localPlanePosition))
{
// Adjust for canvas pivot
localPlanePosition.x = localPlanePosition.x + canvasRTransform.sizeDelta.x * canvasRTransform.pivot.x;
localPlanePosition.y = localPlanePosition.y + canvasRTransform.sizeDelta.y * canvasRTransform.pivot.y;
localPlanePosition.x = Mathf.Clamp(localPlanePosition.x, 0, canvasRTransform.sizeDelta.x);
localPlanePosition.y = Mathf.Clamp(localPlanePosition.y, 0, canvasRTransform.sizeDelta.y);
// Adjust for anchoring
position.x = localPlanePosition.x - canvasRTransform.sizeDelta.x * itemTransform.anchorMin.x;
position.y = localPlanePosition.y - canvasRTransform.sizeDelta.y * itemTransform.anchorMin.y;
Vector3 minLocalPosition;
minLocalPosition.x = canvasRTransform.sizeDelta.x * (0 - canvasRTransform.pivot.x) + itemTransform.sizeDelta.x * itemTransform.pivot.x;
minLocalPosition.y = canvasRTransform.sizeDelta.y * (0 - canvasRTransform.pivot.y) + itemTransform.sizeDelta.y * itemTransform.pivot.y;
Vector3 maxLocalPosition;
maxLocalPosition.x = canvasRTransform.sizeDelta.x * (1 - canvasRTransform.pivot.x) - itemTransform.sizeDelta.x * itemTransform.pivot.x;
maxLocalPosition.y = canvasRTransform.sizeDelta.y * (1 - canvasRTransform.pivot.y) - itemTransform.sizeDelta.y * itemTransform.pivot.y;
position.x = Mathf.Clamp(position.x, minLocalPosition.x, maxLocalPosition.x);
position.y = Mathf.Clamp(position.y, minLocalPosition.y, maxLocalPosition.y);
}
itemTransform.anchoredPosition = position;
itemTransform.localRotation = Quaternion.identity;
itemTransform.localScale = Vector3.one;
}
private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand)
{
GameObject parent = menuCommand.context as GameObject;
bool explicitParentChoice = true;
if (parent == null)
{
parent = GetOrCreateCanvasGameObject();
explicitParentChoice = false;
// If in Prefab Mode, Canvas has to be part of Prefab contents,
// otherwise use Prefab root instead.
PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage();
if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent))
parent = prefabStage.prefabContentsRoot;
}
if (parent.GetComponentInParent