Search Unity

Using Buttons to create units only when selecting the barracks.

Discussion in 'Immediate Mode GUI (IMGUI)' started by Custardcs, Sep 1, 2015.

  1. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    Hi i need a little help here...

    basically i need to click on a object like the (barracks)

    then have it change the buttons on the hud to what ever i place them inside to be..

    for example button 1 will spawn a sniper button 2 will spawn a rocketguy etc

    at the moment i have my barracks spawning the two units by pressing keys on the keyboard..


    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class Buildings : MonoBehaviour
    5. {
    6.      //Buttons for Hud
    7.      public Rigidbody BuildQPrefab1, BuildQPrefab2;
    8.      public Transform spawn;
    9.      //drag Selection
    10.      public bool selected = false;
    11.      //private Vector3 moveToDest = Vector3.zero;
    12.      public float floorOffSet = 1;
    13.      //Click Select
    14.      private bool selectionByClick = false;
    15.    
    16.    
    17.      // Update is called once per frame
    18.      private void Update ()
    19.      {      
    20.          HudButtonsUsage ();
    21.          //Place Layer Unit Control Here.
    22.          if (Input.GetMouseButton (0))
    23.          {
    24.              if (!selectionByClick)
    25.              {
    26.                  Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
    27.                  camPos.y = CamOP.InvertMouseY(camPos.y);
    28.                  selected = CamOP.selection.Contains (camPos);
    29.              }
    30.              if (selected)
    31.                  gameObject.GetComponentInChildren<Projector>().enabled = true;
    32.              else
    33.                  gameObject.GetComponentInChildren<Projector>().enabled = false;
    34.          }
    35.    
    36.      }
    37.      //Function for Spawning
    38.      private void HudButtonsUsage()
    39.      {
    40.          //This Must be the first Button on the HUD
    41.          if (Input.GetKeyUp ("t"))
    42.          {
    43.              Instantiate(BuildQPrefab1, spawn.position, spawn.rotation);
    44.          }
    45.          //This Must be the Second Button on the HUD
    46.          if (Input.GetKeyUp ("y"))
    47.          {
    48.              Instantiate(BuildQPrefab2, spawn.position, spawn.rotation);
    49.          }
    50.      }
    51.      //Function for SimpleMouseClick
    52.      private void OnMouseDown()
    53.      {
    54.          selectionByClick = true;
    55.          selected = true;
    56.      }
    57.      private void OnMouseUp()
    58.      {
    59.          if (selectionByClick)
    60.              selected = true;
    61.        
    62.          selectionByClick = false;
    63.      }
    64. }
    This is the button setup I Currently have..


    and this is kind of what it looks like..
     
  2. Custardcs

    Custardcs

    Joined:
    Aug 26, 2015
    Posts:
    57
    So Im Guessing noone knows? so its not possible? so only way is to draw buttons from script for each building click which is actually retarded.. whats the point of the GUI system them..