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

Unity – Destroy Components

Unity – Destroy Components

Removes a gameobject, component or asset.

DESTROY OBJECT

DRAG AND DROP to an object in the Hierarchy

#pragma strict

function Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        Destroy(gameObject);
    }
}

Run and press Key to destroy object

DELAY DESTRUCTION

DRAG AND DROP to an object in the Hierarchy

#pragma strict

function Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        Destroy(gameObject,3f);
    }
}

Run and press Key to destroy object after 3 seconds ‘3f’

DESTROY OTHER OBJECTS

1. DRAG AND DROP to an object in the Hierarchy

#pragma strict

public var other : GameObject;


function Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        Destroy(other);
    }
}

2. Inspector> variable ‘Other’> small icon to select an object to destroy (onother object)

DESTROY COMPONENTS WITHOUT REMOVE OBJECT

1.DRAG AND DROP to an object in the Hierarchy

#pragma strict

function Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        Destroy(GetComponent(SpriteRenderer));
    }
}

2. Play, look at ‘Inspector’, and press ‘Space Bar’, the component ‘Sprite Renderer’ will disappear.

DESTROY EVERYTHING AHHH!!!! – EXAMPLES LIST

        // Kills the game object
	Destroy (gameObject);

	// Removes this script instance from the game object
	Destroy (this);

	// Removes the rigidbody from the game object
	Destroy (rigidbody);
	
	// Kills the game object in 5 seconds after loading the object
	Destroy (gameObject, 5);

	// When the user presses Ctrl, it will remove the script 
	// named FooScript from the game object
	function Update () {
		if (Input.GetButton ("Fire1") && GetComponent (FooScript))
			Destroy (GetComponent (FooScript));
	}
By |Unity3D|Commenti disabilitati su Unity – Destroy Components

Unity – Enabling and Disabling Components

Unity – Enabling and Disabling Components

Attach this code to a light:

#pragma strict

private var myLight : Light;


function Start ()
{
    myLight = GetComponent(Light);
}


function Update ()
{
    if(Input.GetKeyUp(KeyCode.Space)) // click space bar to toggle light on/off
    {
        myLight.enabled = !myLight.enabled; // invert the status using not keyword
    }
}

Statemets:

myLight.enabled = myLight.enabled; -> light is enabled

myLight.enabled = !myLight.enabled; -> light is disabled, you can use not keyword ‘!’

By |Unity3D|Commenti disabilitati su Unity – Enabling and Disabling Components

Unity – Get Component

Unity – Get Component

Get the values of others components.

1. Create an Object

2. Assign to this object -> AnotherScript.js:

#pragma strict

public var playerScore : int = 9001;

3. Assign to the SAME OBJECT -> MainScript.js:

#pragma strict

private var anotherScript : AnotherScript;

function Awake ()
{
    anotherScript = GetComponent(AnotherScript);   
}


function Start ()
{
    Debug.Log("The player's score is " + anotherScript.playerScore);
  
}

The Result inside Console will be ‘The player’s score is 9001’

Notice:
Outside functions -> private var anotherScript : AnotherScript;
Inside Awake() -> anotherScript = GetComponent(AnotherScript); -> Statement: GetComponent(Component Name)
Inside Start() -> + anotherScript.playerScore -> Statement: variable of GetComponent.variable of Component

By |Unity3D|Commenti disabilitati su Unity – Get Component