公会系统:UIManager.cs

管理员
```csharp -- C# UIManager.cs // UI管理器,负责UI的创建、显示、隐藏等 using UnityEngine; using System.Collections.Generic; using XLua; public class UIManager : MonoSingleton { private Dictionary uiPanels = new Dictionary(); private Transform canvas; private Transform uiRoot; // Toast相关 private GameObject toastPrefab; private Transform toastContainer; protected override void Awake() { base.Awake(); InitializeUI(); } private void InitializeUI() { // 获取Canvas canvas = GameObject.Find("Canvas")?.transform; if (canvas == null) { GameObject canvasObj = new GameObject("Canvas"); canvas = canvasObj.transform; Canvas canvasComp = canvasObj.AddComponent(); canvasObj.AddComponent(); canvasObj.AddComponent(); canvasComp.renderMode = RenderMode.ScreenSpaceOverlay; } // 创建UI Root GameObject uiRootObj = new GameObject("UI_Root"); uiRootObj.transform.SetParent(canvas, false); uiRoot = uiRootObj.transform; // 创建Toast容器 GameObject toastContainerObj = new GameObject("ToastContainer"); toastContainerObj.transform.SetParent(canvas, false); toastContainer = toastContainerObj.transform; } // 显示面板 public GameObject ShowPanel(string panelName, string prefabPath = "") { if (uiPanels.ContainsKey(panelName)) { GameObject panel = uiPanels[panelName]; panel.SetActive(true); return panel; } // 加载预制体 GameObject prefab = Resources.Load(prefabPath); if (prefab == null) { Debug.LogError($"Failed to load UI prefab: {prefabPath}"); return null; } // 实例化 GameObject panel = GameObject.Instantiate(prefab, uiRoot); panel.name = panelName; uiPanels[panelName] = panel; return panel; } // 隐藏面板 public void HidePanel(string panelName) { if (uiPanels.ContainsKey(panelName)) { uiPanels[panelName].SetActive(false); } } // 关闭面板 public void ClosePanel(string panelName) { if (uiPanels.ContainsKey(panelName)) { Destroy(uiPanels[panelName]); uiPanels.Remove(panelName); } } // 获取面板 public GameObject GetPanel(string panelName) { if (uiPanels.ContainsKey(panelName)) { return uiPanels[panelName]; } return null; } // 显示Toast public void ShowToast(string message, float duration = 2f) { StartCoroutine(ShowToastCoroutine(message, duration)); } private IEnumerator ShowToastCoroutine(string message, float duration) { // 创建Toast GameObject toast = new GameObject("Toast"); toast.transform.SetParent(toastContainer, false); RectTransform rectTransform = toast.AddComponent(); rectTransform.anchoredPosition = new Vector2(0, -100); rectTransform.sizeDelta = new Vector2(400, 50); UnityEngine.UI.Image image = toast.AddComponent(); image.color = new Color(0, 0, 0, 0.7f); UnityEngine.UI.Text text = toast.AddComponent(); text.text = message; text.alignment = TextAnchor.MiddleCenter; text.font = Resources.GetBuiltinResource("Arial.ttf"); text.fontSize = 18; text.color = Color.white; // 动画效果 float elapsedTime = 0; while (elapsedTime < duration) { float alpha = Mathf.Lerp(0.7f, 0, elapsedTime / duration); image.color = new Color(0, 0, 0, alpha); text.color = new Color(1, 1, 1, alpha); elapsedTime += Time.deltaTime; yield return null; } Destroy(toast); } } // LuaBehaviour组件,用于将Lua数据绑定到GameObject [LuaCallCSharp] public class LuaBehaviour : MonoBehaviour { private LuaTable luaData; public void SetLuaData(string key, object value) { if (luaData == null) { LuaEnv luaEnv = LuaManager.Instance.GetLuaEnv(); luaData = luaEnv.NewTable(); } luaData.Set(key, value); } public object GetLuaData(string key) { if (luaData != null) { return luaData.Get(key); } return null; } public void ClearLuaData() { if (luaData != null) { luaData.Dispose(); luaData = null; } } private void OnDestroy() { ClearLuaData(); } } // CoroutineUtility,提供协程工具 [LuaCallCSharp] public class CoroutineUtility : MonoBehaviour { private static CoroutineUtility instance; public static CoroutineUtility Instance { get { if (instance == null) { GameObject go = new GameObject("CoroutineUtility"); DontDestroyOnLoad(go); instance = go.AddComponent(); } return instance; } } public static Coroutine StartCoroutine(IEnumerator coroutine) { return Instance.StartCoroutine(coroutine); } public static void StopCoroutine(Coroutine coroutine) { if (coroutine != null) { Instance.StopCoroutine(coroutine); } } } // JsonUtility扩展,提供JSON序列化功能 [LuaCallCSharp] public static class JsonUtilityEx { public static string ToJson(object obj) { return JsonUtility.ToJson(obj); } public static T FromJson(string json) { return JsonUtility.FromJson(json); } } ```
评论 0

发表评论 取消回复

Shift+Enter 换行  ·  Enter 发送
还没有评论,来发表第一条吧