Mercurial > hg > Members > kazuma > JungleforUnity
view Assets/Application/Scripts/Test/SaveDataTest.cs @ 8:599bd8ddb72b
Create Item Tree and Create Stage.
author | Kazuma Takeda |
---|---|
date | Tue, 17 Jan 2017 19:57:19 +0900 |
parents | ad4729c5eec4 |
children | bbab930748c4 |
line wrap: on
line source
using UnityEngine; using UnityEditor; using System.Collections; using UnityEngine.SceneManagement; using JungleDB; using System.Text; public class SaveDataTest : MonoBehaviour { public static SaveDataTest instance; public SceneNode root; public System.Collections.Generic.List<GameObject> objList; public StageManager stageManager; public static Jungle jungle; private JungleTree tree; private JungleTreeEditor editor; private NodePath rootPath; public delegate void Callback (); public Callback callback; /// <summary> /// Jungleの木を構築し終わった時に実行するメソッドをセット /// </summary> public void AddCallback (Callback c) { this.callback = c; print ("Add : " + callback.Method); } private void initJungle () { jungle = new DefaultJungle (null, "Game", new DefaultTreeEditor(new DefaultTraverser())); tree = jungle.createNewTree ("Scene"); rootPath = new DefaultNodePath (); // 編集を可能にする editor = tree.getTreeEditor (); editor = editor.addNewChildAt (rootPath, 0).b(); } private void Awake () { if (instance == null) instance = this; } private void Start () { initJungle (); CreateSceneTree (); CreateItemTree (); } private void CreateTree () { GameObject rootObj = new GameObject (); rootObj.name = "Root"; root = new SceneNode(rootObj, null); } private void Check () { int i = 0; foreach (GameObject obj in UnityEngine.Resources.FindObjectsOfTypeAll(typeof(GameObject))) { string path = AssetDatabase.GetAssetOrScenePath (obj); bool isScene = path.Contains (".unity"); if (isScene) { // このシーンの中のオブジェクトかどうか。 if (obj.transform.childCount == 0 && obj.transform.parent == null || obj.transform.childCount > 0 && obj.transform.parent == null) { // 親を取得 objList.Add(obj); i++; } } } foreach (var obj in objList) { SceneNode node = new SceneNode (obj, root); root.AddChild (node); CheckChild (obj.transform, node); } } private void CheckChild (Transform obj, SceneNode parent) { if (obj.childCount == 0) return; for (int i = 0; i < obj.childCount; i++) { SceneNode node = new SceneNode (obj.GetChild (i).gameObject, parent); parent.AddChild (node); CheckChild (obj.GetChild (i), node); } } private bool CreateNode (NodePath path, int num) { Either<Error, JungleTreeEditor> e = editor.addNewChildAt (path, num); if (e.isA ()) { e.a (); return false; } editor = e.b (); return true; } private bool SetAttribute (NodePath path, GameObject obj) { // Either<Error, TreeEditor> e = editor.putAttribute (path, key, Encoding.UTF8.GetBytes(val)); Either<Error, JungleTreeEditor> e = editor.putAttribute (path, "NodeName", Encoding.UTF8.GetBytes(obj.name)); if (e.isA ()) { e.a (); return false; } editor = e.b (); return true; } private void CreateItemTree () { JungleTree tree = jungle.createNewTree ("ItemTree"); JungleTreeEditor edt = tree.getTreeEditor (); edt = edt.putAttribute (rootPath, "TreeName", Encoding.UTF8.GetBytes ("Item")).b (); edt = edt.addNewChildAt (rootPath, 0).b (); edt = edt.putAttribute (rootPath.add (0), "Category", Encoding.UTF8.GetBytes ("Box")).b (); edt = edt.addNewChildAt (rootPath, 1).b (); edt = edt.putAttribute (rootPath.add (1), "Category", Encoding.UTF8.GetBytes ("Food")).b (); NodePath path = rootPath.add(0); edt = edt.addNewChildAt (path, 0).b (); edt = edt.putAttribute (path.add (0), "ID", Encoding.UTF8.GetBytes ("1")).b (); edt = edt.putAttribute (path.add (0), "Type", Encoding.UTF8.GetBytes ("Grass")).b (); edt = edt.putAttribute (path.add (0), "Broken", Encoding.UTF8.GetBytes ("2")).b (); edt = edt.putAttribute (path.add (0), "Color", Encoding.UTF8.GetBytes ("#019540FF")).b (); edt = edt.addNewChildAt (path, 1).b (); edt = edt.putAttribute (path.add (1), "ID", Encoding.UTF8.GetBytes ("2")).b (); edt = edt.putAttribute (path.add (1), "Type", Encoding.UTF8.GetBytes ("Wood")).b (); edt = edt.putAttribute (path.add (1), "Broken", Encoding.UTF8.GetBytes ("4")).b (); edt = edt.putAttribute (path.add (1), "Color", Encoding.UTF8.GetBytes ("#7F3C01FF")).b (); edt = edt.addNewChildAt (path, 2).b (); edt = edt.putAttribute (path.add (2), "ID", Encoding.UTF8.GetBytes ("3")).b (); edt = edt.putAttribute (path.add (2), "Type", Encoding.UTF8.GetBytes ("Sand")).b (); edt = edt.putAttribute (path.add (2), "Broken", Encoding.UTF8.GetBytes ("1")).b (); edt = edt.putAttribute (path.add (2), "Color", Encoding.UTF8.GetBytes ("#D4500EFF")).b (); edt = edt.addNewChildAt (path, 3).b (); edt = edt.putAttribute (path.add (3), "ID", Encoding.UTF8.GetBytes ("4")).b (); edt = edt.putAttribute (path.add (3), "Type", Encoding.UTF8.GetBytes ("Water")).b (); edt = edt.putAttribute (path.add (3), "Broken", Encoding.UTF8.GetBytes ("5")).b (); edt = edt.putAttribute (path.add (3), "Color", Encoding.UTF8.GetBytes ("#2432ADFF")).b (); edt.commit (); StageManager.Instance.Init (); } private void CreateSceneTree () { JungleTree tree = jungle.createNewTree ("SceneTree"); JungleTreeEditor edt = tree.getTreeEditor (); edt = edt.addNewChildAt (rootPath, 0).b (); edt = edt.putAttribute (rootPath.add (0), "NodeName", Encoding.UTF8.GetBytes ("Player")).b (); NodePath playerpath = rootPath.add (0); edt = edt.addNewChildAt (playerpath, 0).b(); edt = edt.putAttribute (playerpath.add (0), "NodeName", Encoding.UTF8.GetBytes ("HaveItems")).b(); edt = edt.addNewChildAt (rootPath, 1).b (); edt = edt.putAttribute (rootPath.add (1), "NodeName", Encoding.UTF8.GetBytes ("Stage")).b (); edt = edt.commit ().b(); } }