Skip to content

Commit a97dbfc

Browse files
committed
[增加]1. 增加路径搜索功能
1 parent ddcfb64 commit a97dbfc

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed

Runtime/UI/GObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,5 +2198,10 @@ public GTweener TweenRotate(float endValue, float duration)
21982198
}
21992199

22002200
#endregion
2201+
2202+
public string Path
2203+
{
2204+
get { return PathFinderHelper.GetUIPath(this); }
2205+
}
22012206
}
22022207
}

Runtime/Utils/PathFinderHelper.cs

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace FairyGUI.Utils
6+
{
7+
/// <summary>
8+
/// FGUI 路径帮助类
9+
/// </summary>
10+
[UnityEngine.Scripting.Preserve]
11+
public static class PathFinderHelper
12+
{
13+
/// <summary>
14+
/// 根据UI对象获取路径
15+
/// </summary>
16+
/// <param name="o">UI对象</param>
17+
/// <returns>UI所在路径</returns>
18+
public static string GetUIPath(GObject o)
19+
{
20+
var ls = new List<string>();
21+
SearchParent(o, ls);
22+
ls.Reverse();
23+
return string.Join("/", ls);
24+
}
25+
26+
private static void SearchParent(GObject o, List<string> st)
27+
{
28+
if (o.parent != null)
29+
{
30+
st.Add(o.name);
31+
SearchParent(o.parent, st);
32+
}
33+
else
34+
{
35+
st.Add(o.name);
36+
}
37+
}
38+
39+
/// <summary>
40+
/// 根据路径获取FUI对象
41+
/// </summary>
42+
/// <param name="path">UI路径</param>
43+
/// <returns>UI对象</returns>
44+
public static GObject GetUIFromPath(string path)
45+
{
46+
//GRoot / UISynthesisScene / ContentBox / ListSelect / 1990197248 / icon
47+
48+
string[] arr = path.Split('/', StringSplitOptions.RemoveEmptyEntries);
49+
50+
var q = new Queue<string>();
51+
foreach (string pathName in arr)
52+
{
53+
if (pathName == "GRoot")
54+
{
55+
continue;
56+
}
57+
58+
q.Enqueue(pathName);
59+
}
60+
61+
try
62+
{
63+
GObject child = SearchChild(GRoot.inst, q);
64+
return child;
65+
}
66+
catch (Exception exception)
67+
{
68+
Debug.LogError("error uiPath : can not found ui by this path :" + path + ", error : " + exception);
69+
}
70+
71+
return null;
72+
}
73+
74+
private static GObject SearchChild(GComponent o, Queue<string> queue)
75+
{
76+
//防错
77+
if (queue.Count <= 0)
78+
{
79+
return o;
80+
}
81+
82+
string path = queue.Dequeue();
83+
GObject child = null;
84+
if (path[0] == '$')
85+
{
86+
child = o.GetChild(path);
87+
if (child == null)
88+
{
89+
string at = path.Substring(1);
90+
int index = int.Parse(at);
91+
92+
if (index < 0 || index >= o.numChildren)
93+
{
94+
throw new Exception("eror path");
95+
}
96+
97+
child = o.GetChildAt(index);
98+
}
99+
}
100+
else
101+
{
102+
child = o.GetChild(path);
103+
}
104+
105+
if (child == null)
106+
{
107+
throw new Exception("error path");
108+
}
109+
110+
if (queue.Count <= 0)
111+
{
112+
// 说明没有下级了
113+
return child;
114+
}
115+
116+
if (child is GComponent)
117+
{
118+
return SearchChild(child as GComponent, queue);
119+
}
120+
121+
throw new Exception("error path");
122+
}
123+
124+
/// <summary>
125+
/// 路径是否包含该对象
126+
/// </summary>
127+
/// <param name="gObject">对象</param>
128+
/// <param name="path">路径</param>
129+
/// <returns>是否包含</returns>
130+
public static bool IsIncludePath(this GObject gObject, string path)
131+
{
132+
if ("all".ToLower() == path)
133+
{
134+
return false;
135+
}
136+
137+
var q = new List<string>();
138+
139+
foreach (string pathName in path.Split('/', StringSplitOptions.RemoveEmptyEntries))
140+
{
141+
if (pathName == "GRoot")
142+
{
143+
continue;
144+
}
145+
146+
q.Add(pathName);
147+
}
148+
149+
GObject current = gObject;
150+
var list = new List<GObject> { current, };
151+
while (current.parent != null && current.parent.name != "GRoot")
152+
{
153+
current = current.parent;
154+
list.Add(current);
155+
}
156+
157+
// 反转链表
158+
list.Reverse();
159+
160+
if (list.Count < q.Count)
161+
{
162+
// 路径长度小于,肯定是不对的
163+
return false;
164+
}
165+
166+
for (int i = 0; i < q.Count; i++)
167+
{
168+
if (list[i].name == q[i])
169+
{
170+
continue;
171+
}
172+
173+
if (q[i][0] == '$')
174+
{
175+
string at = q[i].Substring(1);
176+
int index = int.Parse(at);
177+
if (list[i].parent.GetChildIndex(list[i]) == index)
178+
{
179+
continue;
180+
}
181+
182+
{
183+
return false;
184+
}
185+
}
186+
187+
return false;
188+
}
189+
190+
return true;
191+
}
192+
193+
/// <summary>
194+
/// 路径是否包含该对象
195+
/// </summary>
196+
/// <param name="path">路径</param>
197+
/// <param name="gObject">对象</param>
198+
/// <returns></returns>
199+
public static bool SearchPathInclude(string path, GObject gObject)
200+
{
201+
if ("all".ToLower() == path)
202+
{
203+
return false;
204+
}
205+
206+
var q = new List<string>();
207+
208+
foreach (string v in path.Split('/'))
209+
{
210+
if (v == "GRoot")
211+
{
212+
continue;
213+
}
214+
215+
q.Add(v);
216+
}
217+
218+
GObject current = gObject;
219+
var list = new List<GObject> { current, };
220+
while (current.parent != null && current.parent.name != "GRoot")
221+
{
222+
current = current.parent;
223+
list.Add(current);
224+
}
225+
226+
// 反转链表
227+
list.Reverse();
228+
229+
if (list.Count < q.Count)
230+
{
231+
// 路径长度小于,肯定是不对的
232+
return false;
233+
}
234+
235+
for (int i = 0; i < q.Count; i++)
236+
{
237+
if (list[i].name == q[i])
238+
{
239+
continue;
240+
}
241+
242+
if (q[i][0] == '$')
243+
{
244+
string at = q[i].Substring(1);
245+
int index = int.Parse(at);
246+
if (list[i].parent.GetChildIndex(list[i]) == index)
247+
{
248+
continue;
249+
}
250+
251+
{
252+
return false;
253+
}
254+
}
255+
256+
return false;
257+
}
258+
259+
return true;
260+
}
261+
}
262+
}

Runtime/Utils/PathFinderHelper.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)