- Home /
Opening inventory in game makes it lag really bad
so i took a script form a forum post,http://forum.unity3d.com/viewtopic.php?t=46222 , and changed to be used for me:
// var inventory : Array; static private var InventoryOpen: boolean = false; var InventoryGUI:Texture2D; var SpawnPoint: Transform; var ChatOpnener;
var doWindow : boolean = false;
//
public var emptyTex : Texture;
// (- ) public var inventorySizeX = 4; public var inventorySizeY = 5;
//
var iconWidthHeight = 40;
// ( X Y) var spacing = 4;
// public var offSet = Vector2( 100, 100 );
// (. Update()) private var itemImage : Texture2D;
//
class InventoryItem { //
var worldObject : GameObject; //,
var texRepresentation : Texture2D; }
//
function Awake() { ChatOpnener = GameObject.Find("Tank Weapon Shop Keep").GetComponent("ActiveChat"); offSet = Vector2(Screen.width/2+offSet.x,Screen.height/2+offSet.y); inventory = new Array(inventorySizeX);
for( var i = 0; i < inventory.length; i ++ )
{
inventory[i] = new Array(inventorySizeY);
}
}
function OnGUI() {
var texToUse : Texture2D; var currentInventoryItem : InventoryItem; if(InventoryOpen&&!ChatOpnener.ChatIsOpen){ GUI.Label(Rect(Screen.width/2-200,Screen.height/2-200,400,400),InventoryGUI); //
for( var i = 0; i < inventory.length; i ++ ) { //
for( var k = 0; k < inventory[i].length; k ++ ) { texToUse = emptyTex; currentInventoryItem = inventory[i][k];
// I- K- ,
if( inventory[i][k] != null )
{
texToUse = currentInventoryItem.texRepresentation;
}
var it = GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
var w : int = 0;
var h : int = 0;
if (it && currentInventoryItem != null)
{
print("TEST");
Instantiate(currentInventoryItem.worldObject,SpawnPoint.transform.position,Quaternion.identity);
currentInventoryItem.texRepresentation = null;
inventory[i][k] = null;
}
//
Debug.Log("item droped");
}
}
}
}
function AddItem( item : InventoryItem ) { //
for( var i = 0; i < inventory.length; i ++ ) { //
for( var k = 0; k < inventory[i].length; k ++ ) { // ,
if( inventory[i][k] == null ) { inventory[i][k] = item; return; } } }
// , -
}
function AddItem1( worldObject : GameObject, texRep : Texture2D ) { var newItem = new InventoryItem();
newItem.worldObject = worldObject;
newItem.texRepresentation = texRep;
AddItem( newItem );
}
function Update(){ if(Input.GetKeyDown(KeyCode.I)){ if(InventoryOpen){ InventoryOpen=false; }else{ InventoryOpen=true; } } }
but when i added this part:
if (it && currentInventoryItem != null) { print("TEST"); Instantiate(currentInventoryItem.worldObject,SpawnPoint.transform.position,Quaternion.identity); currentInventoryItem.texRepresentation = null; inventory[i][k] = null;
}
it lagged my game REALLY bad, it must e something here, just try and help me please, if more info is needed tell me
Answer by Tetrad · Jul 14, 2010 at 03:55 AM
You definitely don't want to have that code inside OnGUI. It runs multiple times a frame.
Probably. Just make sure you're only doing it as often as you need to.
Answer by Ony · Jul 14, 2010 at 07:56 AM
You just need to go through your code and separate the GUI stuff from the calculation stuff. Move the calculations into their own functions and then call them as needed when you press a button in the GUI.
Don't move it to LateUpdate. That won't solve the fundamental issue which is just simply that you need to organize your code into sections that get called when needed instead of multiple times per frame.
Answer by Cyb3rManiak · Aug 11, 2010 at 03:26 PM
First of all - you have to go over the code and tidy it up.... Lots of wasted CPU cycles are bad for global warming :)
Second - if in fact when you add those lines it got really bad, just for kicks try and define it as a boolean and not let UnityScript figure it out by itself. See if it gives you some boost. Duck typing is fun, but has some performance loss.
var it: boolean = GUI.Button(...
instead of:
var it = GUI.Button(...
Answer by MrLolEthan · Oct 20, 2012 at 12:28 PM
Use time.timeScale = 0 when inventory is open to freeze time. That should freeze the lag but if you want to have things moving in the background of the inventory than this is not the best option.
Your answer