4
|
1 using UnityEngine;
|
|
2 using UnityEditor;
|
|
3 using System.Collections;
|
|
4 using UnityEngine.SceneManagement;
|
7
|
5 using JungleDB;
|
8
|
6 using System.Text;
|
11
|
7 using Default = System.Collections.Generic;
|
4
|
8
|
|
9 public class SaveDataTest : MonoBehaviour {
|
6
|
10
|
8
|
11 public static SaveDataTest instance;
|
|
12
|
|
13 public SceneNode root;
|
|
14 public System.Collections.Generic.List<GameObject> objList;
|
|
15 public StageManager stageManager;
|
|
16
|
|
17 public static Jungle jungle;
|
|
18 private JungleTree tree;
|
|
19 private JungleTreeEditor editor;
|
|
20 private NodePath rootPath;
|
|
21
|
|
22 public delegate void Callback ();
|
|
23 public Callback callback;
|
|
24
|
|
25 /// <summary>
|
|
26 /// Jungleの木を構築し終わった時に実行するメソッドをセット
|
|
27 /// </summary>
|
|
28 public void AddCallback (Callback c) {
|
|
29 this.callback = c;
|
|
30 print ("Add : " + callback.Method);
|
|
31 }
|
|
32
|
|
33 private void initJungle () {
|
|
34 jungle = new DefaultJungle (null, "Game", new DefaultTreeEditor(new DefaultTraverser()));
|
|
35 tree = jungle.createNewTree ("Scene");
|
|
36 rootPath = new DefaultNodePath ();
|
|
37 // 編集を可能にする
|
|
38 editor = tree.getTreeEditor ();
|
9
|
39 // editor = editor.addNewChildAt (rootPath, 0).b();
|
8
|
40 }
|
|
41
|
|
42 private void Awake () {
|
|
43 if (instance == null)
|
|
44 instance = this;
|
|
45 }
|
4
|
46
|
|
47 private void Start () {
|
8
|
48 initJungle ();
|
|
49 CreateSceneTree ();
|
|
50 CreateItemTree ();
|
|
51 }
|
|
52
|
|
53 private void CreateTree () {
|
|
54 GameObject rootObj = new GameObject ();
|
|
55 rootObj.name = "Root";
|
|
56 root = new SceneNode(rootObj, null);
|
4
|
57 }
|
|
58
|
|
59 private void Check () {
|
|
60 int i = 0;
|
|
61 foreach (GameObject obj in UnityEngine.Resources.FindObjectsOfTypeAll(typeof(GameObject))) {
|
|
62 string path = AssetDatabase.GetAssetOrScenePath (obj);
|
|
63 bool isScene = path.Contains (".unity");
|
|
64 if (isScene) { // このシーンの中のオブジェクトかどうか。
|
|
65 if (obj.transform.childCount == 0 && obj.transform.parent == null || obj.transform.childCount > 0 && obj.transform.parent == null) { // 親を取得
|
8
|
66 objList.Add(obj);
|
4
|
67 i++;
|
|
68 }
|
|
69 }
|
|
70 }
|
8
|
71
|
|
72 foreach (var obj in objList) {
|
|
73 SceneNode node = new SceneNode (obj, root);
|
|
74 root.AddChild (node);
|
|
75 CheckChild (obj.transform, node);
|
|
76 }
|
|
77 }
|
|
78
|
|
79 private void CheckChild (Transform obj, SceneNode parent) {
|
|
80 if (obj.childCount == 0)
|
|
81 return;
|
|
82
|
|
83 for (int i = 0; i < obj.childCount; i++) {
|
|
84 SceneNode node = new SceneNode (obj.GetChild (i).gameObject, parent);
|
|
85 parent.AddChild (node);
|
|
86 CheckChild (obj.GetChild (i), node);
|
|
87 }
|
|
88 }
|
|
89
|
|
90 private bool CreateNode (NodePath path, int num) {
|
|
91 Either<Error, JungleTreeEditor> e = editor.addNewChildAt (path, num);
|
|
92 if (e.isA ()) {
|
|
93 e.a ();
|
|
94 return false;
|
|
95 }
|
|
96 editor = e.b ();
|
|
97 return true;
|
|
98 }
|
|
99
|
|
100 private bool SetAttribute (NodePath path, GameObject obj) {
|
|
101 // Either<Error, TreeEditor> e = editor.putAttribute (path, key, Encoding.UTF8.GetBytes(val));
|
|
102 Either<Error, JungleTreeEditor> e = editor.putAttribute (path, "NodeName", Encoding.UTF8.GetBytes(obj.name));
|
|
103 if (e.isA ()) {
|
|
104 e.a ();
|
|
105 return false;
|
|
106 }
|
|
107 editor = e.b ();
|
|
108 return true;
|
|
109 }
|
|
110
|
|
111 private void CreateItemTree () {
|
|
112 JungleTree tree = jungle.createNewTree ("ItemTree");
|
|
113 JungleTreeEditor edt = tree.getTreeEditor ();
|
|
114
|
11
|
115
|
|
116 Either<Error, JungleTreeEditor> e = edt.putAttribute (rootPath, "TreeName", "Item");
|
8
|
117
|
11
|
118 System.Func<JungleTreeEditor, JungleTreeEditor> f = (JungleTreeEditor arg) => {
|
|
119 edt = arg;
|
|
120 return edt;
|
|
121 };
|
8
|
122
|
11
|
123 e.fmap (f, edt.addNewChildAt (rootPath, 0));
|
|
124 e.fmap (f, edt.putAttribute ("Category", "Box"));
|
|
125
|
|
126 e.fmap (f, edt.addNewChildAt (rootPath, 1));
|
|
127 e.fmap (f, edt.putAttribute ("Category", "Food"));
|
8
|
128
|
|
129 NodePath path = rootPath.add(0);
|
|
130
|
11
|
131 Default.List<ItemInfo> infoList = new Default.List<ItemInfo> ();
|
|
132 infoList.Add (new ItemInfo (1, 2, "Grass", "#019540FF"));
|
|
133 infoList.Add (new ItemInfo (2, 4, "Wood", "#7F3C01FF"));
|
|
134 infoList.Add (new ItemInfo (3, 1, "Sand", "#D4500EFF"));
|
|
135 infoList.Add (new ItemInfo (4, 5, "Water", "#2432ADFF"));
|
8
|
136
|
11
|
137 int i = 0;
|
|
138 foreach (var info in infoList) {
|
|
139 e.fmap (f, edt.addNewChildAt (path, i));
|
|
140 e.fmap (f, edt.putAttribute (info));
|
|
141 i++;
|
|
142 }
|
8
|
143
|
|
144 edt.commit ();
|
|
145
|
|
146 StageManager.Instance.Init ();
|
|
147 }
|
|
148
|
|
149 private void CreateSceneTree () {
|
|
150 JungleTree tree = jungle.createNewTree ("SceneTree");
|
|
151 JungleTreeEditor edt = tree.getTreeEditor ();
|
|
152
|
|
153 edt = edt.addNewChildAt (rootPath, 0).b ();
|
10
|
154 edt = edt.putAttribute ("NodeName", "Player").b ();
|
8
|
155
|
|
156 NodePath playerpath = rootPath.add (0);
|
|
157
|
|
158 edt = edt.addNewChildAt (playerpath, 0).b();
|
10
|
159 edt = edt.putAttribute ("NodeName", "HaveItems").b();
|
8
|
160
|
|
161 edt = edt.addNewChildAt (rootPath, 1).b ();
|
10
|
162 edt = edt.putAttribute ("NodeName", "Stage").b ();
|
8
|
163
|
|
164 edt = edt.commit ().b();
|
4
|
165 }
|
|
166 }
|