Compare commits

..

4 Commits

5 changed files with 50 additions and 98 deletions

View File

@@ -32,10 +32,14 @@ public class MyWindow : AbsHFUIWindow
| 方法 | 调用时机 | 推荐操作 |
|-----|---------|---------|
| `OnRunAwake` | 创建时一次 | 绑定UI事件 |
| `OnOpen` | 每次打开 | 注册TC事件、刷新数据 |
| `OnRunAwake` | 创建时一次 | 绑定UI事件、初始化滚动列表 `BindEvent` |
| `OnOpen` | 每次打开 | 注册TC事件、调用 `ShowView()` 刷新数据 |
| `OnClose` | 每次关闭 | 取消TC事件 |
**关键原则**
- **绑定类操作**(如 `BindEvent`)放在 `OnRunAwake`,只需执行一次
- **刷新类操作**(如 `ShowView`)放在 `OnOpen`,每次打开都需执行
### 1.3 窗口操作
```csharp
// 打开
@@ -77,17 +81,46 @@ img.HFSetSprite(/* atlasName */, "Icon_Player", /* languageRoot */);
```
### 3.4 滚动列表(核心)
**生命周期正确划分示例**
```csharp
SFUI_ScrollRect scroll = GetComponent<SFUI_ScrollRect>();
scroll.BindEvent(
() => itemList.Count,
(idx) => itemPrefab.gameObject,
OnUpdateItem,
(item) => item.gameObject.HFGetOrAddSimulateBehaviour<ItemSlot>()
private SFUI_ScrollRect m_ScrollList;
private List<ItemData> m_ItemList;
protected override void OnRunAwake()
{
base.OnRunAwake();
m_ScrollList = FindUICtrlByName<SFUI_ScrollRect>("ScrollList");
// ✅ 绑定事件放在 Awake一次绑定永久生效
m_ScrollList.BindEvent(
() => m_ItemList.Count, // 数据源闭包
(idx) => itemPrefab.gameObject, // 生成预制体
OnUpdateItem, // 更新回调
(item) => item.gameObject.HFGetOrAddSimulateBehaviour<ItemSlot>() // 获取组件
);
scroll.ShowView();
}
protected override void OnOpen(Action _onOpenComplete)
{
base.OnOpen(_onOpenComplete);
m_ItemList = GetDataFromServer(); // 获取最新数据
m_ScrollList.ShowView(); // ✅ 刷新显示放在 Open
}
private void OnUpdateItem(int index, AbsHFUIWindow window)
{
ItemSlot slot = window as ItemSlot;
slot.SetData(m_ItemList[index]);
}
```
**设计要点**
- `BindEvent``OnRunAwake` 中执行,建立绑定机制
- `ShowView``OnOpen` 中执行,触发数据刷新
- 数据源通过闭包引用,`ShowView` 时自动获取最新数据
---
## 四、对象池
@@ -175,5 +208,5 @@ public class CardItem : AbsListItem<CardData>
---
**版本**: v1.0
**适用**: StrayFog框架开发人员
**版本**: v1.1
**适用**: StrayFog框架UI开发人员

View File

@@ -167,9 +167,11 @@ Window
### 8.1 概述
**UI相关人员核心工作流程**读取设计效果图和碎片图片生成JSON配置文件再通过工具自动生成Unity预制体。
本项目支持JSON配置与Unity预制体的双向转换
- **正向转换**JSON配置 → UI预制体
- **反向转换**UI预制体 → JSON配置
- **正向转换**JSON配置 → UI预制体(核心流程)
- **反向转换**UI预制体 → JSON配置(开发者调整完预制体后,反向生成用于版本控制或重新生成)
实现UI界面的快速构建、迭代和版本控制。
@@ -287,19 +289,7 @@ GenerateUIPrefab:Assets/Editor/UI/BagWindow.json
└─────────────────────────────────────────────────────────────────────┘
```
### 8.6 Unity编辑器菜单测试
在Unity编辑器中提供了测试菜单
| 菜单路径 | 功能 |
|---------|------|
| `Tools > UI > Export Prefab to JSON` | 选择预制体导出JSON |
| `Tools > UI > Generate BagWindow Prefab` | 生成BagWindow预制体 |
| `Tools > External > Test ReadJson` | 测试ReadJson命令 |
| `Tools > External > Test GenerateUIPrefab` | 测试正向生成 |
| `Tools > External > Test ExportPrefabToJson` | 测试反向导出 |
### 8.7 使用示例
### 8.6 使用示例
#### 方式一:外部命令触发

View File

@@ -148,18 +148,6 @@ public static class ExternalCommandListener
}
}
[MenuItem("Tools/External/Test ReadJson")]
public static void TestExecuteReadJson()
{
ExecuteReadJson(new string[0]);
}
[MenuItem("Tools/External/Test GenerateUIPrefab")]
public static void TestExecuteGenerateUIPrefab()
{
ExecuteGenerateUIPrefab(new[] { "Assets/Editor/UI/BagWindow.json" });
}
private static void ExecuteExportPrefabToJson(string[] args)
{
try
@@ -185,10 +173,4 @@ public static class ExternalCommandListener
Debug.LogError("ExportPrefabToJson failed: " + ex.Message);
}
}
[MenuItem("Tools/External/Test ExportPrefabToJson")]
public static void TestExecuteExportPrefabToJson()
{
ExecuteExportPrefabToJson(new[] { "Assets/Game/GameHFResource/CN/UIWindows/BagWindow/BagWindow.prefab" });
}
}

View File

@@ -8,46 +8,6 @@ using TMPro;
public static class UIPrefabGenerator
{
[MenuItem("Tools/UI/Generate BagWindow Prefab")]
public static void GenerateBagWindowPrefab()
{
string jsonPath = "Assets/Editor/UI/BagWindow.json";
string prefabPath = "Assets/Game/GameHFResource/CN/UIWindows/BagWindow/BagWindow.prefab";
if (!File.Exists(jsonPath))
{
Debug.LogError("JSON file not found: " + jsonPath);
return;
}
string jsonContent = File.ReadAllText(jsonPath);
UILayoutData layoutData = JsonUtility.FromJson<UILayoutData>(jsonContent);
GameObject root = new GameObject(layoutData.name);
root.layer = 5;
Canvas canvas = root.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
canvas.pixelPerfect = layoutData.canvas.pixelPerfect;
root.AddComponent<CanvasScaler>();
root.AddComponent<GraphicRaycaster>();
RectTransform rootRect = root.GetComponent<RectTransform>();
SetRectTransform(rootRect, layoutData.rectTransform);
foreach (var childData in layoutData.children)
{
CreateUIElement(childData, root.transform);
}
CreatePrefab(root, prefabPath);
GameObject.DestroyImmediate(root);
AssetDatabase.Refresh();
Debug.Log("UI Prefab generated successfully: " + prefabPath);
}
public static void GeneratePrefabFromJson(string jsonPath, string prefabPath = null)
{
if (!File.Exists(jsonPath))

View File

@@ -9,19 +9,6 @@ using TMPro;
public static class UIPrefabToJson
{
[MenuItem("Tools/UI/Export Prefab to JSON")]
public static void ExportPrefabToJsonMenu()
{
string prefabPath = EditorUtility.OpenFilePanel("选择预制体", "Assets", "prefab");
if (!string.IsNullOrEmpty(prefabPath))
{
string projectPath = Application.dataPath.Replace("/Assets", "");
string relativePath = prefabPath.Replace(projectPath + "/", "");
string jsonPath = relativePath.Replace(".prefab", ".json");
ExportPrefabToJson(relativePath, jsonPath);
}
}
public static void ExportPrefabToJson(string prefabPath, string jsonPath = null)
{
if (!AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath))