- Home /
Scrollbar position focus issue[Solved]
using the old GUI.BeginScrollView basically i have this code:
scrollPosition = GUI.BeginScrollView (new Rect (0, Screen.height * 0.14f + 31, Screen.width + 50, Screen.height * 0.6f), scrollPosition, new Rect (0, Screen.height * 0.14f + 66 / 2, 0, 28 * numberOfPlayers - 15));
GUI.Label (new Rect (Screen.width * 0.05f + 40, Screen.height * 0.14f + 33, Screen.width - 20, 28 * numberOfPlayers - 5), playerName);
//used to print leaderboard name under one label
for (int i = 0; i < gameResponseObj.GetScoreList().Count; i++)
{
playerName = playerName + nxtLine;
}
not the entirety of the code but thats about most of what's needed
Imagine you have a leaderboard you can scroll through and trying to automatically find my current rank; lets say the player rank is 500/2000 how would i scroll automatically to that position, been trying a lot of thing but can't get it right; i know when to use the code just can't get the position right, need help plz
Answer by zharik86 · Nov 08, 2014 at 08:22 AM
It is necessary to move scroll to the necessary line item. But you remember that if the size of all scroll, for example, 3000, and the amount of its display on the screen, for example, 650, the maximum value of a line item of scroll on y will be 3000-650. Further I will write a small example (write on CSharp):
//Create class for leader with parametr name and score
public class Leader {
public string name = "";
public int score = 0;
}
//Create List of leaders(don't forget add "using System.Collection.Generic"). Of course, you can to create array
public List<Leader> myLeaderBoard = new List<Leader>();
private Vector2 scrollPosition = Vector2.zero;
private string myName = "47"; //name for search
//Update1: create string variable for all text information
private string fullBoard = "";
void Start() {
//For example, initialization you leaders
for(int i =0; i < 100; i++) {
Leader tpLead = new Leader();
tpLead.name = i.ToString();
tpLead.score = 10 * i;
myLeaderBoard.Add(tpLeader)
}
//Update1: Fill string variable
fullBoard = "";
for(int i = 0; i < myLeaderBoard.Count; i++) {
if (fullBoard != "") {
fullBoard = fullBoard + "\n";
}
fullBoard = fullBoard + myLeaderBoard[i].name + ": " + myLeaderBoard[i].score.ToString();
}
}
void OnGUI() {
//Update1: create height text of board. For default you use "label" style
float hBoard = GUI.skin.label.CalcHeight(new GUIContent(fullBoard), Screen.width + 50);
//Create leaderboard, 3td parameyr in scroll: area of full scroll
scrollPosition = GUI.BeginScrollView (new Rect (0, Screen.height * 0.14f + 31, Screen.width + 50, Screen.height * 0.6f), scrollPosition, new Rect (0, 0, Screen.width + 50, hBoard));
GUI.Label(new Rect(0, 0, Screen.width + 50, hBoard), fullBoard);
GUI.EndScroll();
//Create button for search
if (GUI.Button(new Rect(0, 0, 100, 30), "Search")) {
//Search name in list
for(int i = 0; i < myLeaderBoard.Count; i++) {
if (myName == myLeaderBoard[i].name) {
scrollPosition.y = i * hBoard / myLeaderBoard.Count; //Update1: new calculate
if (scrollPosition.y > hBoard - Screen.height * 0.6f) {
scrollPosition.y = hBoard - Screen.height * 0.6f;
}
break;
}
}
}
}
I hope that it will help you.
you could just clamp the scrollPosition.y, but i think the scroll position cant go over the limit anyway. Otherwise my code is slightly similar where is both sames are the same then makes scrollPosition.y = 28*(i-1), i think thats right cause its u start from the 1st player then pos should be 0 and not 28*1=28 but even with that the scrollPosition.y is still off so still don't know what's the issue..
@leonalchemist I don't see and don't know your code, but in my code right calculate is 28*i, because 1st player have index into List by 0. And right doing limit. Check you scroll, set position on "y" twice more than all scroll (in my example it is 28*200).
there isnt much else to it thats needed I have a label with the layer name (I'll just update in the question), my for loop is essential the same as yours... umm but what i can see thats different is you're looping each GUI label so its separate, in my case everything seem to be printed under one label, using example from app42 to upload scores thats why
@leonalchemist I wrote you the common method. Of course, it is possible to use one GUI.Label(). But then it is necessary to calculate on another scroll height. And I don't know, a misprint or not, but in your question the third parametr of scroll:
new Rect (0, Screen.height * 0.14f + 66 / 2, 0, 28 * numberOfPlayers - 15).
Probably ins$$anonymous$$d of the third value 0 shall be any value. I will update the response that it was possible to use one GUI.Label().
[Edit] nvm, got it, the position of the y label had to be an extra 26pixels for some reason. Seems to works perfectly now, thank you :)
i might have gotten it, tried to have a gui label per name like yours so that i can highlight my current name as well, i think the issues was something like some text spacing, even though the size says 28, when adding a next line i think it has an extra pixel before and after printing the label.. i think its mostly working now though not sure why it starts printing from the 2nd name now...(in the array it clearly shows the 1st name is properly stored in the inspector)