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(); renderer.material = planeMaterial; } // 添加碰撞体(可选) if (!addCollider) { Collider collider = createdPlane.GetComponent(); if (collider != null) DestroyImmediate(collider); } // 添加刚体(可选) if (addRigidbody) { createdPlane.AddComponent(); } } 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(); 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.renderMode = RenderMode.WorldSpace; canvas.worldCamera = Camera.main; createdCanvas = canvas; // 高质量Canvas设置 if (useHighQuality) { // 设置Canvas尺寸 RectTransform canvasRect = canvasObj.GetComponent(); canvasRect.sizeDelta = new Vector2(canvasWidth, canvasHeight); // 添加CanvasScaler CanvasScaler scaler = canvasObj.AddComponent(); scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; scaler.referenceResolution = new Vector2(canvasWidth, canvasHeight); scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight; scaler.matchWidthOrHeight = 0.5f; // 添加GraphicRaycaster(如果需要交互) canvasObj.AddComponent(); } else { // 标准设置 CanvasScaler scaler = canvasObj.AddComponent(); 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 = 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(); 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(); if (tmp != null) { tmp.text = logoText; tmp.fontSize = fontSize * (useHighQuality ? textScale : 1.0f); tmp.color = textColor; } } else { Text text = createdText.GetComponent(); 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(); 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(); 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(); } } }