Hello, Unity Community, I have a problem with OnGUI() function. This simple script takes up to .5 ms to update on my Core 2 Duo MacBook Pro. I’m going to port this game to iPhone, and I think that OnGUI() will slow down my game a lot. Is there any way to make this code cleaner or make custom gui to display it?
class Part{
var name : String;
var image : Texture2D;
}
var objects : Part[] = [new Part()];
var disabledList : Array = new Array();
private var scrollPosition : Vector2;
function OnGUI () {
scrollPosition = GUI.BeginScrollView(Rect(0,0,objects[0].image.width + 30,Screen.height),scrollPosition,Rect(0,0,objects[0].image.width + 12,objects.Length *(objects[1].image.height + 10)));
GUILayout.BeginVertical();
var obj : Part;
var inst : GameObject;
var i : int = 0;
var able : boolean = true;
var ii : int = 0;
for(obj in objects){
for(ii in disabledList){
if(ii == i)
able = false;
}
if(able){
if(GUILayout.Button(obj.image)){
inst = Instantiate(Resources.Load(obj.name),Camera.main.ScreenToWorldPoint(Vector3(Screen.width /2, Screen.height /2,10)),Quaternion.identity);
disabledList.Add(i);
}
}
able = true;
i++;
}
GUILayout.EndVertical();
GUI.EndScrollView();
}
A bit about the code: all images are 64px wide and 64px tall. I’ve mixed GUILayout and GUI because I had never played with GUILayout, it looked more simple for me to mix both.