• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Stardog · May 26, 2011 at 07:35 PM · c#javascriptarrayconvert

Help converting this to C# - a few issues

Search for "FIX_ME". I need to know what to do with the var inventory : Array, because only Javascript can use that class. And there's one issue with FIX_ME newItem= new InventoryItem(). I think that might need "InventoryItem" before it, but I'm not sure.

Original Javascipt

 //Our inventory array
 var inventory : Array;

 public var emptyTex : Texture;           //This will be drawn when a slot is empty
 public var inventorySizeX = 8;           //the size of the inventory in x and y dimension
 public var inventorySizeY = 5;

 var iconWidthHeight = 20;                  //The pixel size (height and width) of an inventory slot
 var spacing = 4;                          //Space between slots (in x and y)

 public var offSet = Vector2( 100, 100 ); //set the position of the inventory

 // TEST VARIABLES
 // Assign these to test adding Items with mouse clicks (see Update())
 public var testTex : Texture;
 public var testTex2 : Texture;

 //Our Representation of an InventoryItem
 class InventoryItem
 {
     //GameObject this item refers to
     var worldObject : GameObject;
     //What the item will look like in the inventory
     var texRepresentation : Texture;
 }

 function Awake() 
 { 
     inventory = new Array(inventorySizeX);

     for( var i = 0; i < inventory.length; i ++ ) 
     { 
         inventory[i] = new Array(inventorySizeY);
     } 
 } 
 
 function OnGUI() 
 { 
     var texToUse : Texture;
     var currentInventoryItem : InventoryItem;

     //Go through each row 
     for( var i = 0; i < inventory.length; i ++ ) 
     { 
         // and each column 
         for( var k = 0; k < inventory[i].length; k ++ ) 
         { 
             texToUse = emptyTex;
             currentInventoryItem = inventory[i][k];

             //if there is an item in the i-th row and the k-th column, draw it 
             if( inventory[i][k] != null ) 
             { 
                 texToUse = currentInventoryItem.texRepresentation;
             } 

             GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
         } 
     } 
 } 

 function AddItem( item : InventoryItem )
 {
      //Go through each row 

     for( var i = 0; i < inventory.length; i ++ ) 
     { 
         // and each column 
         for( var k = 0; k < inventory[i].length; k ++ ) 
         { 
             //If the position is empty, add the new item and exit the function
             if( inventory[i][k] == null ) 
             {
                  inventory[i][k] = item;
                  return;
             }
         }
     }    

     //If we got this far, the inventory is full, do somethign appropriate here    
 }

 function AddItem( worldObject : GameObject, texRep : Texture )
 {
     var newItem = new InventoryItem();
     newItem.worldObject = worldObject;
     newItem.texRepresentation = texRep;

     AddItem( newItem );    
 }

 function Update()
 {
     if( Input.GetMouseButtonDown( 0 ) ) 
     {
         AddItem( gameObject, testTex );    
     }
 
     if( Input.GetMouseButtonDown( 1 ) )     
     {    
         AddItem( gameObject, testTex2 );    
     }    
 }

C#

 using UnityEngine;
 using System.Collections;
 public class InventoryTestCS : MonoBehaviour {
 
     //Our inventory array
     FIX_ME Array inventory;
 
     public Texture emptyTex;           //This will be drawn when a slot is empty
     public int inventorySizeX= 8;           //the size of the inventory in x and y dimension
     public int inventorySizeY= 5;
 
     int iconWidthHeight= 20;                  //The pixel size (height and width) of an inventory slot
     int spacing= 4;                          //Space between slots (in x and y)
 
     public Vector2 offSet = Vector2 (100, 100); //set the position of the inventory
 
     // TEST VARIABLES
     // Assign these to test adding Items with mouse clicks (see Update())
     public Texture testTex;
     public Texture testTex2;
 
     //Our Representation of an InventoryItem
     class InventoryItem
     {
         //GameObject this item refers to
         GameObject worldObject;
         //What the item will look like in the inventory
         Texture texRepresentation;
     }
 
     // Create the Inventory
     void Awake()
     {
         inventory = new Array(inventorySizeX);
         for (int i= 0; i < inventory.length; i++)
         {
             inventory[i] = new Array(inventorySizeY);
         }
     }
 
     void OnGUI()
     {
         Texture texToUse;
         InventoryItem currentInventoryItem;
         //Go through each row
         for (int i= 0; i < inventory.length; i++)
         {
             // and each column
             for (int k= 0; k < inventory[i].length; k++)
             {
                 texToUse = emptyTex;
                 currentInventoryItem = inventory[i][k];
                 //if there is an item in the i-th row and the k-th column, draw it
                 if (inventory[i][k] != null)
                 {
                     texToUse = currentInventoryItem.texRepresentation;
                 }
                 GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
             }
         }
     }
 
     void AddItem(InventoryItem item)
     {
         //Go through each row
         for (int i= 0; i < inventory.length; i++)
         {
             // and each column
             for (int k= 0; k < inventory[i].length; k++)
             {
                 //If the position is empty, add the new item and exit the function
                 if (inventory[i][k] == null)
                 {
                     inventory[i][k] = item;
                     return;
                 }
             }
         }
         //If we got this far, the inventory is full, do somethign appropriate here
     }
 
     void AddItem(GameObject worldObject, Texture texRep){
         FIX_ME newItem= new InventoryItem();
         newItem.worldObject = worldObject;
         newItem.texRepresentation = texRep;
         AddItem(newItem);
     }
 
     void Update()
     {
         if( Input.GetMouseButtonDown( 0 ) )
         {
             AddItem( gameObject, testTex );
         }
         if( Input.GetMouseButtonDown( 1 ) )
         {
             AddItem( gameObject, testTex2 );
         }
     }
 }
Comment
Peter G

People who like this

1 Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · May 26, 2011 at 07:37 PM 0
Share

You may want to repost/re-format your code. Not many are going to look at it like that so spaced out.

avatar image Stardog · May 26, 2011 at 07:42 PM 0
Share

It's only really two lines that need looked at: "var inventory : Array;" to C#, and "var newItem = new InventoryItem();" to C#.

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Peter G · May 26, 2011 at 08:05 PM

They are implicitly creating an array of arrays, so you have a few options:

  1. A jagged array:

         InventoryItem[][] inventory;
    
    
  2. You could use a multi-dim array. This is no longer an array of arrays, but since all the arrays are the same size in the js above, you are essentially creating a box anyway.

         InventoryItem[,] inventory;
    
    
  3. A list of lists:

         List<List<InventoryItem>> inventory = new List< List<InventoryItem>> ();
    
    
  4. A list of Arrays. This is a possibility, but it would be a bizarre solution for your case.

         List< InventoryItem[] > inventory = new List< InventoryItem[] > ();
    
    
  5. Or an Array of lists, same as number 4, probably not the best choice:

         List<InventoryItem>[] inventory;
    
    

If you using a list then you will have to make some other adjustments to match the syntax of a list. Just looking at the js, it doesn't look like they resize the array except for the beginning so I would choose any of the first three. Numbers 4 and 5 though would be a little confusing for what you are trying to do so I would probably avoid them since there isn't really any reason in your example to switch types so it would just look strange.

And your other line looks ok since type inference should be able to figure out the type from the right hand side of the assignment operator.

Comment
UniteMage
Muzz5
Stardog
FL

People who like this

4 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UniteMage · May 26, 2011 at 08:38 PM 0
Share

Deserves a good review. Thanks CSDG

avatar image

Answer by testure · May 26, 2011 at 08:01 PM

in C# you need to strictly type your array- from what I saw, it's an array of type InventoryItem, so you'd define it like:

InventoryItem [] inventory; // this will create an uninitialized InventoryItem array.

Although for something like this that may have dynamic sizes attributed to it, you may want to consider using a list instead. Also, here's a useful read on arrays/lists/hashtables, etc:

http://robotduck.wordpress.com/2009/11/04/88/

Comment
Stardog

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Peter G · May 26, 2011 at 08:07 PM 0
Share

@testure they are creating an array of arrays, just not expressing it in the variable declaration:

 inventory = new Array(inventorySizeX);

 for( var i = 0; i < inventory.length; i ++ ) 
 { 
     inventory[i] = new Array(inventorySizeY);
 } 
avatar image

Answer by ThunderAwesome · Sep 03, 2012 at 12:45 AM

This is a conversion of Der Dude's JavaScript code. I noticed you removed/changed some things in the OnGUI function. But here is his code converted for C#. The only addition I made was the "isInventoryOpen" boolean which you'll have to implement somewhere with a keypress or what have you. (You'll notice the class name is different, InventoryTestCS)

Also...this is a bit clunky. Lists would work best, but As long as you don't edit the size of the inventory in real-time, you should be okay.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class InventoryTestCS : MonoBehaviour {
     
     //public static InventoryTestCS statInventory;
     
     public bool isInventoryOpen = false; //my addition (used to show the inventory)
 
     public Texture emptyTex;           //This will be drawn when a slot is empty
     public const int inventorySizeX= 8;           //the size of the inventory in x and y dimension
     public const int inventorySizeY= 5;
     
     //Our inventory array
     InventoryItem[,] inventory;
 
     int iconWidthHeight= 20;                  //The pixel size (height and width) of an inventory slot
     int spacing= 4;                          //Space between slots (in x and y)
 
     public Vector2 offSet = new Vector2 (100, 100); //set the position of the inventory
 
 
     //Our Representation of an InventoryItem
     public class InventoryItem
     {
         //GameObject this item refers to
         public GameObject worldObject;
         //What the item will look like in the inventory
         public Texture texRepresentation;
     }
 
     // Create the Inventory
     void Awake()
     {
         inventory = new InventoryItem[inventorySizeX, inventorySizeY];
     }
 
     void OnGUI()
     {
         if(isInventoryOpen)//if the inventory is open display it
         {
             InventoryItem currentInventoryItem;
             //Go through each row
             for (int i= 0; i < inventorySizeX; i++)
             {
                 // and each column
                 for( int k = 0; k < inventorySizeY; k ++ )
                 {
                     currentInventoryItem = inventory[i,k];
                     
                     //if there is an item in the i-th row and the k-th column, draw it
                     if( inventory[i,k] == null )
                     {
                         GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), emptyTex );
                     }
                     else
                     {
                         GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), currentInventoryItem.texRepresentation );
                     }
                     
                     if(currentInventoryItem != null && 
                     GUI.Button( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), "", new GUIStyle("label") ))
                     {
                     
                         currentInventoryItem.worldObject.transform.position = transform.position;
                         currentInventoryItem.worldObject.transform.rotation = transform.rotation;
                         currentInventoryItem.worldObject.active = true;
                         
                         if(Input.GetMouseButtonUp(0))
                         {        
                             //Equip it
                             currentInventoryItem.worldObject.transform.parent = transform;
                             currentInventoryItem.worldObject.collider.enabled = false;
                         
                         } else if(Input.GetMouseButtonUp(1))
                         {
                             //Drop it
                             inventory[i,k] = null;    
                             currentInventoryItem.worldObject.transform.parent = null;
                         
                         }
                     }
                 }
                 
             }
         }
     }
 
     public void AddItem(InventoryItem item)
     {
         //Go through each row
         for (int i= 0; i < inventorySizeX; i++)
         {
             // and each column
             for (int k= 0; k < inventorySizeY; k++)
             {
                 //If the position is empty, add the new item and exit the function
                 if (inventory[i,k] == null)
                 {
                     inventory[i,k] = item;
                     return;
                 }
             }
         }
         //If we got this far, the inventory is full, do something appropriate here
     }
 
     public void AddItem(GameObject worldObject, Texture texRep){
         InventoryItem newItem= new InventoryItem();
         newItem.worldObject = worldObject;
         newItem.texRepresentation = texRep;
         AddItem(newItem);
     }
 
     void Update()
     {
         
         if(Input.GetKeyDown("i"))
         {
             isInventoryOpen = !isInventoryOpen;
         }
     }
 }

Also, if you are looking for a conversion of the InventoryWorldItem here it is:

 using UnityEngine;
 using System.Collections;
 
 public class InventoryWorldItem : MonoBehaviour {
 
     public Texture iconTex;
     public InventoryTestCS inventory;
     
     void Start()
     {
        //make sure the Player has the inventory script attached otherwise this won't work
         inventory = GameObject.FindWithTag("Player").GetComponent<InventoryTestCS>();
     }
 
     void OnMouseDown()
     {
         gameObject.active = false;
         
         InventoryTestCS.InventoryItem item = new InventoryTestCS.InventoryItem();
         item.worldObject = gameObject;
         item.texRepresentation = iconTex;
         inventory.AddItem(item);
     }
 }
 
Comment
Stardog

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ElectroSphere · Jan 19, 2013 at 11:09 PM 0
Share

Thank you so much Thunder, saved me a lot of time ! you are awesome !

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

unity Javascript to C# Array Conversion 1 Answer

C# convert Bytearray to System.Draw.Image 2 Answers

Converting a Javascript Drag and Drop script to C# 1 Answer

JS to C# conversion is a complete disaster 1 Answer

small example sendmessage from javascript to C# wrong??? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges