• 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
Question by siaran · Mar 06, 2015 at 04:56 PM · uiconversion

Need help converting a SelectionGrid to new UI

I had a script in the old UI that sort of looked like this: (example code is somewhat simplified for clarity)

 public GameObject[] things;
 int selectionIndex;
 string[] content;
     
 void Start(){
  content = new string[things.length];
  for(int i = 0; i < things.length; i++){
   content[i] = things[i].name;
  }
 }
         
 void OnGUI(){
  selectionIndex = GUI.SelectionGrid(new Rect(location), selectionIndex, content, 2);
  if(GUI.Button(new Rect(location), "Make selected thing")) MakeAThing();
 }
 
 void MakeAThing(){
  Instantiate(things[selectionIndex], Vector3.zero, Quaternion.identity);
 }

So, in the new UI, getting a button to call that function is easy enough. But, how do I set the selectionIndex? I could make a ToggleGroup and add Toggles, and give them an OnClick event that calls a method that sets an index value tied to that toggle...

Added complication then is that the size of the things[] is variable, so I'd have to somehow set the value of the int set by the event call on the Toggle... (and create the toggle game objects dynamically, but that's not really an issue)

Well, it seems like it would be really complicated compared to the old way of doing things, and right now it feels like just using the old method is a better idea than trying to make it work with the new UI. So, is it? Or have I missed something about the new UI system that gives me a reasonable way of doing this?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by DiegoSLTS · Mar 08, 2015 at 04:48 PM

I'd create a prefab from a Button that also contains a script attached which stores reference to a GameObject (lets say that reference is called "thing").

When you have to add the buttons on the grid, instantiate that prefab, get the script component and set the game object reference to the thing it must instantiate when pressed. You'll be iterating over the "things" array, so you have the thing there.

Set the On Click event of the Button to call an OnClick method defined on the script, and inside that method call:

 Instantiate(thing, Vector3.zero, Quaternion.identity);

To get the buttons displayed on a grid use de "Grid Layout Group" component on a game object, and when you instantiate the buttons, make them childs of that game object.

Comment

People who like this

0 Show 2 · 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 siaran · Mar 09, 2015 at 02:10 PM 0
Share

I hadn't thought of putting a script with a reference to an object on button prefabs, that is a good idea, but doesn't quite solve my problem.

Thing is, I don't want to instantiate the thing when I press it's button - instead, I want it to be 'selected', show some information about the selected thing in a text area, then have a seperate 'instantiate selected object' button that will instantiate the selected object when pressed.

I also didn't know about the Layout components, that will also help.

avatar image siaran · Mar 09, 2015 at 02:39 PM 0
Share

Alright, I have a working solution, guess I'll post it here

I made a prefab with a Toggle component, and attached this script to it:

 using UnityEngine;
 using System.Collections;
 
 public class ShipSelect : MonoBehaviour {
 
 
     public Fighter fighterReference;
     public ShipSelection selectionReference;
 
     public void GetSelected(bool selected){
 
         if(selected){
             selectionReference.selectedShip = fighterReference;
         }
     }

and I call GetSelected with an OnValueChanged(boolean) on the Toggle.

Then, I made an object with a Grid Layout Group and a ToggleGroup on it and added this script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ShipSelection : MonoBehaviour {
 
     public Fighter[] availableShips;
     public Fighter selectedShip;
     public ToggleGroup toggleGroup;
     public ShipSelect selectionButtonPrefab;
 
     void Start(){
 
         for(int i = 0; i < availableShips.Length; i++){
 
             ShipSelect sl = (ShipSelect) Instantiate(selectionButtonPrefab, Vector3.zero, Quaternion.identity);
             sl.fighterReference = availableShips[i];
             sl.selectionReference = this;
             sl.transform.SetParent(transform, false);
             sl.GetComponent<Toggle>().group = toggleGroup;
             sl.GetComponent<Toggle>().isOn = true;
             selectedShip = sl.fighterReference;
         }
     }
 
 
 
     public void GetSelected(){
         Instantiate(selectedShip, Vector3.zero, Quaternion.identity);
     }
 
 }

And I have a button that calls GetSelected on its OnClick event.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Get the width of a Text element? 0 Answers

InputField null refrence exception 1 Answer

[Unity 4.6] Are Selection Lists and List Items Pre-made? 1 Answer

Input Field problem (Unity 4.6 UI) on LG G3 Android device 1 Answer

Unity 2D 4.6b UI: Why are instantiated prefab sprites rendering over UI elements? 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