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