Advertisement
halleman19

editor game grid | unity3d

Oct 18th, 2023
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | Gaming | 0 0
  1. #if UNITY_EDITOR
  2.  
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. public class EGenericGrid : EditorWindow
  7. {
  8.     public GameObject obj;
  9.  
  10.     [MenuItem("tools/editor grid")]
  11.     public static void ShowEditorGridWind()
  12.     {
  13.         EGenericGrid wind = GetWindow<EGenericGrid>();
  14.         wind.titleContent = new GUIContent("Tool for generic game grid");
  15.         wind.maxSize = new Vector2( 350, 250 );
  16.         wind.minSize = wind.maxSize;
  17.     }
  18.  
  19.     private GUISkin skin;
  20.  
  21.     private string verLengthSize = "0";
  22.     private string horLengthSize = "0";
  23.  
  24.     private string status = "waiting for data input";
  25.  
  26.     private string pathToPrefab = "debug/ed/tile";
  27.  
  28.     private string warningMsg = "Change const gridLength in GameGrid.cs";
  29.  
  30.     private void Awake()
  31.     {
  32.         skin = Resources.Load("debug/ed/edSkin") as GUISkin;
  33.     }
  34.  
  35.     private void OnGUI()
  36.     {
  37.         GUI.skin = skin;
  38.  
  39.         GUI.Box(new Rect(25, 10, 300, 40), new GUIContent("Enter data for grid"));
  40.  
  41.         GUI.Label(new Rect(10, 55, 200, 30), "Enter length grid (vertical)");
  42.         GUI.Label(new Rect(10, 90, 200, 30), "Enter length grid (horizontal)");
  43.  
  44.         verLengthSize = GUI.TextField(new Rect(230, 55, 100, 30), verLengthSize);
  45.         horLengthSize = GUI.TextField(new Rect(230, 90, 100, 30), horLengthSize);
  46.  
  47.         GUI.Label(new Rect(25, 125, 300, 30), status);
  48.  
  49.         if (GUI.Button(new Rect(75, 160, 200, 40), new GUIContent("Generic")))
  50.         {
  51.             int x; int y;
  52.  
  53.             if (!int.TryParse(verLengthSize, out x) || x <= 0)
  54.             {
  55.                 status = "correct input vertical length grid";
  56.                 return;
  57.             }
  58.  
  59.             if (!int.TryParse(horLengthSize, out y) || y <= 0)
  60.             {
  61.                 status = "correct input horizontal length grid";
  62.                 return;
  63.             }
  64.  
  65.             status = "start generic";
  66.  
  67.  
  68.             genericGrid(x, y);
  69.         }
  70.  
  71.         GUI.Box(new Rect(25, 205, 300, 40), warningMsg, skin.FindStyle("warning"));
  72.        
  73.     }
  74.  
  75.     private void genericGrid(int x, int y)
  76.     {
  77.         if (!System.IO.File.Exists(string.Format("Assets/Resources/{0}{1}", pathToPrefab, ".prefab")))
  78.         {
  79.             status = "no find prefab tile";
  80.             return;
  81.         }
  82.  
  83.         status = "run process generic";
  84.  
  85.         GameObject parrObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  86.         parrObj.name = "grid-game";
  87.  
  88.         Component[] components = parrObj.GetComponents(typeof(Component));
  89.  
  90.         foreach (Component comp in components)
  91.         {
  92.             if (comp.GetType() == typeof(Transform))
  93.                 continue;
  94.  
  95.             DestroyImmediate(comp);
  96.         }
  97.  
  98.         GameObject tile = Resources.Load<GameObject>(pathToPrefab);
  99.  
  100.         float size = tile.transform.localScale.x;
  101.  
  102.         for (int i = 0; i < x; i++)
  103.         {
  104.             for (int j = 0; j < y; j++)
  105.             {
  106.                 Transform tileObj = Instantiate(tile, new Vector3(0, 0, 0), Quaternion.identity).GetComponent<Transform>();
  107.                 tileObj.SetParent(parrObj.transform);
  108.                 tileObj.position = new Vector3(-0.5f + (j * size), 0.5f, 0.5f - (i * size));
  109.                 tileObj.name = string.Format("tile {0} {1}", i, j);
  110.                 tileObj.GetComponent<Tile>().setCord(i, j);
  111.             }
  112.         }
  113.  
  114.         Debug.LogWarning(warningMsg);
  115.     }
  116. }
  117.  
  118. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement