UIOfflineData

UI离线数据比普通离线数据增加了一些RectTransformParticleSystem信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using UnityEngine;

public class UIOfflineData : OfflineData
{
public Vector2[] m_AnchorMax;
public Vector2[] m_AnchorMin;
public Vector2[] m_Pivot;
public Vector2[] m_SizeDelta;
public Vector3[] m_AnchoredPos;
public ParticleSystem[] m_Particle;

public override void ResetProp()
{
int allPointCount = m_AllPoint.Length;
for (int i = 0; i < allPointCount; i++)
{
RectTransform tempTrans = m_AllPoint[i] as RectTransform;
if (tempTrans != null)
{
tempTrans.localPosition = m_Pos[i];
tempTrans.localRotation = m_Rot[i];
tempTrans.localScale = m_Scale[i];
tempTrans.anchorMax = m_AnchorMax[i];
tempTrans.anchorMin = m_AnchorMin[i];
tempTrans.pivot = m_Pivot[i];
tempTrans.sizeDelta = m_SizeDelta[i];
tempTrans.anchoredPosition3D = m_AnchoredPos[i];

if (m_AllPointActive[i])
{
if (!tempTrans.gameObject.activeSelf)
{
tempTrans.gameObject.SetActive(true);
}
}
else
{
if (tempTrans.gameObject.activeSelf)
{
tempTrans.gameObject.SetActive(false);
}
}

if (tempTrans.childCount > m_AllPointChildCount[i])
{
int childCount = tempTrans.childCount;
for (int j = m_AllPointChildCount[i]; j < childCount; j++)
{
GameObject tempObj = tempTrans.GetChild(j).gameObject;
if (!ObjectPoolManager.Instance.IsPoolManagerCreat(tempObj))
{
GameObject.Destroy(tempObj);
}
}
}

}

}
int particleCount = m_Particle.Length;
for (int i = 0; i < particleCount; i++)
{
m_Particle[i].Clear(true);
m_Particle[i].Play();
}

}

public override void BindData()
{
Transform[] allTrans = gameObject.GetComponentsInChildren<Transform>(true);
int allTransCount = allTrans.Length;
for (int i = 0; i < allTransCount; i++)
{
GetOrAddRectTransform(allTrans[i]);
}

m_AllPoint = gameObject.GetComponentsInChildren<RectTransform>(true);
m_Particle = gameObject.GetComponentsInChildren<ParticleSystem>(true);

int allPointCount = m_AllPoint.Length;
m_AllPointChildCount = new int[allPointCount];
m_AllPointActive = new bool[allPointCount];
m_Pos = new Vector3[allPointCount];
m_Rot = new Quaternion[allPointCount];
m_Scale = new Vector3[allPointCount];

m_Pivot = new Vector2[allPointCount];
m_AnchorMax = new Vector2[allPointCount];
m_AnchorMin = new Vector2[allPointCount];
m_SizeDelta = new Vector2[allPointCount];
m_AnchoredPos = new Vector3[allPointCount];

for (int i = 0; i < allPointCount; i++)
{
RectTransform temp = m_AllPoint[i] as RectTransform;
m_AllPointChildCount[i] = temp.childCount;
m_AllPointActive[i] = temp.gameObject.activeSelf;
m_Pos[i] = temp.localPosition;
m_Rot[i] = temp.localRotation;
m_Scale[i] = temp.localScale;

m_Pivot[i] = temp.pivot;
m_AnchorMax[i] = temp.anchorMax;
m_AnchorMin[i] = temp.anchorMin;
m_SizeDelta[i] = temp.sizeDelta;
m_AnchoredPos[i] = temp.anchoredPosition3D;
}
}
void GetOrAddRectTransform(Transform trans)
{
if(trans.gameObject.GetComponent<RectTransform>() != null)
{
return;
}
else
{
trans.gameObject.AddComponent<RectTransform>();
}
}
}

UI离线数据的编辑器脚本

我们接着在OfflineDataEditor中修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using UnityEditor;
using UnityEngine;

public class OfflineDataEditor
{
[MenuItem("Assets/离线数据/生成离线数据")]
public static void AssetCreateOfflineData()
{
GameObject[] objects = Selection.gameObjects;
for (int i = 0; i < objects.Length; i++)
{
EditorUtility.DisplayProgressBar("添加离线数据", "正在修改:" + objects[i] + "……", 1.0f / objects.Length * i);
CreateOfflineData(objects[i]);
}
EditorUtility.ClearProgressBar();
}
[MenuItem("Assets/离线数据/生成UI离线数据")]
public static void AssetCreateUIOfflineData()
{
GameObject[] objects = Selection.gameObjects;
for (int i = 0; i < objects.Length; i++)
{
EditorUtility.DisplayProgressBar("添加UI离线数据", "正在修改:" + objects[i] + "……", 1.0f / objects.Length * i);
CreateUIOfflineData(objects[i]);
}
EditorUtility.ClearProgressBar();
}
[MenuItem("离线数据/生成所有UI prefab离线数据")]
public static void AllCreateUIData()
{
string[] allStr = AssetDatabase.FindAssets("t:Prefab",new string[] {"Assets/GameData/Prefabs/UGUI"});
for (int i = 0; i < allStr.Length; i++)
{
string prefabPath = AssetDatabase.GUIDToAssetPath(allStr[i]);
EditorUtility.DisplayProgressBar("添加UI离线数据", "正在扫描路径:" + prefabPath + "……", 1.0f / allStr.Length * i);
GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
if (obj == null)
{
continue;
}
CreateUIOfflineData(obj);
}
Debug.Log("UI离线数据全部生成完毕!");
EditorUtility.ClearProgressBar();
}
public static void CreateOfflineData(GameObject obj)
{
OfflineData offlieData = obj.GetComponent<OfflineData>();
if (offlieData == null)
{
offlieData = obj.AddComponent<OfflineData>();
}
offlieData.BindData();
EditorUtility.SetDirty(obj);
Debug.Log("修改了" + obj.name + " prefab");
Resources.UnloadUnusedAssets();
AssetDatabase.Refresh();
}
public static void CreateUIOfflineData(GameObject obj)
{
obj.layer = LayerMask.NameToLayer("UI");
UIOfflineData uiOfflineData = obj.GetComponent<UIOfflineData>();
if (uiOfflineData == null)
{
uiOfflineData = obj.AddComponent<UIOfflineData>();
}
uiOfflineData.BindData();
EditorUtility.SetDirty(obj);
Debug.Log("修改了" + obj.name + " UIprefab");
Resources.UnloadUnusedAssets();
AssetDatabase.Refresh();
}
}

注意我们把Assets/GameData/Prefabs/UGUI/作为UI的所有prefab地址

注意AssetDatabase.FindAssets这个API只能读取“Asset”开头的路径,我们使用Application.dataPath返回的路径是全路径,不能使用。