Function OnGUI

Function OnGUI

  1. How often/ when is it called

  2. Do i need to use it when im making a window that has a scroll bar

  3. when is the best time to use it

    function OnGUI () {
    var Temp = TurnManagerGO.GetComponent(TurnManager);
    var ALH = new List.();
    ALH = Temp.ActionList;
    var ALHS : GameObject;
    var ALHS2 : Stats;
    var aTexture : GUITexture;
    var Spacer : int;
    var LCount : int;
    LCount = ALH.Count;
    Debug.Log(LCount);
    // An absolute-positioned example: We make a scrollview that has a really large client
    // rect and put it in a small rect on the screen.
    scrollPosition = GUI.BeginScrollView (Rect (0,0,900,120), scrollPosition, Rect (0, 0, 820, 110));

     for(var i = 0; i < LCount; i++){
     	 ALHS = ALH*;*
    
  • Debug.Log(ALHS);*
  • ALHS2 = ALHS.GetComponent(Stats);*
  • aTexure = ALHS2.PlayerGUI;*
  • Spacer = 5;*
    _ //GUI.DrawTexture(Rect(((i * Spacer) + (i * 100)),10,100,100), aTexture, ScaleMode.ScaleToFit, true, 0);_
    }
    GUI.EndScrollView ();
    }
    is this a good use of OnGUI

As it is performed several times per frame, you should only do what is absolutely necessary inside OnGUI. GetComponent, list initialisation and that sort of things should happen at start or when they change.

The engine will call the function at the appropriate time (after all the 3D passes, to draw on top of eveything. That’s why you can’t draw 3D over GUI without a rendertexture).

Finally, if you leave those Debug.Log, especially one in a loop, you’re FPS will near the ground.

var scrollPosition : Vector2 = Vector2.zero;
var Temp = TurnManagerGO.GetComponent(TurnManager);
var ALH = new List.();
var ALHS : GameObject;
var ALHS2 : Stats;
var aTexture : GUITexture;
var Spacer : int = 5;
var LCount : int;

function OnGUI () {
    scrollPosition = GUI.BeginScrollView (Rect (0,0,900,120), scrollPosition, Rect (0, 0, 820, 110));
    for(var i = 0; i < LCount && 25; i++){
    	 ALHS = ALH*;*
  • ALHS2 = ALHS.GetComponent(Stats);*
  • aTexure = ALHS2.PlayerGUI;*
    _ GUI.DrawTexture(Rect(((i * Spacer) + (i * 100)),10,100,100), aTexture, ScaleMode.ScaleToFit, true, 0);_
    }
    GUI.EndScrollView ();
    }

Function ListUpdate(){

  • ALH = Temp.ActionList;*
  • LCount = ALH.Count;*
    }
    so this would be a cleaner better version then?