- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class TaskNode
- {
- private string taskNodeName;
- private string taskParams;
- private List
taskNodeList; -
- public TaskNode(string taskNodeName, string taskParams, List
taskNodeList=null ) - {
- this.taskNodeName = taskNodeName;
- this.taskParams = taskParams;
- this.taskNodeList = taskNodeList;
- }
-
- public string TaskNodeName { get => taskNodeName; set => taskNodeName = value; }
- public string TaskParams { get => taskParams; set => taskParams = value; }
- public List
TaskNodeList { get => taskNodeList; set => taskNodeList = value; } - }
- public class JsonTestHandle : MonoBehaviour
- {
- private TaskNode taskNode;
- // Start is called before the first frame update
- void Start()
- {
- //taskNode = new TaskNode("hexin", "0");
- //List
taskNodes = new List(); - //TaskNode taskNode1 = new TaskNode("hexin1", "1", new List
{ new TaskNode("小明", "2"), new TaskNode("小兰", "3") }); - //TaskNode taskNode2 = new TaskNode("hexin2", "1", new List
{ new TaskNode("张三", "2") }); - //taskNodes.Add(taskNode1);
- //taskNodes.Add(taskNode2);
- //taskNode.TaskNodeList = taskNodes;
- //taskNode.TaskNodeList.Add(new TaskNode("李四", "0", new List
{ new TaskNode("小明", "2"), new TaskNode("小兰", "3") })); - //taskNode.TaskNodeList.Add(
- // new TaskNode("王五", "0", new List
{ - // new TaskNode("王五的孩子", "1", new List
{ - // new TaskNode("王五的孙子", "2", new List
{ - // new TaskNode("小明", "2"), new TaskNode("小兰", "3")
- // })
- // })
- // })
- // );
- //taskNode.TaskNodeList.Add(
- // new TaskNode("王五", "0", new List
{ - // new TaskNode("小明", "1", new List
{ - // new TaskNode("王五的孙子", "2", new List
{ - // new TaskNode("小明", "2"), new TaskNode("小兰", "3")
- // })
- // })
- // })
- // ); taskNode.TaskNodeList.Add(
- // new TaskNode("小明", "0", new List
{ - // new TaskNode("小明", "1", new List
{ - // new TaskNode("王五的孙子", "2", new List
{ - // new TaskNode("小明", "2"), new TaskNode("小兰", "3")
- // })
- // })
- // })
- // );
-
- //JsonTools.SaveJsonToLocal(taskNode, "localDatas");
- }
- private string tempNodeStr = "小明";
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.P))
- {
- TaskNode taskNode= JsonTools.FromFile
(Application.streamingAssetsPath + "/Json/localDatas.json"); -
- DeleteJsonNode(taskNode.TaskNodeList, tempNodeStr);
-
- Debug.Log("删除之后——:" + JsonTools.ToJson(taskNode));
- //JsonTools.SaveJsonToLocal(taskNode, "localDatas");
- }
- if (Input.GetKeyDown(KeyCode.L))
- {
- TaskNode taskNode = JsonTools.FromFile
(Application.streamingAssetsPath + "/Json/localDatas.json"); -
- TaskNode node = new TaskNode("小明1", "12345", new List
{ new TaskNode("何鑫", "1212") }); - ChangeJsonNode(taskNode.TaskNodeList, tempNodeStr,node);
-
- Debug.Log("更新之后——:" + JsonTools.ToJson(taskNode));
- }
- }
- ///
- /// 更新节点
- ///
- ///
- ///
- ///
- void ChangeJsonNode(List
taskNodeList, string nodeStr,TaskNode newTaskNode ) - {
- if (taskNodeList != null)
- {
- List
temp = taskNodeList; - for (int i = 0; i < temp.Count; i++)
- {
- if (temp[i].TaskNodeName.Equals(nodeStr))
- {
- temp[i] = newTaskNode;
- }
- else
- {
- if (temp[i].TaskNodeList != null)
- {
- List
temp1 = temp[i].TaskNodeList; - for (int j = 0; j < temp1.Count; j++)
- {
- if (temp1[j].TaskNodeName.Equals(nodeStr))
- {
- temp1[j] = newTaskNode;
- }
- else
- {
- ChangeJsonNode(temp1[j].TaskNodeList, nodeStr, newTaskNode);
- }
- }
- }
- }
- }
-
- }
- }
- ///
- /// 删除节点
- ///
- ///
- ///
- void DeleteJsonNode(List
taskNodeList, string nodeStr ) - {
- if (taskNodeList != null)
- {
- List
temp = taskNodeList; - for (int i = 0; i < temp.Count; i++)
- {
- if (temp[i].TaskNodeName.Equals(nodeStr))
- {
- taskNodeList.Remove(temp[i]);
- }
- }
- for (int i = 0; i < temp.Count; i++)
- {
- if (temp[i].TaskNodeList != null)
- {
- List
temp1 = temp[i].TaskNodeList; - for (int j = 0; j < temp1.Count; j++)
- {
- if (temp1[j].TaskNodeName.Equals(nodeStr))
- {
- temp1.Remove(temp1[j]);
- }
- }
- for (int j = 0; j < temp1.Count; j++)
- {
- DeleteJsonNode(temp1[j].TaskNodeList, nodeStr);
- }
- }
- }
- }
- }
- }