- Home /
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?
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.
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 - ins$$anonymous$$d, 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.
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 : $$anonymous$$onoBehaviour {
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 : $$anonymous$$onoBehaviour {
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.