Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Philip999 · Jul 21, 2013 at 01:54 PM · uiguirts

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Philip999 · Jul 21, 2013 at 06:01 PM 0
Share

ok Thank you, sounds promising, I will try this out either today or tomorrow, and then mark your reply as answered.

avatar image Philip999 · Jul 22, 2013 at 06:13 PM 0
Share

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.

avatar image Philip999 · Jul 25, 2013 at 05:58 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Procedurally generated uGUI elements render under world space objects 1 Answer

Where do I find UI Text? 3 Answers

How do I keep an ui element in side of the canvas when scaling? 1 Answer

Can't destroy an ui element 2 Answers

Problem with build version 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges