- Home /
GUI.Window IDs, will each individual unit need its own ID?
I am attempting to create a GUI for a strategy game. There are only a few unit types, but there are often more than one instance of a unit type on screen at the same time, and each instance will have its own set of buttons. I was hoping to create one GUI.Window, and change the information/functionality of the buttons in the window, depending on which unit is selected.
The problem I am having is if there is a clone of a unit in the game, only the original unit will display the GUI.Window on selection. So, I can switch between one soldier, and the Base building, but it will never pull up the GUI.Window for the second soldier.
Will I have to have a separate window ID for each individual unit? Is there another way to do this possibly? Thanks.
var iconA : Texture2D;
var iconB : Texture2D;
var unitName : String;
var buttonA : Rect = Rect (5, 20, 40, 40);
var buttonB : Rect = Rect (50, 20, 40, 40);
var buttonC : Rect = Rect (95, 20, 40, 40);
var buttonD : Rect = Rect (140, 20, 40, 40);
public var _showBaseActionBar = false;
public var _showSoldierActionBar = false;
var ActionBarWindowRect : Rect = Rect (0, Screen.height-60,185,65);
function Start () {
}
function OnGUI () {
var ActionBarWindowRect : Rect = Rect (0, Screen.height-60,185,65);
if (_showBaseActionBar == true) {
unitName = "Base";
ActionBarWindowRect = GUI.Window (0, ActionBarWindowRect, ActionBarBuilding, unitName);
}
if (_showSoldierActionBar == true) {
unitName = "Soldier";
ActionBarWindowRect = GUI.Window (1, ActionBarWindowRect, ActionBarSoldier, unitName);
}
}
function ActionBarBuilding (windowID : int) {
// Draw any Controls inside the window here
if (GUI.Button (buttonA, iconA)) {
// execute button function here
}
if (GUI.Button (buttonB, iconA)) {
// execute button function here
}
if (GUI.Button (buttonC, iconA)) {
// execute button function here
}
if (GUI.Button (buttonD, iconA)) {
// execute button function here
}
}
function ActionBarSoldier (windowID : int) {
// Draw any Controls inside the window here
if (GUI.Button (buttonA, iconB)) {
// execute button function here
}
if (GUI.Button (buttonB, iconB)) {
// execute button function here
}
if (GUI.Button (buttonC, iconB)) {
// execute button function here
}
if (GUI.Button (buttonD, iconB)) {
// execute button function here
}
}
Answer by Shadyfella13 · Feb 24, 2011 at 08:20 PM
I worked out a solution, for anyone interested.
Instead of having one object in the scene that controls all the GUI elements, I put an OnGUI function in each units script. In my Unit manager, I build an array of all the objects tagged SOLDIER, and assign the index as the windowID. its still messy, but it works.
in the start function of the Soldier script...
UnitID = UnitManager.GetInstance().CountSoldiers();
SOLDIER_WINDOW_ID = UnitID;
in the UnitManager script...
public var soldiersInSceneList = new Array();
function GetSelectedUnitsCount() {
return selectedUnitsList.length;
}
function CountSoldiers() {
return soldiersInSceneList.length + 1;
}
function AddSoldierCount(go : GameObject) {
soldiersInSceneList.Add(go);
}
Your answer
Follow this Question
Related Questions
Apply Render Texture To GUI + Transparency? 2 Answers
Please! How can i close a GUI-Wndows or Button in Javascript? 1 Answer
GUILayout Window not working inside switch case for conditional menu 1 Answer
Creating a FlightSim Like GUI HUD for Showing the player the angle of the Camera. 1 Answer
talking with a npc 0 Answers