-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cs
More file actions
96 lines (84 loc) · 2.24 KB
/
Copy pathUtil.cs
File metadata and controls
96 lines (84 loc) · 2.24 KB
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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public static class Util
{
public static T GetChildByName<T>(this GameObject go, string name) where T:Component
{
var child = GetChildByName(go, name);
if (child == null)
return default(T);
return child.gameObject.GetComponent<T>();
}
public static GameObject GetChildByName(this GameObject go, string name)
{
var child = GetChildByName(go.transform, name);
return child == null ? null : child.gameObject;
}
public static Transform GetChildByName(Transform tr, string name)
{
// 广度优先
foreach (Transform child in tr)
{
if (child.name == name)
return child;
}
foreach (Transform child in tr)
{
Transform c = GetChildByName(child, name);
if (c != null)
return c;
}
return null;
}
public delegate bool TraversalCallback(Transform go);
public static bool Traversal(this Transform transform, TraversalCallback callback)
{
// 深度优先
foreach (Transform child in transform)
{
if (!callback(child))
return false;
if (!child.Traversal(callback))
return false;
}
return true;
}
public static void ChangeLayer(GameObject go, int layer)
{
go.layer = layer;
go.transform.Traversal(child=>{child.gameObject.layer = layer; return true; });
}
public static string UrlToIP(string url)
{
try
{
var host = System.Net.Dns.GetHostEntry(url);
foreach (System.Net.IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
return ip.ToString();
}
return url;
}
catch(System.Exception e)
{
Debug.LogException(e);
return url;
}
}
public static string Md5File(string file)
{
using (var stream = System.IO.File.OpenRead(file))
{
var md5 = System.Security.Cryptography.MD5.Create();
var data = md5.ComputeHash(stream);
var sb = new System.Text.StringBuilder();
foreach(var b in data)
{
sb.Append(b.ToString("x2"));
}
return sb.ToString().ToLower();
}
}
}