UI框架开发中

This commit is contained in:
2025-11-06 17:34:33 +08:00
parent 5b31b629cd
commit 5fdb3183f2
15 changed files with 349 additions and 11 deletions

View File

@@ -655,7 +655,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
MainCamera: {fileID: 330585545}
PlayMode: 2
PlayMode: 0
--- !u!4 &1019312449
Transform:
m_ObjectHideFlags: 0
@@ -881,6 +881,14 @@ PrefabInstance:
propertyPath: m_Name
value: IngameDebugConsole
objectReference: {fileID: 0}
- target: {fileID: 22331464, guid: 67117722a812a2e46ab8cb8eafbf5f5e, type: 3}
propertyPath: m_Camera
value:
objectReference: {fileID: 124489897}
- target: {fileID: 22331464, guid: 67117722a812a2e46ab8cb8eafbf5f5e, type: 3}
propertyPath: m_RenderMode
value: 1
objectReference: {fileID: 0}
- target: {fileID: 22400762, guid: 67117722a812a2e46ab8cb8eafbf5f5e, type: 3}
propertyPath: m_AnchoredPosition.y
value: 0.0000076293945

View File

@@ -3,9 +3,9 @@ using UnityEngine;
public class MainUICanvas : SingletonMono<MainUICanvas>
{
public GameObject InitBg;
public Transform Top;
public Transform Medium;
public Transform Bottom;
public RectTransform Top;
public RectTransform Medium;
public RectTransform Bottom;
public Camera UICamera;
private void Awake()
{

View File

@@ -0,0 +1,113 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &3149112341066480121
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8374724181955065529}
- component: {fileID: 103223121363424772}
- component: {fileID: 8793378026247639231}
m_Layer: 5
m_Name: Image
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8374724181955065529
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3149112341066480121}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 2569990057225954958}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &103223121363424772
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3149112341066480121}
m_CullTransparentMesh: 1
--- !u!114 &8793378026247639231
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3149112341066480121}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0, g: 0.9355135, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 0}
m_Type: 0
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!1 &6364109098557891170
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 2569990057225954958}
m_Layer: 5
m_Name: FullTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &2569990057225954958
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6364109098557891170}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 8374724181955065529}
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ebc30e082f9e5fa48bd59697dd386346
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -13,8 +13,11 @@ public class GameStart : MonoBehaviour
}
async UniTask LoadSimpleR3Test()
{
AssetHandle handle = YooAssets.LoadAssetAsync<GameObject>("SimpleR3Test");
await handle;
GameObject.Instantiate(handle.AssetObject, MainUICanvas.Inst.Medium);
UIManager.Inst.ShowUI<SimpleR3Test>("test1", MainUICanvas.Inst.Medium);
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test2", MainUICanvas.Inst.Medium);
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test2");
await UIManager.Inst.ShowUIAsync<SimpleR3Test>("test3");
await UIManager.Inst.TestShowUIAsync("FullTest", "test4", MainUICanvas.Inst.Medium,true);
UIManager.Inst.TestShowUI("FullTest","test5", MainUICanvas.Inst.Medium);
}
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 43d2406acc71f48448efde6aa488d064
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -2,16 +2,16 @@ using R3;
using UnityEngine;
using UnityEngine.UI;
public class SimpleR3Test : MonoBehaviour
public class SimpleR3Test : UIBase
{
public SerializableReactiveProperty<float> value = new(0);
public InputField inputField;
public Scrollbar scrollbar;
void Start()
public override void OnCreate()
{
base.OnCreate();
value.BindToScrollbar(scrollbar).AddTo(this);
value.BindToInputField(inputField).AddTo(this);
}

View File

@@ -0,0 +1,32 @@
using UnityEngine;
public class UIBase : MonoBehaviour
{
public RectTransform rectTransform;
public UIBase parent;
public virtual void OnCreate()
{
rectTransform = GetComponent<RectTransform>();
}
public virtual void OnShow() { }
public virtual void OnHide() { }
public void SetFull()
{
rectTransform.anchorMin = Vector2.zero;
rectTransform.anchorMax = Vector2.one;
rectTransform.offsetMin = Vector2.zero;
rectTransform.offsetMax = Vector2.zero;
rectTransform.sizeDelta = Vector2.zero;
}
public void SetParent(RectTransform ui, bool isFull = false)
{
rectTransform.SetParent(ui);
rectTransform.localScale = Vector3.one;
rectTransform.localPosition = Vector3.zero;
rectTransform.localRotation = Quaternion.identity;
if (isFull)
SetFull();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 11bbb38035ff49641be77ec96d27de0c

View File

@@ -0,0 +1,137 @@
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using YooAsset;
public class UIManager : Singleton<UIManager>
{
public Dictionary<string, UIBase> openedUIs = new Dictionary<string, UIBase>();
Stack<UIBase> uiStack = new Stack<UIBase>();
public T ShowUI<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
{
T ui = null;
string uiType = typeof(T).Name;
if (!openedUIs.ContainsKey(uiName))
{
GameObject uiPrefab = LoadUI(uiType);
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
openedUIs[uiName] = ui;
uiStack.Push(ui);
}
else
{
ui = openedUIs[uiName] as T;
}
Debug.Log($"ShowUI====>name:{uiName} type:{typeof(T).Name}");
ui.OnShow();
return ui;
}
public async UniTask<T> ShowUIAsync<T>(string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
{
T ui = null;
string uiType = typeof(T).Name;
if (!openedUIs.ContainsKey(uiName))
{
GameObject uiPrefab = await LoadUIAsync(uiType);
ui = CreateUI<T>(uiPrefab, uiName, parent, isFull);
openedUIs[uiName] = ui;
uiStack.Push(ui);
}
else
{
ui = openedUIs[uiName] as T;
}
Debug.Log($"ShowUIAsync====>name:{uiName} type:{typeof(T).Name}");
ui.OnShow();
return ui;
}
T CreateUI<T>(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false) where T : UIBase
{
GameObject uiObj = GameObject.Instantiate(uiPrefab);
uiObj.name = uiName;
T ui = uiObj.GetComponent<T>();
ui.OnCreate();
if (parent) ui.SetParent(parent, isFull);
Debug.Log($"CreateUI====>name:{uiName} type:{typeof(T).Name}");
return ui;
}
GameObject LoadUI(string uiType)
{
AssetHandle uiHandle = YooAssets.LoadAssetSync<GameObject>(uiType);
return (GameObject)uiHandle.AssetObject;
}
async UniTask<GameObject> LoadUIAsync(string uiType)
{
AssetHandle uiHandle = YooAssets.LoadAssetAsync<GameObject>(uiType);
await uiHandle.ToUniTask();
return (GameObject)uiHandle.AssetObject;
}
#region UIBase的
public Dictionary<string, GameObject> testOpenedUIs = new Dictionary<string, GameObject>();
Stack<GameObject> testUIStack = new Stack<GameObject>();
public GameObject TestShowUI(string path, string uiName, RectTransform parent = null, bool isFull = false)
{
GameObject ui = null;
if (!testOpenedUIs.ContainsKey(uiName))
{
GameObject uiPrefab = LoadUI(path);
ui = TestCreateUI(uiPrefab, uiName, parent, isFull);
testOpenedUIs[uiName] = ui;
testUIStack.Push(ui);
}
else
{
ui = testOpenedUIs[uiName];
}
Debug.Log($"TestShowUI====>name:{uiName}");
return ui;
}
public async UniTask< GameObject> TestShowUIAsync(string path, string uiName, RectTransform parent = null, bool isFull = false)
{
GameObject ui = null;
if (!testOpenedUIs.ContainsKey(uiName))
{
GameObject uiPrefab = await LoadUIAsync(path);
ui = TestCreateUI(uiPrefab, uiName, parent, isFull);
testOpenedUIs[uiName] = ui;
testUIStack.Push(ui);
}
else
{
ui = testOpenedUIs[uiName];
}
Debug.Log($"TestShowUI====>name:{uiName}");
return ui;
}
GameObject TestCreateUI(GameObject uiPrefab, string uiName, RectTransform parent = null, bool isFull = false)
{
GameObject uiObj = GameObject.Instantiate(uiPrefab);
uiObj.name = uiName;
RectTransform ui = uiObj.GetComponent<RectTransform>();
if (parent) TestSetParent(ui, parent, isFull);
Debug.Log($"TestCreateUI====>name:{uiName}");
return uiObj;
}
void TestSetFull(RectTransform rectTransform)
{
rectTransform.anchorMin = Vector2.zero;
rectTransform.anchorMax = Vector2.one;
rectTransform.offsetMin = Vector2.zero;
rectTransform.offsetMax = Vector2.zero;
rectTransform.sizeDelta = Vector2.zero;
}
void TestSetParent(RectTransform ui, RectTransform parent, bool isFull = false)
{
ui.SetParent(parent);
ui.localScale = Vector3.one;
ui.localPosition = Vector3.zero;
ui.localRotation = Quaternion.identity;
if (isFull)
TestSetFull(ui);
}
#endregion
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a59f1c308a49e0742878b0c58f906538

View File

@@ -1,2 +1,28 @@
fileFormatVersion: 2
guid: 358f9376bf3004b4dadf89a4e4518d83
guid: 358f9376bf3004b4dadf89a4e4518d83
labels:
- NuGetForUnity
PluginImporter:
externalObjects: {}
serializedVersion: 3
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
Any:
enabled: 0
settings: {}
Editor:
enabled: 0
settings:
DefaultValueInitialized: true
WindowsStoreApps:
enabled: 0
settings: {}
userData:
assetBundleName:
assetBundleVariant: