Posted on 01.12.11

First, better to say that optimize performance is something to do at the end.
Some of the code can be much faster but less readable, so better leave this for the end of the project.

array.
When you create an array
[] is 3 times faster than new Array() ( same for Object , {} 3 time faster than new Object() )


//wrong
var arr : Array = new Array( 1 , 2 , 3 , 4 ) ;
for( var i : int = 0 ; i < arr.length ; ++i )
{
//
}

//good
var arr : Array = new Array( 1 , 2 , 3 , 4 ) ;
var l : int = arr.length ;
var i : int ;
for( i = 0 ; i < l ; i++ )
{
//
}

So, calculate the length of array before the loop, is a massive speed up when loopin an array.
And remember
Arrays do take a considerable amount of time to create.
Retrieving a value is almost twice as fast as setting a value.

Array VS Vector
Array and Vector are similar in performance. You will see a big difference in performance when you do this

var c : Boolean = myArray[ j ].booleanProp
//this is slower than this :
var obj : Object = myArray[ i ] ;
var c : Boolean = obj.booleanProp
// that is similar if you use Vector as this
var arrVector : Vector.;

Avoid push(), pop(), shift() or unshift();

if else VS switch.
If else or switch doesnt have a relevant difference.

++
++i or i++ doesnt have any relevant difference, but better than i = i + 1

int and uint.
int is significantly faster than uint, and miles faster than Number, used as iterator in a loop
so never for( var i : uint = 0 ...

moltiplication and division by power of 2.
if you need an integer, divide by 2 in this was. this run almost double faster.
Here the rest of operation


var nr : int = 10 >> 1  // var nr : int = 10 / 2
var nr : int = 10 >> 2  // var nr : int = 10 / 4

var nr : int = 10 << 1  // var nr : int = 10*2
var nr : int = 10 << 2  // var nr : int = 10*4
var nr : int = 10 << 4  // var nr : int = 10*16

getter and setter.
Are your enemy! in any loop try to have direct access to variable, and avoid any call to other methods or getter and setter. ( now you can understand why we should optimize later, closer to the end ).
Direct access of class members is three times as fast as accessing via a function.

Math.floor VS int
Math.floor( number ) is 10 time slower than int( number )

Math.abs VS ifelse
Math.abs( number ) is A LOT slower than
number = number < 0 ? -number : number
//or this even faster
if( number < 0 ) number = -number ;

Math.ceil VS ifelse
Math.ceil( number ) is damn slower than
number = number == int(number) ? number : int(number)+1

Math.sin or Math.cos
Use a lookiup table if you are using only integer angle. you will save time in calculation of sin, and conversion between radians and degree

What you can do
Give your variables decent names, you are not increasing performance by using shorter names or abbreviations.


Setting up FDT with Starling is quite strightforward. But you need to trick a bit, as there is a misunderstanding between FDT and Starling for having some naming in the class path.

Download Starling.

Download the new playerglobal.swc for Flash Player 11.

Download the latest Flex SDK ( 4.5 )

Follow this steps to set up FDT

Cool , now you can publish for player 11, and have access to the wonderland.
When you copy Starling to your FDT project, you will have a lot of errors and warning, caused by the fact that most of the Starling API have same name as flash display and event. So if you use import starling.display.Stage, you will get error if , in your script , need to access the flash.display.Stage, even if you use the full description of the path class. So easy solution.
Grab the com and starling directory, and copy in a directory that you can call as you want. Add this to the Classpath ( right-click on the directory, in the Flash Explorer panel etc ).
To remove all that annoying errors, just right click on the project -> properties -> FDT Build Path
select the starling directory, and uncheck ‘Generate Problems/Task’.
Someone else suggest to create a swc, but i think this will update quite soon, so maybe is better to keep like that.
Spot on, you are in Starling :)
 


Hi there,
The scope of this tutorial is to realize this
http://www.otlabs.net/blog/stuff/tutorials/unity2dpart1/

In part2 etc we will start to analyze the interaction with the character, and how to do a scrolling platform world

What you need
Unity 3.3
Sprite Manager ( http://www.anbsoft.com/middleware/sm2/ ) ( it cost a few dollars, but without is bloody hard to work in 2d with unity )
Zip with necessary files

So Lets start

Create a Project in Unity ( File -> New Project )
Create a new Scene ( File -> New Scene )

First we need to import the Sprite Manager, that is a set of scripts that will help us to manage 2d stuff, and 2d animations.
So,
Assets -> Import Package and locate the Sprite Manager directory

Once this is done, add this script as well, as we will need a lot to edit multiple images
http://www.unifycommunity.com/wiki/index.php?title=TextureImportSettings

Ok so now lets import the Assets to create our running hero.
Go in the stuff directory you downloaded ( here ) and copy, in the Assets folder of the unity project, the ‘animation-files’ directory.

Select all the images, and press Custom from the main menu
Custom -> Texture ->Change Texture Type
and Select Advanced

Select all the images, and press Custom from the main menu
Custom -> Texture ->Change Non Power of 2
and Select None

Select all the images, and press Custom from the main menu
Custom -> Texture ->Change MiniMap
and Select Disable MiniMap

Finally our images are ready to be used , to create our flat Hero
First we will create a single Hero, with a set of animations ( run , jump , fall , etc )
In part 2 we will create a multiple Hero, that means a set of Heroes ( for example different clothes ) , all of them with a set of animations ( run , jump , etc ) ;

Create a new GameObject. name it as HeroManSingle

*** this is not essential.. its just to have a visual object on the stage
go to Component -> Mesh -> Mesh Filter
Go in the editor and select the material button ( bottom right in the Editor )
Select a Capsule
go to Component -> Mesh -> Mesh Renderer
go in the editor and select the material button , and select Default-Diffuse
You should see now a capsule. you can hide or show the capsule with thick in the editor, in the mesh render section.
This will be our ‘movieclip’. We can actually skip this part, and this is just to have a visual object on the screen…
****end of not essential part

Now lets give this some physics to the gameobject ( this is essential ).
Go to Component -> Physics -> RigidBody
Finally lets add some physics that will use for collision so
Go to Component -> Physics -> Sphere Collider, and set radius = 0.4 ( this have to adapt to your particular design )

Lets create now the Packed Sprite that will contains our animations
Create a GameObject and name it HeroBlue
go to Component -> Scripts and add PackedSprite
go to Window and press on Sprite Timeline. So drag on the Texture window of the Sprite timeline the first picture of your running hero sequence.
Press + and add a new animation and call it Run
drag all the 10 frames representing the hero running.
Do the same for Jump and Falling and call like that.
At this point you should see this in Editor ( as fig1 )
fig1

Good. Lets drag now the HeroBlue in HeroManSingle.
Now lets give some material to our Hero. Basically we created the PackedSprite, and now we need to build the Atlas, that is the image containing all the sequences of the animations.

In Assets create a directory and call Material, or as you wish. right click on it in the Project window, and
create -> Material
Select the material you just created , and call it HeroBlue, as your GameObject. This is not essential, but help you keep track of stuff.
On the material , in the shader drop down menu choose
Transparent Vertex Colored Fast. You can find different option, but I found this really good for performance, especially if you are working for mobile.

So now we have our material, lets write stuff on it.
Select from your Hierarchy the HeroBlue GameObject. Scroll Down the Editor and drag this material we just created on the material field in the MeshRenderer.
Now Select again the HeroBlue material, and go on the main menu
Tools -> A&B Software -> Build Atlas for Material and press create
If you double click on the Text Icon, the UI will show you , in the Editor, the settings for the atlas. Here you can play with the compression to find the best quality/performance settings.

Now Select the PackedSprite ( in our case HeroBlue ) and go
Tools -> A&B Software -> Size Sprites, and choose the right proportion.
Don’t forget to select , in the editor of the HeroBlue Packed Sprite the option ‘AutoResize’

Now you should see your hero on the screen, and if you press play you should see him falling, stopped on the first frame of Run. If you don’t see on the screen, you should see it after you press play ( unity acting a bit weird )

Now lets put some platforms, so our hero will stop falling down and we can start interacting with it.
So lets build a platform.

Create an GameObject. name it as Platform.
Give it a Mesh Filter ( Cube ) and a Mesh Renderer. Scale as you wish and at the end add a Physics -> Box Coolider.
If Z are in the right position, for hero and platform ( should be the same, so the two bodies will collide ) , the Hero will fall on the platform and stop, when you press Play.


Posted on 29.06.11

God, this is heavy. With this command you can destroy stuff in your library ( Resources ) run time…
Super

GameObject smoke = Resources.Load(YOUR_ADDRESS + “SmokeJump”) as GameObject;

DestroyImmediate( smoke , true ) ;

gone. your stuff is gone from Resources … for ever :)


Posted on 23.05.11

I had to go through a few steps, to make it working properly, so i am trying to replicate here what i did.
First, I didnt find yet a way to tween ANY parameter, as i am doing with Tweened on AS3. So the solution i came up is to create an empty GameObject, tween is X position, and on update, read the x position, and update my abstract var.


ashtable ht = new Hashtable();
ht.Add("x", 10 );
ht.Add("time", .6);
ht.Add("oncomplete", "onCompleteTest");
ht.Add("onCompleteTarget" , gameObject ) ;
//my empty game Object is stored in gv as GO_iTween )
Debug.Log("gv.GO_iTween :" + GameVars.GO_iTween ) ;
iTween.MoveTo( gv.GO_iTween , ht) ;

As you can see i am using onCompleteTarget. Sounds any bell? yes.. it is as you are back in AS2, where the tween try to fire , on complete event , on the gameObject you are moving. So if you want to fire an event in the class you create the tween, you need to set the target.

I’ll post more soon about onUpdate, or any other bloody way i can find it


« Previous Entries
Category: