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