首页 > 解决方案 > 不是从第一个索引开始 Array2D

问题描述

我基于二维数组制作了一个网格。稍后将有一艘在网格上移动的船。我想确定船的 X 和 Y 位置。我的问题是,如何让船从 [0.0] 开始,如下所示。因为索引数组已关闭。

我想显示我的船位置.. [XY] 但它应该从 0,0 而不是从 0,8 开始

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections.Generic;
using System.IO;
using UnityEngine.UI;
using System;



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 Canvas canvas;
    
    public RectTransform shipvalue;
    
    public RectTransform newpos;
    RectTransform currentposition;

    public ShipData currentShipData;
    public ShipData JSONSHIP;
    //public RectTransform windspeedvalue;
    //public RectTransform temperaturevalue;
    //public RectTransform depthvalue;

    public ShipData UpdateJSON;


    //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}};  
  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;
        Vector3 myVector;
    
        float zvalue;
    
        [Serializable]
        public class ShipData
        {
    
            public Vector2Int position;
            public float depth;
            public float windspeed;
            public float temperature;
            public float flow;
    
        }
    
      
    
        [SerializeField]private ShipData JSON_ShipData = new ShipData();
        
    
        public void SaveintoJson()
        {
            
            string data = JsonUtility.ToJson(JSON_ShipData, true);
            System.IO.File.AppendAllText(Application.persistentDataPath + "/DataCenter.json", data);
           
            //Debug.Log(data);
        }
    
        void Start()
        {
            p1 = 0;
            p2 = 8;
            
            grid[p1, p2] = 1;
            panelhole.SetActive(false);
            panelturtle.SetActive(false);
    
            //Debug.Log(grid[p1, p2]);
            //Debug.Log(shiposition.transform.position);
           
        }
    
    
    
        private bool TryGetPosition(int[,] grid, out Vector2Int position)
        {
            position = default;
    
            for (int i = 0; i < grid.GetLength(1); i++)
            {
                for (int j = 0; j < grid.GetLength(0); j++)
                {
                    if (grid[i, j] == 1)
                    {
                        position = new Vector2Int(i, j);
    
                        shiposition.text = $"X : {i} Y : {j}";
                        
                        return true;
                    }
                }
            }
    
            return false;
        }

标签: c#unity3d

解决方案


如果要从 开始0, 0,只需将变量 p2 更改为p2=0,因为现在是p2=8.

我希望它有帮助!


推荐阅读