# 搜索子物体组件与对象

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
/// <summary>
/// 搜索子物体组件-GameObject版
/// </summary>
public static T Get<T>(GameObject go, string subnode) where T : Component {
if (go != null) {
Transform sub = go.transform.FindChild(subnode);
if (sub != null) return sub.GetComponent<T>();
}
return null;
}

/// <summary>
/// 搜索子物体组件-Transform版
/// </summary>
public static T Get<T>(Transform go, string subnode) where T : Component {
if (go != null) {
Transform sub = go.FindChild(subnode);
if (sub != null) return sub.GetComponent<T>();
}
return null;
}

/// <summary>
/// 搜索子物体组件-Component版
/// </summary>
public static T Get<T>(Component go, string subnode) where T : Component {
return go.transform.FindChild(subnode).GetComponent<T>();
}
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
/// <summary>
/// 添加组件
/// </summary>
public static T Add<T>(GameObject go) where T : Component {
if (go != null) {
T[] ts = go.GetComponents<T>();
for (int i = 0; i < ts.Length; i++) {
if (ts[i] != null) GameObject.Destroy(ts[i]);
}
return go.gameObject.AddComponent<T>();
}
return null;
}

/// <summary>
/// 添加组件
/// </summary>
public static T Add<T>(Transform go) where T : Component {
return Add<T>(go.gameObject);
}

/// <summary>
/// 查找子对象
/// </summary>
public static GameObject Child(GameObject go, string subnode) {
return Child(go.transform, subnode);
}

/// <summary>
/// 查找子对象
/// </summary>
public static GameObject Child(Transform go, string subnode) {
Transform tran = go.FindChild(subnode);
if (tran == null) return null;
return tran.gameObject;
}

/// <summary>
/// 取平级对象
/// </summary>
public static GameObject Peer(GameObject go, string subnode) {
return Peer(go.transform, subnode);
}

/// <summary>
/// 取平级对象
/// </summary>
public static GameObject Peer(Transform go, string subnode) {
Transform tran = go.parent.FindChild(subnode);
if (tran == null) return null;
return tran.gameObject;
}

/// <summary>
/// 计算字符串的MD5值
/// </summary>
public static string md5(string source) {
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] data = System.Text.Encoding.UTF8.GetBytes(source);
byte[] md5Data = md5.ComputeHash(data, 0, data.Length);
md5.Clear();

string destString = "";
for (int i = 0; i < md5Data.Length; i++) {
destString += System.Convert.ToString(md5Data[i], 16).PadLeft(2, '0');
}
destString = destString.PadLeft(32, '0');
return destString;
}

/// <summary>
/// 计算文件的MD5值
/// </summary>
public static string md5file(string file) {
try {
FileStream fs = new FileStream(file, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(fs);
fs.Close();

StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++) {
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
} catch (Exception ex) {
throw new Exception("md5file() fail, error:" + ex.Message);
}
}

/// <summary>
/// 清除所有子节点
/// </summary>
public static void ClearChild(Transform go) {
if (go == null) return;
for (int i = go.childCount - 1; i >= 0; i--) {
GameObject.Destroy(go.GetChild(i).gameObject);
}
}

/// <summary>
/// 清理内存
/// </summary>
public static void ClearMemory() {
GC.Collect(); Resources.UnloadUnusedAssets();
LuaManager mgr = AppFacade.Instance.GetManager<LuaManager>(ManagerName.Lua);
if (mgr != null) mgr.LuaGC();
}