- Home /
List to buttons
Hello i have code what have list but how i can make so that every item on list have automatic button appeared ? Example i have 5 items then it give me 5 buttons. and when 8 items then 8 buttons.
i make list with this code:
foreach(Match match in Regex.Matches(page,name)){
matchList.Add(match.Groups[1].Value);
}
This is on editor (in a inspector) or in game (in your game ui)?
inspector i think, im not sure about that. But application point its get html info and then make buttons and when open then there is news
my full code look so:
using UnityEngine;
using System.Collections.Generic;
using System.Component$$anonymous$$odel;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections;
using UnityEngine.UI;
public class WebGrabber : $$anonymous$$onoBehaviour {
public Text proov;
List<string> matchList = new List<string>();
// Use this for initialization
void Start () {
WebClient w = new WebClient ();
string page = w.DownloadString ("http://blog.counter-strike.net/index.php/category/updates/");
string name = "Release (.*?)</a>";
foreach($$anonymous$$atch match in Regex.$$anonymous$$atches(page,name)){
matchList.Add(match.Groups[1].Value);
}
for (int i = 0; i < matchList.Count; i++)
{
proov.text += ("Release" + matchList[i] + "\n");
}
}
void Update () {
}
}
Answer by antx · Jan 15, 2016 at 02:50 PM
Make a Button and make a prefab from it. Then in your script you can instantiate the button.
Best is if you make a class for your buttons and attach it to your prefab button so you can easily work with them.
public class BafelmaukButton : MonoBehaviour {
public Text label; // asign the buttons text GO here in the inspector.
}
public class ButtonListScript : MonoBehaviour {
public Canvas canvas; // asign your canvas here in the inspector.
public BafelmaukButton myButtonPrefab;
void Start () {
// make sure your matchlist is also somewhere in code
for(int i = 0; i < matchList.Count; i++) {
BafelmauButton myBB = Instantiate(myButtonPrefab);
myBB.transform.SetParent(canvas);
myBB.Label = "Button " + i;
myBB.transform.positon = new Vector3 (100, i * 80, 0);
}
}
}
Interesting i never tried instantiating a prefab this way
I dont know why but there was problem with this code i tried differently but best was that there was some errors:
for(int i = 0; i < matchList.Count; i++) {
BafelmauButton myBB = Instantiate(myButtonPrefab);
myBB.transform.SetParent(canvas);
myBB.Label = "Button " + i;
myBB.transform.positon = new Vector3 (100, i * 80, 0);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Button Action Triggering Multiple Times Per Click? 1 Answer
Filling array with Scribtable Objects automaticly? 1 Answer
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers