Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////Grid.cs/////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
- public class Grid
- {
- private int width;
- private int height;
- private float cellSize;
- private Vector3 originPosition;
- private int[,] gridArray;
- private TextMesh[,] debugTextArray;
- public Grid (int width, int height, float cellSize, Vector3 originPosition) {
- this.width = width;
- this.height = height;
- this.cellSize = cellSize;
- this.originPosition = originPosition;
- gridArray = new int[width, height];
- debugTextArray = new TextMesh[width, height];
- for (int x = 0; x < gridArray.GetLength(0); x++) {
- for (int y = 0; y < gridArray.GetLength(1); y++) {
- debugTextArray[x,y] = CreateWorldText(null, gridArray[x, y].ToString(), GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 20, Color.white, TextAnchor.MiddleCenter, TextAlignment.Center);
- Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
- Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
- }
- }
- Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
- Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
- }
- private Vector3 GetWorldPosition(int x, int y) {
- return new Vector3(x, y) * cellSize + originPosition;
- }
- public void SetValue(int x, int y, int value) {
- if (x >= 0 && y >= 0 && x < width && y < height) {
- gridArray[x, y] = value;
- debugTextArray[x, y].text = gridArray[x, y].ToString();
- }
- }
- public void SetValue(Vector3 worldPosition, int value) {
- int x, y;
- GetXY(worldPosition, out x, out y);
- SetValue(x, y, value);
- }
- public int GetValue(int x, int y) {
- if (x >= 0 && y >= 0 && x < width && y < height) {
- return gridArray[x, y];
- } else {
- return 0;
- }
- }
- public int GetValue (Vector3 worldPosition) {
- int x, y;
- GetXY(worldPosition, out x, out y);
- return GetValue(x, y);
- }
- public void GetXY(Vector3 worldPosition, out int x, out int y) {
- x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
- y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
- }
- public TextMesh CreateWorldText(Transform parent, string text, Vector3 localPosition, int fontSize, Color color, TextAnchor textAnchor, TextAlignment textAlignment) {
- GameObject gameObject = new GameObject("World_Text", typeof(TextMesh));
- Transform transform = gameObject.transform;
- transform.SetParent(parent, false);
- transform.localPosition = localPosition;
- TextMesh textMesh = gameObject.GetComponent<TextMesh>();
- textMesh.anchor = textAnchor;
- textMesh.alignment = textAlignment;
- textMesh.text = text;
- textMesh.fontSize = fontSize;
- textMesh.color = color;
- return textMesh;
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////Testing.cs////////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Testing : MonoBehaviour
- {
- Grid grid;
- int value;
- private void Start() {
- grid = new Grid(4, 2, 10f, new Vector3(20,0));
- }
- private void Update() {
- if (Input.GetMouseButtonDown(0)) {
- grid.SetValue(Camera.main.ScreenToWorldPoint(Input.mousePosition), 10);
- }
- if (Input.GetMouseButtonDown(1)) {
- Debug.Log(grid.GetValue(Camera.main.ScreenToWorldPoint(Input.mousePosition)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement