Files
JJBB/Assets/Editor/Scripts/UI/CreateZhanyouUI.cs

245 lines
7.2 KiB
C#
Raw Permalink Normal View History

2024-08-23 15:49:34 +08:00

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
public class CreateZhanyouUI : Editor {
[MenuItem("GameObject/UI/Zhanyou/Text")]
public static void CreateText()
{
var textObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\Text.prefab");
InstantiateAsChild(textObj);
}
[MenuItem("GameObject/UI/Zhanyou/Button")]
public static void CreateButton()
{
var textObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\Button.prefab");
InstantiateAsChild(textObj);
}
[MenuItem("GameObject/UI/Zhanyou/Toggle")]
public static void CreateToggle()
{
var textObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\Toggle.prefab");
InstantiateAsChild(textObj);
}
[MenuItem("GameObject/UI/Zhanyou/ProcessBar")]
public static void CreateProcessBar()
{
var uiObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\Processbar.prefab");
InstantiateAsChild(uiObj);
}
[MenuItem("GameObject/UI/Zhanyou/TagPanel")]
public static void CreateTagPanel()
{
var uiObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\UITagPanel.prefab");
InstantiateAsChild(uiObj);
}
[MenuItem("GameObject/UI/Zhanyou/ContainerScoll")]
public static void CreateContainerScoll()
{
var uiObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\UIContainerScroll.prefab");
InstantiateAsChild(uiObj);
}
[MenuItem("GameObject/UI/Zhanyou/ImgText")]
public static void CreateImgText()
{
var uiObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\UIImgText.prefab");
InstantiateAsChild(uiObj);
}
private static void InstantiateAsChild(GameObject prefab)
{
GameObject itemGO = GameObject.Instantiate(prefab);
itemGO.name = itemGO.name.Replace("(Clone)", "");
Object[] selection = Selection.GetFiltered(typeof(GameObject), SelectionMode.ExcludePrefab);
if (selection.Length > 0 && selection[0] is GameObject)
{
var parentGO = selection[0] as GameObject;
itemGO.transform.parent = parentGO.transform;
itemGO.transform.localScale = new Vector3(1,1,1);
itemGO.transform.localPosition = Vector3.zero;
}
}
#region
[MenuItem("GameObject/UI/Font/Mode1")]
public static void ChangeFontMode1()
{
var text = GetPrefabText();
UseFontMode(text, 0);
}
[MenuItem("GameObject/UI/Font/Mode2")]
public static void ChangeFontMode2()
{
var text = GetPrefabText();
UseFontMode(text, 1);
}
[MenuItem("GameObject/UI/Font/Mode3")]
public static void ChangeFontMode3()
{
var text = GetPrefabText();
UseFontMode(text, 2);
}
[MenuItem("GameObject/UI/Font/Mode4")]
public static void ChangeFontMode4()
{
var text = GetPrefabText();
UseFontMode(text, 3);
}
[MenuItem("GameObject/UI/Font/Mode5")]
public static void ChangeFontMode5()
{
var text = GetPrefabText();
UseFontMode(text, 4);
}
[MenuItem("GameObject/UI/Font/Mode6")]
public static void ChangeFontMode6()
{
var text = GetPrefabText();
UseFontMode(text, 5);
}
[MenuItem("GameObject/UI/Font/Mode8")]
public static void ChangeFontMode8()
{
var text = GetPrefabText();
UseFontMode(text, 6);
}
[MenuItem("GameObject/UI/Font/Mode9")]
public static void ChangeFontMode9()
{
var text = GetPrefabText();
UseFontMode(text, 7);
}
[MenuItem("GameObject/UI/Font/ChildMode1")]
public static void ChangeFontChildMode1()
{
Object[] selection = Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel);
if (selection.Length > 0 && selection[0] is GameObject)
{
var selectionPrefab = PrefabUtility.GetPrefabParent((selection[0] as GameObject));
var textObjs = (selectionPrefab as GameObject).GetComponentsInChildren<Text>();
foreach (var text in textObjs)
{
UseFontMode(text, 0);
}
}
}
[MenuItem("GameObject/UI/Font/Prefab")]
public static void AutoEmoji()
{
GameObject uiPrefab = null;
Object[] selection = Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel);
if (selection.Length > 0 && selection[0] is GameObject)
{
var selectionPrefab = PrefabUtility.GetPrefabParent((selection[0] as GameObject));
uiPrefab = (selectionPrefab as GameObject);
}
if (uiPrefab == null)
return;
var textObjs = uiPrefab.GetComponentsInChildren<Text>(true);
foreach (var textObj in textObjs)
{
int textMode = 0;
var imgGroup = (textObj).GetComponentsInParent<Image>(true);
foreach (var img in imgGroup)
{
if (img == null)
continue;
if (img.sprite == null)
continue;
if (img.sprite.name.Contains("按钮黄") || img.sprite.name.Contains("按钮蓝"))
{
Debug.Log("Btn text:" + textObj.name);
textMode = 1;
UseFontMode(textObj, textMode);
break;
}
}
Debug.Log("text mode:" + textMode);
//UseFontMode(textObj, textMode);
}
}
private static Text GetPrefabText()
{
Object[] selection = Selection.GetFiltered(typeof(GameObject), SelectionMode.TopLevel);
if (selection.Length > 0 && selection[0] is GameObject)
{
var selectionPrefab = PrefabUtility.GetPrefabParent((selection[0] as GameObject));
Debug.Log("Prefab:" + selectionPrefab.name);
var text = (selectionPrefab as GameObject).GetComponent<Text>();
return text;
}
return null;
}
private static UIFontMode _PrefabFontMode;
private static void UseFontMode(Text text, int mode)
{
if (_PrefabFontMode == null)
{
var uiObj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets\\Project3D\\Tool\\Editor\\UI\\FontMode.prefab");
_PrefabFontMode = uiObj.GetComponent<UIFontMode>();
}
var fontModeMark = text.gameObject.GetComponent<UIFontMark>();
if(fontModeMark == null)
fontModeMark = text.gameObject.AddComponent<UIFontMark>();
fontModeMark.FontModeMark = mode;
text.font = _PrefabFontMode._FontModeInfo[mode]._Font;
text.color = _PrefabFontMode._FontModeInfo[mode]._FontColor;
text.fontSize = _PrefabFontMode._FontModeInfo[mode]._FontSize;
var outline = text.gameObject.GetComponent<Outline>();
if (outline != null)
{
GameObject.DestroyImmediate(outline, true);
}
}
#endregion
}