Hi guys
I have a script:
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour
{
public Texture2D aboutButton;
public Texture2D optionsButton;
public Texture2D helpButton;
public Texture2D titleLabel;
public Texture2D game1Button;
public Texture2D game2Button;
float menuButtonWidth;
float menuButtonHeight;
float gameButtonWidth;
float gameButtonHeight;
float verticalOffset = 10;
float horizontalMargin = 10;
float verticalMargin = 10;
Vector2 scrollPosition = Vector2.zero;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//all main menu buttons has the same size
menuButtonWidth = optionsButton.width;
menuButtonHeight = optionsButton.height;
gameButtonHeight = game1Button.height;
gameButtonWidth = game1Button.width;
}
void OnGUI()
{
float totalHeight = titleLabel.height + 2*verticalOffset + 3*(verticalOffset+menuButtonHeight);
float totalWidth = horizontalMargin * 3 + titleLabel.width + game1Button.width+20 + verticalMargin;
GUI.BeginGroup(new Rect(Screen.width/2 - totalWidth/2,Screen.height/2 - totalHeight/2,totalWidth,2 * verticalMargin + totalHeight));
float y = verticalMargin;
float x = horizontalMargin;
GUI.Box(new Rect(-horizontalMargin,0,totalWidth,2 * verticalMargin + totalHeight),"");
GUI.Label(new Rect(x,y,titleLabel.width,titleLabel.height),titleLabel);
scrollPosition = GUI.BeginScrollView(new Rect(x+horizontalMargin+titleLabel.width,y,game1Button.width+20,totalHeight),scrollPosition,new Rect(0,0,game1Button.width,10*(game1Button.height+verticalMargin)),false,false);
for(int cnt=0;cnt<10;cnt++)
{
Rect r = new Rect(0,cnt*(gameButtonHeight+verticalMargin),gameButtonWidth,gameButtonHeight);
if(cnt%2 == 0) GUI.Button(r,game1Button,"l");
else GUI.Button(r,game2Button,"l");
}
GUI.EndScrollView();
x += titleLabel.width/2 - menuButtonWidth/2;
y += verticalOffset + titleLabel.height;
GUI.Button(new Rect(x,y,menuButtonWidth,menuButtonHeight),optionsButton,"l");
y += verticalOffset + menuButtonHeight;
GUI.Button(new Rect(x,y,menuButtonWidth,menuButtonHeight),helpButton,"l");
y += verticalOffset + menuButtonHeight;
GUI.Button(new Rect(x,y,menuButtonWidth,menuButtonHeight),aboutButton,"l");
GUI.EndGroup();
}
}
It draws all what I expect,but when it comes to use the scroll it works very,very laggy.I know there is a clipping area there,but is it possible to cause such delays by it??I doubt that.
Please tell me,why the srolling is working so slowly,and how to fix that issue.
↧