using UnityEngine; using UnityEngine.UI; using QFramework; using System.Collections.Generic; using System.Text.RegularExpressions; using TMPro; using UnityEngine.UIElements; namespace QFramework.Example { public class UITextWindowData : UIPanelData { public string text; public string audio; public string title; public List btns; public float scrollSpeed = 25; public string position; } public partial class UITextWindow : UIPanel { ResLoader loader; protected override void OnInit(IUIData uiData = null) { mData = uiData as UITextWindowData ?? new UITextWindowData(); loader = ResLoader.Allocate(); TypeEventSystem.Global.Register((arg) => Hide()).UnRegisterWhenGameObjectDestroyed(gameObject); // please add init code here } protected override void OnOpen(IUIData uiData = null) { mData = uiData as UITextWindowData ?? new UITextWindowData(); Des.text = Regex.Replace(mData.text, @"\\n", "\n"); BtnContent.RemoveAllChildren(); if (mData.btns != null) { foreach (var item in mData.btns) { GameObject btn = GameObject.Instantiate(Btn.gameObject, BtnContent); btn.transform.Find("Label").GetComponent().text = item; btn.name = item; } } if (string.IsNullOrEmpty(mData.audio) == false) { string path = Global.audioPath + mData.audio; loader = ResLoader.Allocate(); loader.Add2Load(path.ToLocalAudioResName(), (success, res) => { if (success) { AudioKit.PlayVoice(res.Asset.As(), false); } }); loader.LoadAsync(); } Title.text = mData.title; Scroll.verticalNormalizedPosition = 1; switch (mData.position) { case "left": // 将锚点设为父容器的右侧中间(Middle-Right) Content.rectTransform.anchorMin = new Vector2(0, 0.5f); // 左下锚点(右侧) Content.rectTransform.anchorMax = new Vector2(0, 0.5f); // 右上锚点(右侧) // 将位置偏移归零(相对于锚点) //Content.rectTransform.anchoredPosition = Vector2.zero; // 可选:设置轴心点为右侧中点(影响缩放/旋转中心) Content.rectTransform.pivot = new Vector2(0, 0.5f); break; case "right": default: // 将锚点设为父容器的右侧中间(Middle-Right) Content.rectTransform.anchorMin = new Vector2(1, 0.5f); // 左下锚点(右侧) Content.rectTransform.anchorMax = new Vector2(1, 0.5f); // 右上锚点(右侧) // 将位置偏移归零(相对于锚点) Content.rectTransform.anchoredPosition = new Vector2(-30, 96); // 可选:设置轴心点为右侧中点(影响缩放/旋转中心) Content.rectTransform.pivot = new Vector2(1, 0.5f); break; } } protected override void OnShow() { } protected override void OnHide() { AudioKit.StopMusic(); } protected override void OnClose() { } } }