我第一次接触OSA,第一感觉就是庞杂,相关的文档和资料基本都是英文,运行下示例场景,效果和效率确实很香。本文仅针对初次接触OSA、望而却步的朋友们进行快速运用的引导。
首先,找个安装包,导入项目后,观看视频(看不懂?也要看);
好了,整个视频其实你只需要搞懂⼀开始的那张MVC结构图。简单来说,OSA是基于MVC架构封装的,适⽤于UGUI的列表组件(这⾥UGUI的ScrollView问题不再赘述)。OSA基本的⼯作流程就是, 将列表单元格的数据封装成ItemModel(可以是⼀个JsonObject,很⽅便从json配表中取值封
装),通过ItemViewAdapter创建、复⽤预设并显⽰到Content中,⽽ItemViewsHolder则控制单元
格的显⽰效果。下⾯,我们来个实战:
1、创建⼀个新场景

2、添加Canvas

3、给Canvas添加OSA

4、选择布局

5、选择列表是Grid还是List

6、脚本创建完成后,会有⼀个要求添加预设的弹框

7、在Canvas下创建⼀个空对象,拖⾄ItemPrefab


8、打开刚才创建的Adapter脚本BasicListAdapter

9、修改Adapter脚本
a、打开Start中的数据填充函数RetrieveDataAndUpdate;
protected override void Start()
{Data = new SimpleDataHelper<MyListItemModel>(this);// Calling this initializes internal data and prepares the adapter to handle item count changesbase.Start();// Retrieve the models from your data source and set the items countRetrieveDataAndUpdate(500);
}
b、修改异步添加model数据函数FetchMoreItemsFromDataSourceAndUpdate;
1 IEnumerator FetchMoreItemsFromDataSourceAndUpdate(int count)
2 {
3 // Simulating data retrieving delay
4 yield return new WaitForSeconds(.5f);
5 var newItems = new MyListItemModel[count];
6 // Retrieve your data here
7 for (int i = 0; i < count; ++i)
8 {
9 var model = new MyListItemModel()
10 {
11 title = "Random item " + i,
12 color = new Color(
13 UnityEngine.Random.Range(0f, 1f),
14 UnityEngine.Random.Range(0f, 1f),
15 UnityEngine.Random.Range(0f, 1f),
16 UnityEngine.Random.Range(0f, 1f)
17 )
18 };
19 newItems[i] = model;
20 }
21 OnDataRetrieved(newItems);
22 }
c、打开数据刷新函数UpdateViewsHolder,通过model数据刷新view
1 protected override void UpdateViewsHolder(MyListItemViewsHolder newOrRecycled)
2 {
3 // In this callback, "newOrRecycled.ItemIndex" is guaranteed to always
reflect the
4 // index of item that should be represented by this views holder. You'll use
this index
5 // to retrieve the model from your data set
6 MyListItemModel model = Data[newOrRecycled.ItemIndex];
7 newOrRecycled.backgroundImage.color = model.color;
8 newOrRecycled.titleText.text = model.title + " #" + newOrRecycled.ItemIndex;
9 }
d、打开model结构定义
1 public class MyListItemModel
2 {
3 public string title;
4 public Color color;
5 }
e、获取view的组件
1 public override void CollectViews()
2 {
3 base.CollectViews();
4 // GetComponentAtPath is a handy extension method from
frame8.Logic.Misc.Other.Extensions
5 // which infers the variable's component from its type, so you won't need to
specify it yourself
6 root.GetComponentAtPath("Text", out titleText);
7 backgroundImage = root.GetComponent<Image>();
8 }
10、修改ItemPrefab

11、好了,点击运⾏看看效果吧

现在你可以试试,怎样创建BasicGridAdapter了。