Advertisement
Guest User

Untitled

a guest
May 26th, 2020
4,823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.82 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. ////////////////////////////////////////////////////////////////////////////////////Grid.cs/////////////////////////////////////////////////////////////////////////////////////
  3. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10.  
  11. public class Grid
  12. {
  13.     private int width;
  14.     private int height;
  15.     private float cellSize;
  16.     private Vector3 originPosition;
  17.     private int[,] gridArray;
  18.     private TextMesh[,] debugTextArray;
  19.  
  20.     public Grid (int width, int height, float cellSize, Vector3 originPosition) {
  21.         this.width = width;
  22.         this.height = height;
  23.         this.cellSize = cellSize;
  24.         this.originPosition = originPosition;
  25.  
  26.         gridArray = new int[width, height];
  27.         debugTextArray = new TextMesh[width, height];
  28.  
  29.         for (int x = 0; x < gridArray.GetLength(0); x++) {
  30.             for (int y = 0; y < gridArray.GetLength(1); y++) {
  31.                 debugTextArray[x,y] = CreateWorldText(null, gridArray[x, y].ToString(), GetWorldPosition(x, y) + new Vector3(cellSize, cellSize) * .5f, 20, Color.white, TextAnchor.MiddleCenter, TextAlignment.Center);
  32.                 Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
  33.                 Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
  34.             }
  35.         }
  36.         Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
  37.         Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
  38.     }
  39.  
  40.     private Vector3 GetWorldPosition(int x, int y) {
  41.         return new Vector3(x, y) * cellSize + originPosition;
  42.     }
  43.  
  44.     public void SetValue(int x, int y, int value) {
  45.         if (x >= 0 && y >= 0 && x < width && y < height) {
  46.             gridArray[x, y] = value;
  47.             debugTextArray[x, y].text = gridArray[x, y].ToString();
  48.         }
  49.     }
  50.  
  51.     public void SetValue(Vector3 worldPosition, int value) {
  52.         int x, y;
  53.         GetXY(worldPosition, out x, out y);
  54.         SetValue(x, y, value);
  55.     }
  56.  
  57.     public int GetValue(int x, int y) {
  58.         if (x >= 0 && y >= 0 && x < width && y < height) {
  59.             return gridArray[x, y];
  60.         } else {
  61.             return 0;
  62.         }
  63.     }
  64.  
  65.     public int GetValue (Vector3 worldPosition) {
  66.         int x, y;
  67.         GetXY(worldPosition, out x, out y);
  68.         return GetValue(x, y);
  69.     }
  70.     public void GetXY(Vector3 worldPosition, out int x, out int y) {
  71.         x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
  72.         y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
  73.     }
  74.  
  75.     public TextMesh CreateWorldText(Transform parent, string text, Vector3 localPosition, int fontSize, Color color, TextAnchor textAnchor, TextAlignment textAlignment) {
  76.         GameObject gameObject = new GameObject("World_Text", typeof(TextMesh));
  77.         Transform transform = gameObject.transform;
  78.         transform.SetParent(parent, false);
  79.         transform.localPosition = localPosition;
  80.         TextMesh textMesh = gameObject.GetComponent<TextMesh>();
  81.         textMesh.anchor = textAnchor;
  82.         textMesh.alignment = textAlignment;
  83.         textMesh.text = text;
  84.         textMesh.fontSize = fontSize;
  85.         textMesh.color = color;
  86.         return textMesh;
  87.     }
  88. }
  89.  
  90. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  91. //////////////////////////////////////////////////////////////////////////////////Testing.cs////////////////////////////////////////////////////////////////////////////////////
  92. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  93. using System.Collections;
  94. using System.Collections.Generic;
  95. using UnityEngine;
  96.  
  97. public class Testing : MonoBehaviour
  98. {
  99.     Grid grid;
  100.     int value;
  101.     private void Start() {
  102.         grid = new Grid(4, 2, 10f, new Vector3(20,0));
  103.     }
  104.  
  105.     private void Update() {
  106.         if (Input.GetMouseButtonDown(0)) {
  107.             grid.SetValue(Camera.main.ScreenToWorldPoint(Input.mousePosition), 10);
  108.         }
  109.  
  110.         if (Input.GetMouseButtonDown(1)) {
  111.             Debug.Log(grid.GetValue(Camera.main.ScreenToWorldPoint(Input.mousePosition)));
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement