using UnityEngine; using UnityEditor; using UnityEngine.UI; public class UGUIShortcutKey : Editor { public const int UIlayer = 5; [MenuItem("Component/UI/Shortcut/Create Text #&L")] public static void CreateText() { if (Selection.gameObjects.Length == 1) { GameObject obj = Selection.gameObjects[0]; GameObject text = new GameObject(); RectTransform textRect = text.AddComponent<RectTransform>(); Text textTx = text.AddComponent<Text>(); text.transform.SetParent(obj.transform); text.name = "Text"; text.layer = UIlayer; //設置字體 textTx.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/GameMain/Localization/ChineseSimplified/Fonts/MainFont.ttf"); textTx.text = "plateface"; textTx.alignment = TextAnchor.MiddleCenter; textRect.localScale = Vector3.one; textRect.anchoredPosition = Vector2.zero; textRect.anchoredPosition3D = Vector3.zero; textRect.sizeDelta = new Vector2(textTx.preferredWidth, textTx.preferredHeight); RectTransformZero(textRect); } } [MenuItem("Plateface/CreateUGUI Button #&B")] public static void CreateButton() { if (Selection.gameObjects.Length == 1) { GameObject obj = Selection.gameObjects[0]; if (obj == null) return; GameObject button = new GameObject(); GameObject buttonTx = new GameObject(); RectTransform buttonRect = button.AddComponent<RectTransform>(); RectTransform buttonTxRect = buttonTx.AddComponent<RectTransform>(); button.AddComponent<Image>(); Text texBtn = buttonTx.AddComponent<Text>(); texBtn.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/GameMain/Localization/ChineseSimplified/Fonts/MainFont.ttf"); texBtn.text = "Button"; buttonTxRect.sizeDelta = new Vector2(texBtn.preferredWidth, texBtn.preferredHeight); button.transform.SetParent(obj.transform); buttonTx.transform.SetParent(button.transform); button.name = "Button"; buttonTx.name = "Text"; button.layer = UIlayer; buttonTx.layer = UIlayer; RectTransformZero(buttonRect); RectTransformZero(buttonTxRect); } } [MenuItem("Plateface/CreateUGUI Image #&S")] public static void CreateImage() { if (Selection.gameObjects.Length == 1) { GameObject obj = Selection.gameObjects[0]; //RectTransform selectionObjRect = Selection.gameObjects[0].GetComponent<RectTransform>(); GameObject image = new GameObject(); RectTransform imageRect = image.AddComponent<RectTransform>(); image.AddComponent<Image>(); image.transform.SetParent(obj.transform); image.name = "Image"; image.layer = 5; RectTransformZero(imageRect); } } [MenuItem("Plateface/CreateUGUI InputField #&I")] public static void CreateInputField() { if (Selection.gameObjects.Length == 1) { GameObject obj = Selection.gameObjects[0]; GameObject inputField = new GameObject(); RectTransform rectTransform = inputField.AddComponent<RectTransform>(); Image image = inputField.AddComponent<Image>(); //image.sprite = Resources.Load<Sprite>("UnityPlugins/UGUIShortcutKeyTexture/background1"); inputField.AddComponent<InputField>(); rectTransform.localScale = new Vector3(1, 1, 1); inputField.layer = UIlayer; inputField.transform.SetParent(obj.transform); inputField.name = "InputField"; GameObject placeholder = new GameObject(); Text placeholderTx = placeholder.AddComponent<Text>(); placeholderTx.font = AssetDatabase.LoadAssetAtPath<Font>("Assets/GameMain/Localization/ChineseSimplified/Fonts/MainFont.ttf"); placeholderTx.text = "Enter text..."; placeholder.transform.SetParent(inputField.transform); placeholder.name = "Placeholder"; placeholder.layer = UIlayer; placeholderTx.color = Color.black; GameObject text = new GameObject(); Text textTx = text.AddComponent<Text>(); text.transform.SetParent(inputField.transform); text.name = "Text"; text.layer = UIlayer; textTx.color = Color.black; RectTransformZero(rectTransform); } } public static void RectTransformZero(RectTransform rectTransform) { rectTransform.localScale = Vector3.one; rectTransform.anchoredPosition = Vector2.zero; rectTransform.anchoredPosition3D = Vector3.zero; } }