方法1

数据结构样式

1
2
3
4
5
6
7
(ROOT)
/ \
Dir2 Dir3
/ \ \
Dir4 Dir5 Dir6
/
Dir7

创建表:filesystem

Id dirName
1 Root
2 Dir2
3 Dir3
4 Dir4
5 Dir5
6 Dir6
7 Dir7

创建表:treePath

Id ancestor descendant
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
6 1 6
7 1 7
8 2 2
9 2 4
10 2 5
11 2 7
12 3 3
13 3 6
14 4 4
15 4 7
16 5 5
17 6 6
18 7 7

获取全部

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void tt()
{
new DataService("tempDB.sqlite3");
dbPath = string.Format(@"Assets/StreamingAssets/tempDB.sqlite3");
_connection = new SQLiteConnection(dbPath, SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create);
var files = _connection.Table<Filesystem>().ToObservable();
var treePath = _connection.Table<TreePath>();
var joine = files.SelectMany(x => treePath, (x, y) => new {x, y})
.Where(t => t.x.Id == t.y.descendant)
.Where(t => t.y.ancestor == 1)
.Select(t => t.x.dirName);
joine.Do(x => Debug.Log(x))
.Subscribe();
}

结果为:

id dirname
1 ROOT
2 Dir2
3 Dir3
4 Dir4
5 Dir5
6 Dir6
7 Dir7

当.Where(t => t.y.ancestor == 3)
结果为:

id dirname
3 Dir3
6 Dir6

方法2

数据结构样式

创建表:tree

Id parent title lft rgt
1 Food 1 18
2 Food Fruit 2 11
3 Fruit Red 3 6
4 Red Cherry 4 5
5 Fruit Yellow 7 10
6 Yellow Banana 8 9
7 Food Meat 12 17
8 Meat Beef 13 14
9 Meat Pork 15 16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var ii = _connection.Table<Item>();
ii.ToObservable()
.Where(x=>x.lft>2&&x.lft<11)
.Do(x => Debug.Log(x.ToString()))
.Subscribe();
public class Item
{
[PrimaryKey,AutoIncrement]
public int Id { get; set; }
public string parent { get; set; }
public string title { get; set; }
public int lft { get; set; }
public int rgt { get; set; }
public override string ToString()
{
return string.Format("[Item: parent={0}, title={1}, lft={2}, rgt={3}]", parent, title, lft, rgt);
}
}

方法3

确实没有想到sql做成树状结构会这么繁琐,我的想法是物理结构就是tables list,逻辑结构可以通过xml来管理。

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
runtime data config
|
Player |
+---+World |
| +----+Le^el |
| | +---+grid1 |
| | | +---+curItems+-----------+ |
| | | | | |
| | | +---+monster +-------------+ |
| | | | | | | +-+item
| | | +---+room1,2,3...+->Xml +---->Items tables+-+
| | +---+grid2 | | | +-+equipment
| | | +---+curItems+-----------+ | |
| | | | +-->Monster tables+-+player
| | | +---+monster +-------------+ | |
| | | | | +-+creature
| | | +---+room1,2,3...+->Xml |
| | +---+... |
| +----+Fam |
| |
+---+Other |
|
|
|


多重流合并操作
CombineLatest

1
2
3
4
5
6
7
8
9
10
void Start()
{
var sourceA = Observable.Return(5);
var sourceB = Observable.Return("b");
var sourceC = Observable.Return(0.2f);
var joined =
Observable.CombineLatest(sourceA, sourceB, sourceC, (a, b, c) => new {A = a, B = b, C = c})
.Do(t => Debug.Log(t.A + t.B + t.C))
.Subscribe();
}

Select

1
2
3
4
5
6
7
8
9
10
11
12
void Start()
{
var sourceA = Observable.Return(5);
var sourceB = Observable.Return("b");
var sourceC = Observable.Return(0.2f);
var query =
sourceA.Select(s1 => sourceB.Select(s2 => sourceC.Select(s3 => new {s1, s2, s3})))
.Switch()
.SelectMany(t => t)
.Do(t => Debug.Log(t.s1 + t.s2 + t.s3))
.Subscribe();
}

Zip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void tt()
{
var aa = new string[] {"a", "b", "c", "d"};
var bb = new int[] {1, 2, 3, 4, 5};
var cc = new float[] {0.1f, 0.2f, 0.3f, 0.4f};
var a = aa.ToObservable();
var b = bb.ToObservable();
var c = cc.ToObservable();
a.Zip(b, (lhs, rhs) => new {left = lhs, right = rhs,})
.Do(t => Debug.Log(t.left + t.right))
.Subscribe();
}

subject

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var streams = new Subject<IObservable<int>>();
var mergedStreams = streams.Merge();
var mergedObserver = mergedStreams.Subscribe(...);
// now create your streams
...
// add them to the streams subject
streams.OnNext(seq1);
streams.OnNext(seq2);
...
streams.OnNext(seq3);
streams.OnNext(seq4);
...
// If we know there will be no more streams, tell the Subject...
streams.OnCompleted();