x


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

more ▼

asked Jul 14 '10 at 02:34 AM

Adam 2 gravatar image

Adam 2
11 4 4 4

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

You definitely don't want to have that code inside OnGUI. It runs multiple times a frame.

more ▼

answered Jul 14 '10 at 03:55 AM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

what do i want it in then? LateUpdate?

Jul 14 '10 at 04:14 AM Adam 2

Probably. Just make sure you're only doing it as often as you need to.

Jul 14 '10 at 06:12 AM Tetrad
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 14 '10 at 07:56 AM

Ony gravatar image

Ony
903 29 36 48

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Oct 20 '12 at 12:28 PM

MrLolEthan gravatar image

MrLolEthan
41 1 2 6

(comments are locked)
10|3000 characters needed characters left

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(...
more ▼

answered Aug 11 '10 at 03:26 PM

Cyb3rManiak gravatar image

Cyb3rManiak
1.6k 1 4 17

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3461
x1363
x196

asked: Jul 14 '10 at 02:34 AM

Seen: 1094 times

Last Updated: Oct 20 '12 at 12:28 PM