- Home /
How to deal with 100 UI text elements displayed as a grid and sharing same event logic
Hi, I'm a beginner in Unity and C#. I've spent hours and hours watching tutorials but I'm not sure what would be the best way to address following issue :
I will have on one single screen 100 UI "elements" displayed in ten lines of 10 columns. These elements will show no value in the begin. In my idea, each element could have two "properties" like LineName and ColumnNo.
when the user clicks on one of the elements, the system will calculate something based on the LineName and ColumnNo of this element (and other calculation parameters that I don't need to describe here) and display the result in the element.
I would like to be able to "undo" the player's choice. This means I need to know the last element that has been clicked after process. It would also be great if it was possible to set a backcolor to the selected element. After that, the player will click on a button to validate his choice, this would set the backcolor to default. The "Undo" option could be a button that retrieves the last clicked element and that sets the value back to null and the color to default.
I don't want to use a grid because of display purposes.
My questions :
Q1 : what kind of UI element would best suit to this expected behaviour ? - Text ? I saw a video where the person added a component to the UI Text in order to change its display properties (change color on MouseMove, change value onMouseClick...) - Input Text (the user won't input anything) ? - Buttons ? The Onclick event is available, the highlighted color is a standard behaviour, I know I can change the text shown by the button, etc... but it's strange for me to use buttons for that purpose. - another UI element ?
Q2 : do I have to call the function that calculates the result on each of the 100 elements or is there a way to avoid this ? I dream of an "OnClick" event shared by all the 100 elements that would do the job based on the element's "address" (LineName and ColumnNo).
Q3 : has someone seen something that looks like this in a sample project or a tutorial I could have a look at ?
I hope that at least one person has understood what I'm trying to do...
Thanks for any answer.
Is the calculated result a globally calculated thing (eg dependent on other items), or is it solely based on the item clicked?
Thank you for your answer.
The result is a globally calculated thing depending on the lineName and on other items. To be sure that you understand what I'm trying to achieve, we can think of the Triple Yahtzee game. 5 dices have been thrown : 1 1 2 3 6. Clicking on an element of the "Ones" (3 fields on this line) will store the value 2 in the element clicked (two ones). Clicking on the first element of the third line (the Threes) would store 3 and so on.
==>I need two know on which element the player has clicked before calculating. For example the element with LineName = Threes and ColumnNo = 3. The calculation must now look for the threes, sum them up and save+show the result back in the element that had been clicked.
For the moment, I don't want to call the calculation function from each of the 100 elements, I guess there is a lighter solution. I'm probabling missing something somewhere but this is the first project I'm working on and I don't want to give up too soon ! ;)
Answer by NorthStar79 · Jul 19, 2017 at 06:22 AM
q1 : take a look at grid layout group and layout element. q2 : yes you can create a logic like that, you may want to take a look at obj. oriented programming tips. q3: sorry but no,
Answer by Candide69 · Jul 22, 2017 at 05:21 AM
I found a workaround. I didn't want to change the code on 100 buttons (calling the same function with the ButtonName as parameter). What I did is that each of the buttons now calls the same function without any parameter but this function finds in the EventSystem what was the last button that was clicked (string selectedButtonName = EventSystem.current.currentSelectedGameObject.name;)
I don't know if that is the best solution but it works.
I didn't find a solution in the OOP tips (I was looking for an option to bind the 100 buttons to an "Object Class" that would have hold the event-logic (OnClick event would then be set on the class and not on each of the buttons).
each of the buttons now calls the same function without any parameter but this function finds in the EventSystem what was the last button that was clicked
wow, that's a good workaround.
Your answer
Follow this Question
Related Questions
Detect Text in GUI; Print 1 Answer
GUI text font change 3 Answers
Limit on GUI Components? 0 Answers
Unity GUI text displaying as noise 1 Answer
GUI.PasswordField "•" makes "?" 1 Answer