Search Unity

Menu Item that Asks for Input

Discussion in 'Editor & General Support' started by TheLeadHound, Oct 5, 2017.

  1. TheLeadHound

    TheLeadHound

    Joined:
    Sep 26, 2015
    Posts:
    1
    I have been looking around for quite a while and not seen this addressed. Can you make a Menu Item that asks for input? Right now I have a Menu Item Script which works fine, but if I want to change the number of hexes it creates, I have to reopen the file and change the x and y numbers. Is there a way to ask for an x and y value before it generates them?

    Here is my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class HexGenerator
    7. {
    8.  
    9.     static int y = 16;//size of the map I want this to be decided when I click the Menu Item
    10.     static int x = 16;
    11.  
    12.     [MenuItem("Hex Board/Generate Tiles")]
    13.     static void Create()
    14.     {
    15.         float Xdist = 1.27f; //The x distance between collumns of tiles IN EVEN NUMBERED ROWS
    16.         float Ydist = 0.96f; //The y distance between rows of tiles
    17.         GameObject HexBlueprint;
    18.         GameObject HexContainer = new GameObject("Hex Container");
    19.         Sprite HexImage = new Sprite();//loads the default hex image so it does not need to be loaded everytime for each hex
    20.         Texture2D HexPNG = Resources.Load<Texture2D>("Hexes/Base Hex");
    21.         HexImage =
    22.             Sprite.Create(HexPNG,
    23.             new Rect(0, 0, HexPNG.width, HexPNG.height),
    24.             new Vector2(0.5f, 0.5f));
    25.  
    26.         CharacterMovement mapInfo = GameObject.Find("Movement Controler").GetComponent<CharacterMovement>();
    27.         mapInfo.maxX = x - 1;//sets the max coordinates so the Movement Controller knows the largest x and y values of the map.
    28.         mapInfo.maxY = y - 1;
    29.  
    30.         for (int i = 0; i < y; i++)
    31.         {
    32.             for (int j = 0; j < x; j++)
    33.             {
    34.                 if (i % 2 == 0)
    35.                 {
    36.                     HexBlueprint = new GameObject("Hex," + j + "," + i);
    37.                     HexBlueprint.transform.position = new Vector3(j * Xdist, i * Ydist, 1);//places the tile in position based on the coordinates
    38.  
    39.                     SpriteRenderer renderer = HexBlueprint.AddComponent<SpriteRenderer>();
    40.                     renderer.sprite = HexImage;
    41.                     HexBlueprint.transform.parent = HexContainer.transform;//puts hex into easy to collapse container
    42.                 }
    43.                 else
    44.                 {
    45.                     HexBlueprint = new GameObject("Hex," + j + "," + i);
    46.                     HexBlueprint.transform.position = new Vector3(j * Xdist + Xdist / 2, i * Ydist, 1);//places the tile in position based on the coordinates
    47.  
    48.                     SpriteRenderer renderer = HexBlueprint.AddComponent<SpriteRenderer>();
    49.                     renderer.sprite = HexImage;
    50.                     HexBlueprint.transform.parent = HexContainer.transform;//puts hex into easy to collapse container
    51.                 }
    52.             }
    53.         }
    54.     }
    55. }
    56.