Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Badgerfish · Sep 10, 2016 at 04:25 PM · guinetworkingbuttonturn-basedstrategy

Networking and UI Buttons

I have a Networked game where each player is essentially a floating camera, it's much like a Turn-based Strategy game.

Everything works fine up to unit selection.

When I have a selected unit there's a couple of options you can do, which are shown as buttons in the UI, Move, Shoot, Dodge, are some examples.

Unit selection is handled by a manager script that is part of the player's prefab. Which is instantiated(spawned) when they connect.

I have a problem when I have a unit selected and I click on the 'Move' button (or any of them really). How do I bind the 'onClick' function of the action to work on the player's selected model?

So say if there is a function in my player manager that enters "movementMode", called "EnterMovementMode()". Which has to be executed when the player clicked on the "Move" Button in the UI.

Seen in image bellow, The selected unit is highlighted by the green gear-like projector. alt text

Should I make the GUI part of the player object and spawn it as a networkbehaviour object so it is pre-affixed to the action I want it to take?

I'm kinda stuck here

Thanks,

selectedunit.png (82.5 kB)
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
2
Best Answer

Answer by AurimasBlazulionis · Sep 10, 2016 at 05:13 PM

This is what I found on the internet:

thebutton.onClick.AddListener(delegate { YourFunction(anyParameters); }); }

You should add this to your manager and call it for each button.

Now how to get that button? Place a script which acts like a Button Cache. Inside it have something like public static ButtonCache instance;. In start function do this:

instance = this;

Now you can write a variable for each button component and access each button like this:

ButtonCache.instance.whateverButton

Comment
Add comment · Show 4 · 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 Badgerfish · Sep 10, 2016 at 05:40 PM 0
Share

Let me see if I'm understanding the solution correctly.

The player manager, when he joins the game either on Start or OnLocalPlayerStart, I get a reference to the button and set a delegate to it.

It would seem simple, but what I'm not getting is how to grab that reference. I'd like to avoid using something like GetObjectWithTag.

So something like having each button have a script that returns a reference to itself and then GetComponentInParent().GetComponent().onClick.AddListener... etc.

Would something like that work? Or am I totally off?

avatar image AurimasBlazulionis Badgerfish · Sep 10, 2016 at 06:00 PM 0
Share

You could write a script which stores all the buttons. Place it in canvas. $$anonymous$$ake that script have a static variable which points to it. So in your manager, you can access that button by ButtonCache.instance.fireButton. This seems like the cleanest solution.

avatar image Badgerfish AurimasBlazulionis · Sep 10, 2016 at 06:19 PM 0
Share

I gotta be honest, it seems hacky. But by RNG it totally works!

Thanks a bunch! I can now move on with my project.

avatar image AurimasBlazulionis Badgerfish · Sep 10, 2016 at 06:04 PM 0
Share

Also, in the answer, remove getcomponent as if you are writing the button cache, you can already store buttons as Button type.

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

106 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Searching for gameobjects with a tag and creating a button for each one? 1 Answer

Button then instanitates gameobject 1 Answer

TheWebExpert says: My GUI Button is not showing up on Camera Preview 2 Answers

GUI Button Animation Cue 1 Answer

GUI window popup button 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