c1_unity/Assets/Editor/HierarchyTools/GameObjectEditer.cs
2023-04-03 11:04:31 +08:00

207 lines
6.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using TMPro;
using BFEditor.Resource;
using BF;
namespace BFEditor
{
public class GameObjectEditer : Editor
{
static Material CustomUIDefault;
// 重写Text菜单默认取消raycastTarget
[MenuItem("GameObject/UI/Text", true)]
static bool ValidateUITextMenu()
{
return true;
}
[MenuItem("GameObject/UI/Text", false)]
static void CreateUIText()
{
GameObject go = new GameObject("Text", typeof(UnityEngine.UI.Text));
UnityEngine.UI.Text text = go.GetComponent<UnityEngine.UI.Text>();
text.raycastTarget = false;
text.material = GetCustomDefaultUIMat();
CheckAndSetUIParent(go.transform);
}
[MenuItem("GameObject/UI/TextMeshPro - Text", true)]
static bool ValidateUITextMeshProMenu()
{
return true;
}
[MenuItem("GameObject/UI/TextMeshPro - Text", false)]
static void CreateUITextMeshPro()
{
Debug.LogError("请在UI/BF通用控件里创建Text");
}
// 重写Image菜单默认取消raycastTarget
[MenuItem("GameObject/UI/Image", true)]
static bool ValidateUIImageMenu()
{
return true;
}
[MenuItem("GameObject/UI/Image", false)]
static void CreateUIImage()
{
GameObject go = new GameObject("Image", typeof(UnityEngine.UI.Image));
UnityEngine.UI.Image image = go.GetComponent<UnityEngine.UI.Image>();
image.raycastTarget = false;
image.material = GetCustomDefaultUIMat();
CheckAndSetUIParent(go.transform);
}
[MenuItem("GameObject/UI/BFSlider", false)]
static void CreateBFSlider()
{
GameObject go = new GameObject("BFSlider", typeof(BFSlider));
BFSlider slider = go.GetComponent<BFSlider>();
slider.raycastTarget = false;
slider.material = GetCustomDefaultUIMat();
CheckAndSetUIParent(go.transform);
}
[MenuItem("GameObject/UI/Raw Image", true)]
static bool ValidateUIRawImageMenu()
{
return true;
}
[MenuItem("GameObject/UI/Raw Image", false)]
static void CreateUIRawImage()
{
GameObject go = new GameObject("RawImage", typeof(UnityEngine.UI.RawImage));
UnityEngine.UI.RawImage image = go.GetComponent<UnityEngine.UI.RawImage>();
image.raycastTarget = false;
image.material = GetCustomDefaultUIMat();
CheckAndSetUIParent(go.transform);
}
static void CheckAndSetUIParent(Transform uiTransform)
{
if (Selection.activeTransform) // 当前有选择一个父节点
{
uiTransform.SetParent(Selection.activeTransform, false);
}
else
{
var currentPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
if (currentPrefabStage != null) //当前处于prefab编辑模式
{
var prefabRoot = currentPrefabStage.prefabContentsRoot;
uiTransform.SetParent(prefabRoot.transform, false);
}
}
Selection.activeTransform = uiTransform;
}
static Material GetCustomDefaultUIMat()
{
if (CustomUIDefault == null)
CustomUIDefault = AssetDatabase.LoadAssetAtPath<Material>(ResourceProcessConfig.CUSTOM_DEFAULT_UI_MAT_PATH);
return CustomUIDefault;
}
[MenuItem("GameObject/UI/Button", true)]
static bool ValidateUIButtonMenu()
{
return true;
}
[MenuItem("GameObject/UI/Button", false)]
static void CreateUIButton()
{
Debug.LogError("请在UI/BF通用控件里创建Button");
}
[MenuItem("GameObject/UI/Button - TextMeshPro", true)]
static bool ValidateUIButtonTMPMenu()
{
return true;
}
[MenuItem("GameObject/UI/Button - TextMeshPro", false)]
static void CreateUIButtonTMP()
{
Debug.LogError("请在UI/BF通用控件里创建Button");
}
[MenuItem("GameObject/UI/Text - TextMeshPro", true)]
static bool ValidateUITextTMPMenu()
{
return true;
}
[MenuItem("GameObject/UI/Text - TextMeshPro", false)]
static void CreateUITextTMP()
{
Debug.LogError("请在UI/BF通用控件里创建Text");
}
[MenuItem("GameObject/UI/BF通用控件/Button")]
static void CreateBFCommonButton()
{
GameObject go = new GameObject("Button", typeof(UnityEngine.UI.Image));
UnityEngine.UI.Image image = go.GetComponent<UnityEngine.UI.Image>();
image.material = GetCustomDefaultUIMat();
go.AddComponent<BF.UITouchEvent>();
CheckAndSetUIParent(go.transform);
}
[MenuItem("GameObject/UI/BF通用控件/Text")]
static void CreateBFCommonText()
{
if (TMP_Settings.defaultFontAsset == null)
{
var tmpSettings = TMP_Settings.instance;
var type = tmpSettings.GetType();
var fontAssetField = type.GetField("m_defaultFontAsset", System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
var fontAssets = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(ResourceProcessConfig.DEFAULT_TMP_FONTASSET_PATH);
fontAssetField.SetValue(tmpSettings, fontAssets);
AssetDatabase.Refresh();
}
GameObject go = new GameObject("TextMeshPro", typeof(TMPro.TextMeshProUGUI));
TMPro.TextMeshProUGUI textMeshPro = go.GetComponent<TMPro.TextMeshProUGUI>();
textMeshPro.raycastTarget = false;
var font = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(
"Assets/arts/fonts/tmpfonts/default/tmpfont/font_sdf.asset");
textMeshPro.font = font;
textMeshPro.material = GetCustomDefaultUIMat();
textMeshPro.enableWordWrapping = false;
CheckAndSetUIParent(go.transform);
if (TMP_Settings.defaultFontAsset != null)
{
var tmpSettings = TMP_Settings.instance;
var type = tmpSettings.GetType();
var fontAssetField = type.GetField("m_defaultFontAsset", System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
fontAssetField.SetValue(tmpSettings, null);
AssetDatabase.Refresh();
}
}
[MenuItem("GameObject/UI/创建序列帧")]
static void CreateFrameAnimation()
{
FrameAnimationTools.CreateFrameAnimation();
}
[MenuItem("GameObject/UI/创建序列帧(父节点)")]
static void CreateFrameAnimationInParent()
{
FrameAnimationTools.CreateFrameAnimation(true);
}
}
}