- Home /
Tower Defence Weapon hud
First off i have a window which has 6 buttons, i want this to be my weapon cache. How do i go about setting each button to have a different weapon, and when clicked it will allow me in game to drag the current weapon onto a part of the map. Basically what i want is within many tower defence games where you select a weapon then place it on the map(minus the hud and buttons).
my code so far(only shows buttons and window which can be opened and closed):
using UnityEngine; using System.Collections;
public class MyGUI : MonoBehaviour { //public GUISkin mySkin;
public float buttonWidth = 5.0f; public float buttonHeight = 5.0f;
// / Weapon Cache window */
/*******/
private bool displayWeaponCache = true; private const int WEAPON_CACHE_ID = 1; private Rect weaponWindowRect = new Rect(525.0f, 5.0f, 50.0f, 400.0f); private int weaponCols = 1; private int weaponRows = 9;
// Use this for initialization void Start () {
}
// Update is called once per frame void Update () {
}
void OnGUI() { if (displayWeaponCache) { weaponWindowRect = GUI.Window(WEAPON_CACHE_ID, weaponWindowRect, WeaponCacheWindow, ""); }
if(GUI.Button(new Rect (575.0f, 5.0f, 25.0f, 25.0f),"X")) { displayWeaponCache= !displayWeaponCache; } }
public void WeaponCacheWindow(int winID) { for (int y = 0; y < weaponRows; y++) { for (int x = 0; x < weaponCols; x++) { GUI.Button(new Rect(5 + (x buttonWidth), 10 + (y buttonHeight),buttonWidth, buttonHeight), (x + y * weaponCols).ToString()); } } }
}
i also know this is a lot of work but im, only looking for tips and to be lead in the correct direction thanks much appreciated :D
i've not got this question answered but i found an alternate way but it may not be best option. i set two buttons so far and when i click either button my boolean is set to true then i can click on map to instantiate the specified prefab. although whne i place it, it flips about a lot :s
Answer by Samir 1 · Mar 06, 2011 at 04:36 PM
I have found a way to do this now so thanks anyway :)
Answer by slkjdfv · Mar 06, 2011 at 04:52 PM
where you do this
public void WeaponCacheWindow(int winID)
{
for (int y = 0; y < weaponRows; y++)
{
for (int x = 0; x < weaponCols; x++)
{
GUI.Button(new Rect(5 + (x * buttonWidth),
10 + (y * buttonHeight),buttonWidth, buttonHeight),
(x + y * weaponCols).ToString());
}
}
}
try this instead
public void WeaponCacheWindow(int winID)
{
for (int y = 0; y < weaponRows; y++)
{
for (int x = 0; x < weaponCols; x++)
{
float myTempString = (x + y * weaponCols);
if (GUI.Button(new Rect(5 + (x * buttonWidth),
10 + (y * buttonHeight),buttonWidth, buttonHeight),
(x + y * weaponCols).ToString()))
{
if(myTempString == 0)
{
//This is where you put the code to instantiate the object selected
}
}
}
}
}