c# - 不是从第一个索引开始 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;
}
解决方案
如果要从 开始0, 0
,只需将变量 p2 更改为p2=0
,因为现在是p2=8
.
我希望它有帮助!
推荐阅读
- html - 无法在元素上应用 css z-index 属性
- javascript - 带有 XML 文件和自定义标记的 Google 地图无法在移动设备上加载
- linux - 带有 arm-linux-gnueabihf-gcc 的无法识别的命令行选项“-fstack-protector-strong”
- mongodb - FreeBSD:将 MongoDB 服务器从 2.6 升级到 3.0
- java - 限制谷歌视觉中的检测区域,文本识别
- python-3.x - ImportError:无法从“bs4.element”导入名称“nonwhitespace_re”
- java - 使用通配符搜索字符串
- javascript - 如何更改 Material UI 字体的颜色?
- php - Ajax 到 PHP 循环问题
- c# - 我正在尝试使用属性和字符串填充列表