- Home /
Adding a string to grid layout group
Hey there,
I'm trying to add a small UI element that tracks some quests. I do this by storing a list of quest objects, out of which I want to add a few elements to my UI in the form of a grid. Here is the code I have at the moment.
public List<Quest> questLog; // the list containing all the quests
void Start()
{
foreach (Quest q in questLog)
{
bool marker = q.questDisplayed;
string goal = q.questProgress + " / " + q.questGoal;
string description = q.questLogDescription;
}
}
Now my problem is that I can't seem to be able to wrap my head around adding these elements to my grid. I have added a Grid Layout Group with a constraint of 3 columns, and added that to a Scroll View's Content section, according to how this tutorial does it. However, they seem to add an image to the grid and instantiate it, while I want to add 3 different parts to the grid, namely a checkbox and two strings. I could not figure out how to add the text to the grid layout after googling around for some time, so if you have any suggestion as to how I should or can add such components to my grid layout, please leave it in a comment below!
I advise making a prefab element with a script attached you can pass the Quests info to that you add to the Grid Layout Group. It's the method I have used before when wanting complex dynamic elements added to a Grid Layout Group.
I don't quite understand the advice you're giving here. I understand how to create a prefab with a script added to it, but I don't quite understand what the other part is that you're trying to advice me to do, so could you provide an example of this method? It looks exactly like what I want, with the dynamic element adding to a grid layout group, but I don't quite understand the 'how' of it.
Answer by akaBase · Jul 03, 2019 at 10:44 AM
Reply to comment asking for more Information.
Create an empty GameObject with a couple of Texts and an Image(?) as childen to show the information about the Quest and add a script to it you can pass the Quest to.
Example Prefab Script
public class QuestInfo : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private Text _goalText;
[SerializeField] private Text _descriptionText;
[SerializeField] private Image _markerImg;
#pragma warning restore 0649
public void Setup(Quest quest )
{
_markerImg.gameObject.SetActive(quest.questDisplayed);
_goalText.text = quest.questProgress + " / " + q.questGoal;
_descriptionText.text = quest.questLogDescription;
}
}
And then modify your current script to add the prefab and pass the Quest Object to it.
Example Modified Script
public class Quests : MonoBehaviour
{
public List<Quest> questList;
public Transform gridLayoutGroupsTransform;
public GameObject questInfoPrefab;
void Start()
{
foreach (Quest quest in questList)
{
GameObject go = Instantiate(questInfoPrefab, gridLayoutGroupsTransform.position, Quaternion.identity);
go.transform.SetParent(gridLayoutGroupsTransform);
QuestInfo questInfo = go.GetComponent<QuestInfo>();
questInfo.Setup(quest);
}
}
}
Hope that makes more sense now.
Your answer
Follow this Question
Related Questions
Canvas is flipping, 2D game 0 Answers
How do I carry over ui text to the next by obtaining the gameobject children 0 Answers
,InputField backspace returns already send text 0 Answers
Unity 5 UI GUI Text Canvas Screen Resolution Adaption Problem 2 Answers
Grid and Canvas react on mouse events simultaneously 0 Answers