Video Games Development

Unity – Loops – FOR – WHILE – DO WHILE – FOR EACH

Unity – Loops – FOR – WHILE – DO WHILE – FOR EACH

In Unity loops can execute a block of code a number of times.

FOR

#pragma strict

var numEnemies : int = 3;


function Start ()
{
    for(var i : int = 0; i < numEnemies; i++)
    {
        Debug.Log("Creating enemy number: " + i);
    }
}

The result is:
Creating enemy number: 0
Creating enemy number: 1
Creating enemy number: 2

WHILE

#pragma strict

var cupsInTheSink : int = 4;


function Start ()
{
    while(cupsInTheSink > 0)
    {
        Debug.Log ("I've washed a cup!");
        cupsInTheSink--;
    }
}

The result is:
I’ve washed a cup! (number 4)
I’ve washed a cup! (number 3)
I’ve washed a cup! (number 2)
I’ve washed a cup! (number 1)

DO WHILE

#pragma strict

function Start()
{
    var shouldContinue : Boolean = false;
        
    do
    {
        print ("Hello World");
            
    }while(shouldContinue == true);
}
[

The result is:
Hello World if shouldContinue == true

<h2>FOR EACH and ARRAYS</h2>

#pragma strict

function Start () 
{
    var strings = ["First string", "Second string", "Third string"];
    
    for(var item : String in strings)
    {
        print (item);
    }
}

The result is:

First string
Second string
Third string

By |Unity3D|Commenti disabilitati su Unity – Loops – FOR – WHILE – DO WHILE – FOR EACH

Unity – Time.deltaTime

Unity – Time.deltaTime

The time in seconds it took to complete the last frame (Read Only).
Use this function to make your game frame rate independent, and for smoot movements.

#pragma strict

function Update () {
		// Move the object 10 meters per second!
		var translation : float = Time.deltaTime * 10;
		transform.Translate (translation, 0, 0);
	}
By |Unity3D|Commenti disabilitati su Unity – Time.deltaTime

Unity – Activating GameObjects

Unity – Activating GameObjects

Activate Object

DRAG AND DROP this script over a object in the Hierarchy:

#pragma strict

function Start ()
{
    gameObject.SetActive(false); // Deactivate an object
}

Check State

Create an object in the scene and give it the name ‘GreatObject’

Create this Script and give it the name ‘MyScript’

#pragma strict

public var myObject :GameObject;


function Start ()
{
    Debug.Log("Active Self: " + myObject.activeSelf);
    Debug.Log("Active in Hierarchy" + myObject.activeInHierarchy);
}

1. DRAG AND DROP this script over ‘GreatObject’ in the Hierarchy
2.Select the ‘GreatObject’> Inspector> MyScript> myObject> the slot is ‘None’, on the right ‘Select Game object’ small icon> Scene> select ‘GreatObject’ from the scene, NOW ‘public var myObject = GreatObject’, you will Debug.Log it

NOTICE:

– if you disable a PARENT object also CHILD will be disabled.
– if you disable a CHILD object the PARENT does not change his original status.

By |Unity3D|Commenti disabilitati su Unity – Activating GameObjects

Unity – How optimize building

Unity – How optimize building

IMAGES

Projects> Assets> select an Image> Inspector> Max Size / Format

SOUND EFX

Projects> Assets> select an Sound EFX> Inspector> Audio Format / Load Type / Compression (kbps)

BUILD

MAIN TOP MENU> File> Build Settings…> ‘Player Settings’ button (inteso come il player Unity)> Inspector

RENDER MODE – 3D SCENES ONLY

– Vertex Lighting
-> fastest
-> it interpolare lighting over the surface of the mesh
-> it NOT allow Normal mapping / Light Cookies / Real Time Shadows

– Per pixel Lighting
-> best quality
-> it calculate lighting in every screen pixel
-> it allow Normal mapping / Light Cookies / Real Time Shadows

Setup:

MAIN TOP MENU> Edit> Project Settings> Quality> Pixel Light Count, maggiore è il valore, maggiore qualità, minore velocità di rendering

Hierarchy> select Light> Inspector> Render Mode> Important (Per pixel Lighting) / Not important (Vertex Lighting)

By |Unity3D|Commenti disabilitati su Unity – How optimize building

Unity – Enumerations

Unity – Enumerations

Default values:

#pragma strict

// The default values are North=0 East=1 South=2 etc...
enum Direction{North, East, South, West};

function Start () 
{
    var myDirection : Direction;
    
    myDirection = Direction.South; // the result is 2
}

Declared values:

#pragma strict

// The first value can be declared
enum Direction{North=1, East, South, West};

function Start () 
{
    var myDirection : Direction;
    
    myDirection = Direction.South; // the result is 3
}
#pragma strict

// All values can be declared
enum Direction{North = 15, East = 18, South = 34, West = 35};

function Start () 
{
    var myDirection : Direction;
    
    myDirection = Direction.South; // the result is 34
}
#pragma strict

enum Direction{North, East, South, West};

function Start () 
{
    var myDirection : Direction;
    
    myDirection = Direction.North;
}

function ReverseDirection (dir : Direction) : Direction
{
    if(dir == Direction.North)
        dir = Direction.South;
    else if(dir == Direction.South)
        dir = Direction.North;
    else if(dir == Direction.East)
        dir = Direction.West;
    else if(dir == Direction.West)
        dir = Direction.East;
    
    return dir;     
}
By |Unity3D|Commenti disabilitati su Unity – Enumerations