void LoadJson() { var chars = Resources.Load("Jsons/chars") as TextAsset; var charsTree = Resources.Load("Jsons/chars_tree") as TextAsset; var charJson = JsonUtility.FromJson(chars.text, typeof(CharJson)) as CharJson; var charTree = JsonConvert.DeserializeObject <CharTree>(charsTree.text); // var charTree = JsonUtility.FromJson(charsTree.text, typeof(CharTree)) as CharTree; foreach (var i in charJson.basicChar) { basicCharIds.Add(i); } foreach (var i in charJson.data) { characterDict.Add(i.id, i.character); characterRevDict.Add(i.character, i.id); // !!! 必须手动给到 charId i.attackInfo.charId = i.id; charaterAttackInfoDict.Add(i.id, i.attackInfo); } foreach (var i in charTree.data) { var node = new CharTreeNode(); characterTreeRootDict.Add(i.keyId, node); // build tree BuildTree(node, i); } Debug.Log("Loading characters finished"); }
void BuildTree(CharTreeNode root, CharTreeItem rootItem) { root.id = rootItem.keyId; root.toId = rootItem.toId; if (rootItem.nextItem != null) { foreach (var i in rootItem.nextItem) { CharTreeNode node = new CharTreeNode(i.keyId, i.toId); root.nextNodes.Add(node); BuildTree(node, i); } } }
void _Traverse(CharTreeNode root, List <string> strs) { strs.Add(characterDict[root.id]); if (root.toId != null) { string str = ""; strs.ForEach((s) => { str += s; }); str += " -> " + characterDict[root.toId.Value]; Debug.Log(str); idToStringsDict[root.toId.Value] = strs; } foreach (var i in root.nextNodes) { List <string> ss = new List <string>(strs); _Traverse(i, ss); } }
int?SearchTreeByIds(CharTreeNode node, List <int> ids, int index = 0) { if (index >= ids.Count) { return(null); } if (index == ids.Count - 1) { // 如果是最后一个id return(node.toId); } foreach (var i in node.nextNodes) { if (i.id == ids[index + 1]) { var res = SearchTreeByIds(i, ids, index + 1); return(res); } } return(null); }