123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- using UnityEngine;
- using UnityEngine.UI;
- using TMPro;
- using System.Collections.Generic;
- public class LogoPlaneCreator : MonoBehaviour
- {
- [Header("目标对象设置")]
- [SerializeField] private GameObject targetObject; // 要添加Plane的目标对象
- [SerializeField] private string targetObjectName = "Object001"; // 如果targetObject为空,则通过名称查找
-
- [Header("Plane设置")]
- [SerializeField] private Vector3 planePosition = Vector3.zero; // 相对于目标对象的位置
- [SerializeField] private Vector3 planeRotation = Vector3.zero; // 旋转角度
- [SerializeField] private Vector3 planeScale = Vector3.one; // 缩放
- [SerializeField] private Material planeMaterial; // Plane的材质
-
- [Header("Logo文字设置")]
- [SerializeField] private string logoText = "LOGO"; // Logo文字
- [SerializeField] private Font logoFont; // 字体
- [SerializeField] private int fontSize = 48; // 字体大小
- [SerializeField] private Color textColor = Color.white; // 文字颜色
- [SerializeField] private TextAlignmentOptions textAlignment = TextAlignmentOptions.Center; // 文字对齐
- [SerializeField] private Vector3 textOffset = Vector3.zero; // 文字位置偏移
-
- [Header("高级设置")]
- [SerializeField] private bool useTextMeshPro = true; // 是否使用TextMeshPro
- [SerializeField] private bool createAsChild = true; // 是否作为子对象创建
- [SerializeField] private bool addCollider = false; // 是否添加碰撞体
- [SerializeField] private bool addRigidbody = false; // 是否添加刚体
-
- [Header("文字质量设置")]
- [SerializeField] private bool useHighQuality = true; // 使用高质量渲染
- [SerializeField] private float textScale = 2.0f; // 文字缩放倍数(提高分辨率)
- [SerializeField] private bool enableAntiAliasing = true; // 启用抗锯齿
- [SerializeField] private int canvasWidth = 1024; // Canvas宽度
- [SerializeField] private int canvasHeight = 512; // Canvas高度
- [SerializeField] private bool usePixelPerfect = true; // 像素完美模式
- [SerializeField] private FilterMode textureFilterMode = FilterMode.Bilinear; // 纹理过滤模式
-
- private GameObject createdPlane;
- private GameObject createdText;
- private Canvas createdCanvas;
-
- [ContextMenu("创建Logo Plane")]
- public void CreateLogoPlane()
- {
- // 查找目标对象
- GameObject target = targetObject;
- if (target == null)
- {
- target = GameObject.Find(targetObjectName);
- if (target == null)
- {
- Debug.LogError($"未找到名为 {targetObjectName} 的对象");
- return;
- }
- }
-
- // 创建Plane
- CreatePlane(target);
-
- // 创建文字Logo
- CreateTextLogo();
-
- Debug.Log($"已在 {target.name} 上成功创建Logo Plane");
- }
-
- private void CreatePlane(GameObject parent)
- {
- // 创建Plane GameObject
- createdPlane = GameObject.CreatePrimitive(PrimitiveType.Plane);
- createdPlane.name = "LogoPlane";
-
- // 设置Transform
- Transform planeTransform = createdPlane.transform;
- if (createAsChild)
- {
- planeTransform.SetParent(parent.transform);
- planeTransform.localPosition = planePosition;
- planeTransform.localRotation = Quaternion.Euler(planeRotation);
- planeTransform.localScale = planeScale;
- }
- else
- {
- planeTransform.position = parent.transform.position + planePosition;
- planeTransform.rotation = parent.transform.rotation * Quaternion.Euler(planeRotation);
- planeTransform.localScale = planeScale;
- }
-
- // 设置材质
- if (planeMaterial != null)
- {
- MeshRenderer renderer = createdPlane.GetComponent<MeshRenderer>();
- renderer.material = planeMaterial;
- }
-
- // 添加碰撞体(可选)
- if (!addCollider)
- {
- Collider collider = createdPlane.GetComponent<Collider>();
- if (collider != null)
- DestroyImmediate(collider);
- }
-
- // 添加刚体(可选)
- if (addRigidbody)
- {
- createdPlane.AddComponent<Rigidbody>();
- }
- }
-
- private void CreateTextLogo()
- {
- if (string.IsNullOrEmpty(logoText))
- {
- Debug.LogWarning("Logo文字为空,跳过文字创建");
- return;
- }
-
- if (useTextMeshPro)
- {
- CreateTextMeshPro();
- }
- else
- {
- CreateLegacyText();
- }
- }
-
- private void CreateTextMeshPro()
- {
- // 创建TextMeshPro对象
- createdText = new GameObject("LogoText_TMP");
- createdText.transform.SetParent(createdPlane.transform);
- createdText.transform.localPosition = textOffset;
- createdText.transform.localRotation = Quaternion.identity;
-
- // 添加TextMeshPro组件
- TextMeshPro tmp = createdText.AddComponent<TextMeshPro>();
- tmp.text = logoText;
- tmp.fontSize = fontSize * (useHighQuality ? textScale : 1.0f);
- tmp.color = textColor;
- tmp.alignment = textAlignment;
-
- // 高质量设置
- if (useHighQuality)
- {
- // 设置高质量渲染
- tmp.enableAutoSizing = false;
- tmp.fontStyle = FontStyles.Normal;
- tmp.enableWordWrapping = false;
- tmp.richText = false;
-
- // 设置材质质量
- if (tmp.material != null)
- {
- tmp.material.SetFloat("_UnderlayDilate", 0.0f);
- tmp.material.SetFloat("_UnderlaySoftness", 0.0f);
- tmp.material.SetFloat("_WeightNormal", 0.5f);
- tmp.material.SetFloat("_WeightBold", 0.75f);
- }
- }
-
- // 设置字体
- if (logoFont != null)
- {
- // 注意:TextMeshPro使用TMP_FontAsset,不是普通Font
- // 这里需要手动设置TMP_FontAsset
- Debug.Log("请手动设置TextMeshPro的字体资源");
- }
-
- // 调整位置,让文字面向摄像机
- createdText.transform.LookAt(Camera.main.transform);
- createdText.transform.Rotate(0, 180, 0);
-
- // 应用缩放
- if (useHighQuality && textScale != 1.0f)
- {
- createdText.transform.localScale = Vector3.one / textScale;
- }
- }
-
- private void CreateLegacyText()
- {
- // 创建Canvas
- GameObject canvasObj = new GameObject("LogoCanvas");
- canvasObj.transform.SetParent(createdPlane.transform);
- canvasObj.transform.localPosition = textOffset;
- canvasObj.transform.localRotation = Quaternion.identity;
-
- Canvas canvas = canvasObj.AddComponent<Canvas>();
- canvas.renderMode = RenderMode.WorldSpace;
- canvas.worldCamera = Camera.main;
- createdCanvas = canvas;
-
- // 高质量Canvas设置
- if (useHighQuality)
- {
- // 设置Canvas尺寸
- RectTransform canvasRect = canvasObj.GetComponent<RectTransform>();
- canvasRect.sizeDelta = new Vector2(canvasWidth, canvasHeight);
-
- // 添加CanvasScaler
- CanvasScaler scaler = canvasObj.AddComponent<CanvasScaler>();
- scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
- scaler.referenceResolution = new Vector2(canvasWidth, canvasHeight);
- scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
- scaler.matchWidthOrHeight = 0.5f;
-
- // 添加GraphicRaycaster(如果需要交互)
- canvasObj.AddComponent<GraphicRaycaster>();
- }
- else
- {
- // 标准设置
- CanvasScaler scaler = canvasObj.AddComponent<CanvasScaler>();
- scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
- scaler.referenceResolution = new Vector2(1920, 1080);
- }
-
- // 创建Text对象
- createdText = new GameObject("LogoText");
- createdText.transform.SetParent(canvasObj.transform);
- createdText.transform.localPosition = Vector3.zero;
- createdText.transform.localRotation = Quaternion.identity;
- createdText.transform.localScale = Vector3.one;
-
- // 添加Text组件
- Text text = createdText.AddComponent<Text>();
- text.text = logoText;
- text.font = logoFont;
- text.fontSize = (int)(fontSize * (useHighQuality ? textScale : 1.0f));
- text.color = textColor;
- text.alignment = TextAnchor.MiddleCenter;
-
- // 高质量文字设置
- if (useHighQuality)
- {
- text.horizontalOverflow = HorizontalWrapMode.Overflow;
- text.verticalOverflow = VerticalWrapMode.Overflow;
- text.resizeTextForBestFit = false;
- text.supportRichText = false;
-
- // 设置字体样式
- if (logoFont != null)
- {
- text.font = logoFont;
- text.fontStyle = FontStyle.Normal;
- }
- }
-
- // 设置RectTransform
- RectTransform rectTransform = createdText.GetComponent<RectTransform>();
- if (useHighQuality)
- {
- rectTransform.sizeDelta = new Vector2(canvasWidth * 0.8f, canvasHeight * 0.8f);
- }
- else
- {
- rectTransform.sizeDelta = new Vector2(400, 100);
- }
- rectTransform.anchoredPosition = Vector2.zero;
-
- // 应用缩放
- if (useHighQuality && textScale != 1.0f)
- {
- createdText.transform.localScale = Vector3.one / textScale;
- }
-
- // 创建高质量材质(可选)
- if (useHighQuality && enableAntiAliasing)
- {
- CreateHighQualityMaterial(text);
- }
- }
-
- private void CreateHighQualityMaterial(Text text)
- {
- // 创建高质量材质
- Material highQualityMaterial = new Material(Shader.Find("UI/Default"));
-
- // 设置材质属性
- highQualityMaterial.SetFloat("_SrcBlend", (float)UnityEngine.Rendering.BlendMode.SrcAlpha);
- highQualityMaterial.SetFloat("_DstBlend", (float)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
- highQualityMaterial.SetFloat("_ZWrite", 0);
- highQualityMaterial.DisableKeyword("_ALPHATEST_ON");
- highQualityMaterial.EnableKeyword("_ALPHABLEND_ON");
- highQualityMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
- highQualityMaterial.renderQueue = 3000;
-
- // 应用材质
- text.material = highQualityMaterial;
- }
-
- [ContextMenu("删除Logo Plane")]
- public void DeleteLogoPlane()
- {
- if (createdPlane != null)
- {
- DestroyImmediate(createdPlane);
- createdPlane = null;
- createdText = null;
- createdCanvas = null;
- Debug.Log("已删除Logo Plane");
- }
- }
-
- [ContextMenu("更新Logo文字")]
- public void UpdateLogoText()
- {
- if (createdText != null)
- {
- if (useTextMeshPro)
- {
- TextMeshPro tmp = createdText.GetComponent<TextMeshPro>();
- if (tmp != null)
- {
- tmp.text = logoText;
- tmp.fontSize = fontSize * (useHighQuality ? textScale : 1.0f);
- tmp.color = textColor;
- }
- }
- else
- {
- Text text = createdText.GetComponent<Text>();
- if (text != null)
- {
- text.text = logoText;
- text.fontSize = (int)(fontSize * (useHighQuality ? textScale : 1.0f));
- text.color = textColor;
- }
- }
- Debug.Log("已更新Logo文字");
- }
- }
-
- [ContextMenu("查找目标对象")]
- public void FindTargetObject()
- {
- GameObject found = GameObject.Find(targetObjectName);
- if (found != null)
- {
- targetObject = found;
- Debug.Log($"已找到目标对象: {found.name}");
- }
- else
- {
- Debug.LogWarning($"未找到名为 {targetObjectName} 的对象");
- }
- }
-
- [ContextMenu("优化文字质量")]
- public void OptimizeTextQuality()
- {
- if (createdText != null)
- {
- if (useTextMeshPro)
- {
- TextMeshPro tmp = createdText.GetComponent<TextMeshPro>();
- if (tmp != null)
- {
- // 重新应用高质量设置
- tmp.fontSize = fontSize * textScale;
- if (tmp.material != null)
- {
- tmp.material.SetFloat("_UnderlayDilate", 0.0f);
- tmp.material.SetFloat("_UnderlaySoftness", 0.0f);
- }
- }
- }
- else
- {
- Text text = createdText.GetComponent<Text>();
- if (text != null)
- {
- text.fontSize = (int)(fontSize * textScale);
- text.horizontalOverflow = HorizontalWrapMode.Overflow;
- text.verticalOverflow = VerticalWrapMode.Overflow;
- }
- }
- Debug.Log("已优化文字质量");
- }
- }
-
- // 在Inspector中实时预览
- private void OnValidate()
- {
- if (Application.isPlaying && createdText != null)
- {
- UpdateLogoText();
- }
- }
- }
|