- Home /
UI for Selected Buildings
I'm making a RTS game (c#) and I need my UI to be able to change its content depending on which building I have selected.
So pretty much all buildings will be able to be destroyed through a button press, but not all, and each type of building would have additional different buttons relating to different functions.
The way I would like to have done this, is to have a base class that declares the destroy button and any other common buttons, and then have a class (inherited from the base class) that is specific to the type of building. Which declares the unique buttons of that building type. The Script would then be attached to the prefab.
Then when a building gets selected, it would pass these buttons to a function in my main UI class, and display them in the a designated area.
However GUILayout.button (); is a function and returns a bool. So I can't create and array of buttons and pass it to the UI. I do realise I could create an array of strings, and create the button based on that in the main UI, but that would mean I couldn't easily organise the order or layout of them.
Coming from a .Net forms approach I would be able to create an button object, and event handler. Does anyone have any ideas or know of any tutorials/topics that cover a system that allows me to achieve my desired result? I am fairly new to unity so I may be missing something obvious.
Answer by private void · Jul 21, 2013 at 04:49 PM
You can use Window for that. GUI.Window or GUILayout.Window
You define your GUI functions as
BuildingGUI(selectedBuilding:int)
{
switch(selectedBuilding)
{
//you call GUILayout.Button()'s here
}
}
Then you call these functions in your OnGUI() method as such:
function OnGUI()
{
if(any_building_is_selected)
myGUIRect = GUILayout.Window(0,myGUIRect,BuildingGUI);
}
ok Thank you, sounds promising, I will try this out either today or tomorrow, and then mark your reply as answered.
Hi I've looked into what you suggested, and it would seem like its what I'm looking for however I do have an issue. I looked at windows in the unity reference and copied their example. when I placed the function within the same class, it all work fine, however when I called a function in a different class (one stored on my prefab) I got the following error.
InvalidProgramException: Invalid IL code in (wrapper delegate-invoke) UnityEngine.GUI/WindowFunction:invoke_void_this__int (int): IL_004c: castclass 0x00000007
UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/GUI.cs:1283)
my code was something like this
$$anonymous$$ainCampUI test = selectObject.GetComponent<<$$anonymous$$ainCampUI>>();
Rect windowRect0 = new Rect(20, 20, 120, 50);
windowRect0 = GUI.Window(0, windowRect0, test.Do$$anonymous$$yWindow, "$$anonymous$$y Window");
and the separate class had
void Do$$anonymous$$yWindow(int windowID) {
if (GUI.Button(new Rect(10, 20, 100, 20), "Hello World"))
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
do you know what I might be doing wrong? Thanks
I dont know how to write text within < > these in code block so the GetComponent should have $$anonymous$$ainCampUI in it.
O$$anonymous$$ So silly mistake I hadn't updated my prefab to contain the script, I keep making the mistake, really annoying!!! so working fine now