Unity3D: Converting from Javascript to C#, Arrays.

Javascript works ok for some things but many Unity developers have switched over to C# because it offers more powerful features like Events & Delegates.

Here’s an example of Javascript Arrays in Unity:

private var months= new Array();
private var monthsXpos= new Array();
...

	months.Push("January");
	months.Push("February");
	months.Push("March");
	months.Push("April");
	months.Push("May");
	months.Push("June");
	months.Push("July");
	months.Push("August");
	months.Push("Septemeber");
	months.Push("October");
	months.Push("Novemeber");
	months.Push("December"); 	
	
	monthsXpos = [  UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.38f ),  //January
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.376f ), //February
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.395f ), //March
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.405f ), //April
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.409f ), //May
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.405f ), //June
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.405f ), //July
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.389f ), //August
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.357f ), //September 
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.387f ), //October
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.363f ), //November
					UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.372f )];//December	

Note, in C# a simple array’s length is immutable. In other words, they have a set length that can’t be changed. There are other Collections objects in C# that allow for a list of variable length. Here’s the same thing in C# syntax:

	private string[] months;
	private float[] monthsXpos;
         ...
		months = new string[12] { "January",  
								  "February",	
								  "March",
								  "April",
								  "May",
								  "June",
								  "July",
								  "August",	
								  "Septemeber",
							      "October",	
							      "Novemeber",	
								  "December"};
		
		monthsXpos = new float[12] { UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.38f ),  //January
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.376f ), //February
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.395f ), //March
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.405f ), //April
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.409f ), //May
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.405f ), //June
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.405f ), //July
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.389f ), //August
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.357f ), //September 
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.387f ), //October
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.363f ), //November
									UIRelative.xPercentFrom( UIxAnchor.Left, Screen.width, 0.372f )};//December	
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s