Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将2D数组保存为JSON_C#_Unity3d - Fatal编程技术网

C# 将2D数组保存为JSON

C# 将2D数组保存为JSON,c#,unity3d,C#,Unity3d,我无法将模拟保存到JSON文件中。 我做一艘移动的船的基础。在世界空间将有一个探测器按钮,它将提供有关飞船的信息。我想将这些信息保存到JSON中。 任何人都知道怎么做。我是新来的。 在按钮()中,我想保存船舶信息(如位置、深度、风速、温度、流量) public class test : MonoBehaviour { // Start is called before the first frame update public GameObject Ship; publ

我无法将模拟保存到JSON文件中。 我做一艘移动的船的基础。在世界空间将有一个探测器按钮,它将提供有关飞船的信息。我想将这些信息保存到JSON中。 任何人都知道怎么做。我是新来的。 在按钮()中,我想保存船舶信息(如位置、深度、风速、温度、流量)

public class test : MonoBehaviour
{
    // Start is called before the first frame update

    public GameObject Ship;
    public GameObject hole;
    public GameObject turtle;
    public GameObject panelhole;
    public GameObject panelturtle;
    
    public RectTransform shipvalue;
    //public RectTransform flowvalue;
    //public RectTransform windspeedvalue;
    //public RectTransform temperaturevalue;
    //public RectTransform depthvalue;
    public Vector3 offset;
    public RectTransform Basicobject; //parent



    //public static bool captureAllKeyboardInput = false;

    private bool paneloff = false;
    public float duration = 1;

    public int[,] grid = new int[10, 16];
    public float[,] depth = new float[4, 3]
    {{1.6f, 2.3f, 3.5f },
     {4, 5, 6.5f},
     {7, 8, 6.5f},
     {7, 8, 6.5f}};
 
    public float[,] temperaturedata = new float[10, 16]
    {{0,    0,      0,      0,      0,      0,      0,      22.5f,  22.7f,  23,     23.9f,  24,     26.3f,  26.4f,  26.4f,  26.3f},
    {0,     0,      0,      0,      0,      0,      22.8f,  23.2f,  23.8f,  24.4f,  25,     24.3f,  26.5f,  26.5f,  26.5f,  26.6f},
    {0,     0,      0,      0,      0,      22.5f,  23.1f,  24.8f,  25.3f,  25.7f,  0,      0,      26.7f,  26.3f,  26.2f,  26.6f},
    {0,     0,      0,      0,      23.2f,  23.8f,  25.1f,  25.4f,  25.9f,  0,      0,      26.8f,  26.9f,  26.5f,  26.3f,  26.3f},
    {0,     0,      24.5f,  23.3f,  23.9f,  24.5f,  25.7f,  25.6f,  26.8f,  0,      0,      26.9f,  27.1f,  26.6f,  26.4f,  26.4f},
    {0,     24.1f,  23.9f,  24.9f,  25.4f,  25.5f,  25.9f,  27.4f,  27.2f,  0,      0,      27,     27.2f,  26.8f,  26.4f,  26   },
    {27.4f, 27.7f,  27.3f,  26.2f,  26.2f,  25.9f,  27.5f,  27.7f,  27.3f,  0,      26.8f,  27.2f,  27.2f,  26.9f,  26.4f,  26.2f},
    {28.5f, 29,     27.5f,  27.3f,  27.3f,  27.5f,  27.7f,  27.7f,  27.5f,  27.2f,  27.2f,  27.4f,  27.4f,  26.9f,  26.3f,  26.7f},
    {28.5f, 27.6f,  27.1f,  27,     26.5f,  27.6f,  27.6f,  27.6f,  27.7f,  27.4f,  27.8f,  27.7f,  27.7f,  27,     27,     26.6f},
    {28.5f, 27.6f,  25,     27.3f,  26.8f,  27.8f,  27.3f,  27.5f,  28.1f,  27.9f,  28,     27.6f,  27.7f,  26.9f,  27.1f,  26.8f}};


    public float[,] flowdata = new float[10, 16]
 
    {{0,    0,      0,      0,      0,      0,      0,      0.4f,   0.4f,   0.6f,   0.8f,   0.6f,   0.7f,   0.4f,   0.4f,   0.4f},
    {0,     0,      0,      0,      0,      0,      0.3f,   0.5f,   0.5f,   0.8f,   0.8f,   0.8f,   0.6f,   0.6f,   0.5f,   0.5f},
    {0,     0,      0,      0,      0,      0.4f,   0.7f,   0.7f,   0.7f,   0.7f,   0,      0,      0.9f,   0.6f,   0.4f,   0.4f},
    {0,     0,      0,      0,      0.5f,   0.5f,   0.6f,   0.7f,   0.6f,   0,      0,      0.8f,   0.8f,   0.4f,   0.3f,   0.3f},
    {0,     0,      000,    0.7f,   0.6f,   0.5f,   0.7f,   1,      0.8f,   0,      0,      0.9f,   0.5f,   0.3f,   0.1f,   0.3f},
    {0,     0.5f,   0.7f,   0.6f,   0.8f,   0.8f,   1.3f,   0.9f,   0.5f,   0,      0,      0.8f,   0.3f,   0.1f,   0.2f,   0.2f},
    {0.6f,  0.6f,   0.6f,   0.7f,   1.1f,   0.9f,   0.8f,   0.4f,   0.3f,   0,      0.9f,   0.6f,   0.2f,   0.2f,   0.2f,   0.2f},
    {0.4f,  0.4f,   0.5f,   0.5f,   0.3f,   0.4f,   0.3f,   0.2f,   0.4f,   0.2f,   0.8f,   0.3f,   0.2f,   0.2f,   0.2f,   0.1f},
    {000,   0.3f,   0.5f,   0.2f,   0.2f,   0.2f,   0.2f,   0.1f,   0.2f,   0.6f,   0.6f,   0.3f,   0.3f,   0.2f,   0.2f,   0.2f},
    {000,   000,    0.1f,   0.4f,   0.3f,   0.3f,   0.2f,   0.2f,   0.1f,   0.2f,   0.3f,   0.1f,   0.2f,   0.2f,   0.2f,   0.2f}};

    public float[,] windspeeddata = new float[10, 12]
    {{0,0,0,0,0,0,0,4,4,4,3,3},
    {0,0,0,0,0,0,4,5,4,3,3,3},
    {0,0,0,0,0,5,5,5,5,3,0,0},
    {0,0,0,0,4,4,4,4,4,0,0,3},
    {0,0,3,0,4,4,4,4,4,0,0,3},
    {0,4,4,4,4,4,4,4,3,0,0,3},
    {4,4,4,4,4,4,4,3,3,0,3,4},
    {5,4,4,4,4,4,3,3,3,3,4,4},
    {4,4,4,4,4,4,3,3,3,3,4,4},
    {5,4,4,4,4,4,4,4,4,3,3,3}};


    int row, column, num1, num2;
    int p1;
    int p2;

    int[] grid2 = new int[5];
    public Text shiposition = null;
    public Text depthtext = null;
    public Text windspeedtext = null;
    public Text temperaturetext = null;
    public Text flowtext = null;

    //direction = Ship.transform.rotation.z;
    float LastMove;
    float timeIn = 0.5f;
    public Vector3 direction;

    float zvalue;

    [Serializable]
    public class ShipData
    {

        public int grid;
        public float depth;
        public float windspeed;
        public float temperature;

    }

    [SerializeField]
    private ShipData JSON_ShipData = new ShipData();

    public void SaveintoJson()
    {
        string data = JsonUtility.ToJson(JSON_ShipData);
        System.IO.File.WriteAllText(Application.persistentDataPath + "/DataCenter.json", data);
        Debug.Log("Saving as JSON" + JSON_ShipData);
        Debug.Log(Application.persistentDataPath);
    }

    void Start()
    {
        p1 = 9;
        p2 = 0;
        
        grid[p1, p2] = 1;
        panelhole.SetActive(false);
        panelturtle.SetActive(false);
        Debug.Log(grid[p1, p2]);
        Debug.Log(shiposition.transform.position);
       
    }
    // Update is called once per frame




    void Update()
    {
        
        //Debug.Log(Ship.transform.localEulerAngles.z);
        zvalue = Ship.transform.localEulerAngles.z;

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {

            StartCoroutine(Forward());
       }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            StartCoroutine(Backward());
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            StartCoroutine(Rotate(Vector3.forward, -90, 1.0f));
            Debug.Log(transform.rotation.eulerAngles.z);
        }
       if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            StartCoroutine(Rotate(Vector3.forward, 90, 1.0f));
            Debug.Log(transform.rotation.eulerAngles.z);
        }
         

        if (Time.time - LastMove > timeIn)
        {
            LastMove = Time.time;
           
        }

        if (Input.GetKeyDown(KeyCode.W))
        {
            
                StartCoroutine(Rotate(Vector3.forward, 90, 1.0f));
               // position(grid);
               // depthsea(depth);
           
        }
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (p2 >= 0)
            {
                StartCoroutine(Backward());
                grid[p1, p2] = 0;
                grid[p1, --p2] = 1;
                //position(grid);
                //depthsea(depth);
            }
            else
            {
                Debug.Log("You can't move left!!");
            }
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            
            // z = 0
            if (p2 <= 12 && zvalue == 0)
            {
                StartCoroutine(Forward());
                grid[p1, p2] = 0;
                grid[p1, ++p2] = 1;
                //position(grid);
                //depthsea(depth);
                Debug.Log(zvalue);
            }
            // z = 270
            else if (p2 <= 12 && zvalue == 270)
            {
                StartCoroutine(Forward());
                grid[p1, p2] = 0;
                grid[++p1, p2] = 1;
               // position(grid);
                //depthsea(depth);
                Debug.Log(zvalue);
            }

            //// z = 180
            else if (p2 <= 12 && zvalue == 180)
            {
                StartCoroutine(Forward());
                grid[p1, p2] = 0;
                grid[p1, --p2] = 1;
               // position(grid);
                //depthsea(depth);
                Debug.Log(zvalue);
            }
            //// z = 90
            else if (p2 <= 12 && zvalue == 90)
            {
                StartCoroutine(Forward());
                grid[p1, p2] = 0;
                grid[--p1, p2] = 1;
               // position(grid);
                //depthsea(depth);
                Debug.Log(zvalue);
            }
            else
            {
                Debug.Log("You can't move right any further!!");
                Debug.Log(Ship.transform.localEulerAngles.z);
            }
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
           
                StartCoroutine(Rotate(Vector3.forward, -90, 1.0f));
                //position(grid);
                //depthsea(depth);
           
        }
        //WebGLInput.captureAllKeyboardInput = false;
    }

    
    private void position(int[,] grid)
    {

        for (int i = 0; i < grid.GetLength(0); i++)
        {
            for (int j = 0; j < grid.GetLength(1); j++)
            {
                if (grid[i, j] == 1)
                {
                   // Debug.Log("x: " + i + " y: " + j + " Grid: " + grid[i, j]);
                   
                    shiposition.text = "X : " + i + " " + "Y : " + j;
                    shiposition.text.ToString();
                    PlayerPrefs.SetString("position", shiposition.text);
                    PlayerPrefs.Save();
                    Debug.Log(shiposition.text);
                    
                }

            }
        }
    }

    public void windspeed(float[,] windspeeddata)
    {
        for (int x = 0; x < windspeeddata.GetLength(0); x++)
        {
            for (int y = 0; y < windspeeddata.GetLength(1); y++)
            {
                if (grid[x, y] == 1)
                {

                    windspeedtext.text = "Windspeed Level :" + windspeeddata[x, y];
                   
                }

            }
        }

    }


    public void temperature(float[,] temperaturedata)
    {
        for (int x = 0; x < temperaturedata.GetLength(0); x++)
        {
            for (int y = 0; y < temperaturedata.GetLength(1); y++)
            {
                if (grid[x, y] == 1)
                {
                    //Debug.Log(temperaturedata[x, y]);
                    temperaturetext.text = "Temperature :" + temperaturedata[x, y] + "C";
                }

            }
        }
    }

    public void flow(float[,] flowdata)
    {
        for (int x = 0; x < flowdata.GetLength(0); x++)
        {
            for (int y = 0; y < flowdata.GetLength(1); y++)
            {
                if (grid[x, y] == 1)
                {

                    flowtext.text = "Flow :" + flowdata[x, y];
                }

            }
        }
    }

    public void depthsea(float[,] depth)
    {
        for (int x = 0; x < depth.GetLength(0); x++)
        {
            for (int y = 0; y < depth.GetLength(1); y++)
            {
                if (grid[x, y] == 1)
                {
                    depthtext.text = "Depth :" + depth[x, y];
                    Debug.Log(depth[x, y]);
                }
       
            }
        }
    }

    public void moveobject()
    {
        
        shipvalue.transform.position = Ship.transform.position;
        //shipvalue.transform.position.y + 0.5f;
        //flowvalue.transform.position = Ship.transform.position;
        //windspeedvalue.transform.position = Ship.transform.position;
        //temperaturevalue.transform.position = Ship.transform.position;
        //depthvalue.transform.position = Ship.transform.position;

    }
    
    public void button()
    {
        
        position(grid);
        depthsea(depth);
        windspeed(windspeeddata);
        temperature(temperaturedata);
        flow(flowdata);
        moveobject();
     

        //value.transform.position = Ship.transform.position;
        //string newposition = JsonUtility.ToJson(shiposition.text);
        //System.IO.File.WriteAllText(Application.persistentDataPath + "PositionData.json", newposition);
        //Debug.Log(shiposition.text);
    }
公共类测试:单一行为
{
//在第一帧更新之前调用Start
公共游戏对象船;
公共游戏对象洞;
公开游戏对象海龟;
公共游戏对象面板洞;
公共游戏对象;
公共价值;
//公共价值;
//公共风速值;
//公共温度值;
//公共价值;
公共矢量3偏移量;
公共RectTransform Basicobject;//父对象
//公共静态bool captureAllKeyboardInput=false;
private bool paneloff=假;
公共浮动持续时间=1;
公共整数[,]网格=新整数[10,16];
公共浮动[,]深度=新浮动[4,3]
{{1.6f,2.3f,3.5f},
{4,5,6.5f},
{7,8,6.5f},
{7,8,6.5f};
公开浮动[,]温度数据=新浮动[10,16]
{{0,0,0,0,0,0,0,22.5f,22.7f,23,23.9f,24,26.3f,26.4f,26.4f,26.3f},
{0,0,0,0,0,22.8f,23.2f,23.8f,24.4f,25,24.3f,26.5f,26.5f,26.5f,26.6f},
{0,0,0,0,22.5f,23.1f,24.8f,25.3f,25.7f,0,0,26.7f,26.3f,26.2f,26.6f},
{0,0,0,0,23.2f,23.8f,25.1f,25.4f,25.9f,0,0,26.8f,26.9f,26.5f,26.3f,26.3f},
{0,0,24.5f,23.3f,23.9f,24.5f,25.7f,25.6f,26.8f,0,0,26.9f,27.1f,26.6f,26.4f,26.4f},
{0,24.1f,23.9f,24.9f,25.4f,25.5f,25.9f,27.4f,27.2f,0,0,27,27,27.2f,26.8f,26.4f,26},
{27.4f、27.7f、27.3f、26.2f、26.2f、25.9f、27.5f、27.7f、27.3f、0、26.8f、27.2f、27.2f、27.2f、26.9f、26.4f、26.2f},
{28.5f,29,27.5f,27.3f,27.3f,27.5f,27.7f,27.5f,27.2f,27.2f,27.2f,27.4f,27.4f,26.9f,26.3f,26.7f},
{28.5f、27.6f、27.1f、27、26.5f、27.6f、27.6f、27.7f、27.4f、27.8f、27.7f、27.7f、27.7f、27、27、26.6f},
{28.5f、27.6f、25、27.3f、26.8f、27.8f、27.3f、27.5f、28.1f、27.9f、28、27.6f、27.7f、26.9f、27.1f、26.8f};
公共浮动[,]flowdata=新浮动[10,16]
{{0,0,0,0,0,0,0,0,0,0.4f,0.4f,0.6f,0.8f,0.6f,0.7f,0.4f,0.4f,0.4f},
{0,0,0,0,0,0,0.3f,0.5f,0.5f,0.8f,0.8f,0.8f,0.6f,0.6f,0.5f,0.5f},
{0,0,0,0,0,0.4f,0.7f,0.7f,0.7f,0.7f,0,0,0.9f,0.6f,0.4f,0.4f},
{0,0,0,0,0.5f,0.5f,0.6f,0.7f,0.6f,0,0,0.8f,0.8f,0.4f,0.3f,0.3f},
{0,0,000,0.7f,0.6f,0.5f,0.7f,1,0.8f,0,0,0.9f,0.5f,0.3f,0.1f,0.3f},
{0,0.5f,0.7f,0.6f,0.8f,0.8f,1.3f,0.9f,0.5f,0,0.8f,0.3f,0.1f,0.2f,0.2f},
{0.6f、0.6f、0.6f、0.7f、1.1f、0.9f、0.8f、0.4f、0.3f、0、0.9f、0.6f、0.2f、0.2f、0.2f、0.2f},
{0.4f,0.4f,0.5f,0.5f,0.3f,0.4f,0.2f,0.4f,0.2f,0.8f,0.3f,0.2f,0.2f,0.2f,0.1f},
{000,0.3f,0.5f,0.2f,0.2f,0.2f,0.2f,0.1f,0.2f,0.6f,0.6f,0.3f,0.3f,0.2f,0.2f,0.2f},
{000000,0.1f,0.4f,0.3f,0.3f,0.2f,0.2f,0.1f,0.2f,0.3f,0.1f,0.2f,0.2f,0.2f};
公共浮动[,]windspeeddata=新浮动[10,12]
{{0,0,0,0,0,0,0,4,4,4,3,3},
{0,0,0,0,0,0,4,5,4,3,3,3},
{0,0,0,0,0,5,5,5,5,3,0,0},
{0,0,0,0,4,4,4,4,4,0,0,3},
{0,0,3,0,4,4,4,4,4,0,0,3},
{0,4,4,4,4,4,4,4,3,0,0,3},
{4,4,4,4,4,4,4,3,3,0,3,4},
{5,4,4,4,4,4,3,3,3,3,4,4},
{4,4,4,4,4,4,3,3,3,3,4,4},
{5,4,4,4,4,4,4,4,4,3,3,3}};
int行,列,num1,num2;
int-p1;
int p2;
int[]grid2=新的int[5];
公共文本shiposition=null;
公共文本深度文本=null;
公共文本windspeedtext=null;
公共文本temperaturetext=null;
公共文本flowtext=null;
//方向=Ship.transform.rotation.z;
浮动最后移动;
浮动时间=0.5f;
公共向量3方向;
浮点数;
[可序列化]
公共类船舶数据
{
公共int网格;
公众漂浮深度;
公众浮标风速;
公众浮标温度;
}
[序列化字段]
私有ShipData JSON_ShipData=新ShipData();
public void SaveintoJson()
{
string data=JsonUtility.ToJson(JSON_ShipData);
System.IO.File.WriteAllText(Application.persistentDataPath+“/DataCenter.json”,数据);
Log(“另存为JSON”+JSON_ShipData);
Log(Application.persistentDataPath);
}
void Start()
{
p1=9;
p2=0;
网格[p1,p2]=1;
panelhole.SetActive(假);
panelturtle.SetActive(false);
Log(网格[p1,p2]);
Log(shiposition.transform.position);
}
//每帧调用一次更新
无效更新()
{
//Log(Ship.transform.localEulerAngles.z);
zvalue=Ship.transform.localEulerAngles.z;
if(Input.GetKeyDown(KeyCode.RightArrow))
{
start例程(Forward());
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
start例程(Backward());
}
if(Input.GetKeyDown(KeyCode.DownArrow))