unity3d

Unity – BG Music

Videogames Development – Unity – How to add a background music

LOAD A BG MUSIC

1. MAIN TOP MENU> Edit> Assets> Import New Asset…> your-bgmusic.mp3
2. DRAG AND DROP ‘your-bgmusic.mp3’ from ‘Project’ window to ‘Scene’ viewport

MODIFY A BG MUSIC

1. Hierarchy> select your-bgmusic.mp3
2. InspectorA setup parameters

SETUP CAMER TO LISTEN MUSIC

1. Hierarchy> select Main Camera
2. Inspector> check Audio Listener

NOTICE:
The Audio Listener acts as a microphone-like device. It receives input from any given Audio Source in the scene and plays sounds through the computer speakers.

By |Unity3D|Commenti disabilitati su Unity – BG Music

Unity – Scripting – If Statements

Videogames Development – Unity – If Statements (dichiarazioni If)

IF – ELSE

if (1 == a)
{
             // Do a set of actions
}
else
{
             // Do another set of actions
}

IF – ELSE IF

if (1 == a)
{
             // Do a set of actions
}
else if (2 -- b)
{
             // Do another set of actions
}
else
{
             // Do YET another set of actions
}

IF – NESTED (annidato)

if (1 == a)
 {
     if (2 ==b)
      {
             // Do a set of actions
      }
}
By |Unity3D|Commenti disabilitati su Unity – Scripting – If Statements

Unity – Scripting – Debug

Videogames Development – Unity – Debug.Log

Logs message to the Unity Console.

Statements:

// Message with a link to an object.
Debug.Log ("Hello", gameObject);

// Message using rich text.
Debug.Log("<color=red>Fatal error:</color> AssetBundle not found");

SCRIPT

MAIN TOP MENU> ASSETS> CREATE> Javascript, type the name ‘GetMousePosition’

Assets> GetMousePosition> Inspector> ‘Open…’ button

Write:

#pragma strict

function Start () {

}

function Update () {
	var mouse = Input.mousePosition;
        Debug.Log(mouse);
		
	}

Create an Object inside the Hierarchy ‘MyObject’

Assets> DRAG AND DROP ‘GetMousePosition’ script over Hierarchy> MyObject

Play> move the mouse, look at the BOTTOM OF THE SCREEN, you will see the coordinates as: (536.0, 583.0, 0.0)

By |JavaScript, Unity3D, Video Games Development|Commenti disabilitati su Unity – Scripting – Debug

Unity – Scripting – Array

Videogames Development – Unity – Array

An Array stores multiple values in a single variable.
The first array’s value is 0.

There are two types of arrays in Unity:
1- builtin arrays and normal Javascript Arrays
2- builtin arrays (native .NET arrays), are extremely fast and efficient but they can not be resized

Javascript Arrays

Assign this code to an Empty Object in the Scene:

#pragma strict

function Start()
{
    
}

function Update()
{
// Array Start
var mycars = new Array();
mycars[0] = "Saab"; // Notice: the first value is 0
mycars[1] = "Volvo";
mycars[2] = "BMW";
// Array End
 var i= 1; // Array index
 Debug.Log(mycars[i]); // The result is Volvo
}

Javascript Arrays: Print an Array Content

#pragma strict

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

Javascript Arrays: basic use

var myArray = new Array();     // declaration
myArray.Add(anItem);           // add an item to the end of the array
var thisItem = myArray[i];     // retrieve an item from position i (richiamare un contenuto dalla posizione i)
myArray.RemoveAt(i);           // removes an item from position i
var howBig = myArray.length;   // get the length of the Array

.NET Arrays

Syntax 1 – multiple lines

#pragma strict

// type [] name = new Array type[number of items]
int[] myIntArray = new Array int[3];

function Start () 
{
// name of array[index] = value
myIntArray[0] = 12;
myIntArray[1] = 16;
myIntArray[2] = 18;
}

Syntax 2 – one line

#pragma strict

// type [] name = new Array type[number of items]
int[] myIntArray = {12, 16, 18};

function Start () 
{

}

Public Array – you can see them in Inspector window

The code below creates an Array of Game Objects with Tag ‘Player’

#pragma strict

public var players : GameObject[];

function Start ()
{
    players = GameObject.FindGameObjectsWithTag("Player");
    
    for(var i : int = 0; i < players.Length; i++)
    {
        Debug.Log("Player Number "+i+" is named "+players[i].name);
    }
}
By |Unity3D|Commenti disabilitati su Unity – Scripting – Array

Unity – Scripting – Delay Execution Time

Videogames Development – Unity – Delay Execution Time

yield WaitForSeconds

#pragma strict

function Start()
{
// Prints 0
print (Time.time);
// Waits 5 seconds
yield WaitForSeconds (5);
// Prints 5.0
print (Time.time);    
}

function Update()
{

}

NOTICE:
yield WaitForSeconds (5); -> yield (dare la precedenza) Aspetta per Secondi 5

Invoke

#pragma strict
 
function Start()
{
    // Invoke (function name, delay time in seconds)
    Invoke ("myFunction", 2);
    Invoke ("myFunctionTwo", 4);
}
    
function myFunction()
{
    Debug.Log("Delay time of 2 seconds");
}

function myFunctionTwo()
{
    Debug.Log("Delay time of 4 seconds");
}

Invoke Repeat

#pragma strict
 
var x: int;

function Start()
{
    // InvokeRepeiting (function name, delay time in seconds, repeat every x seconds...)
    InvokeRepeating("myFunction", 2, 1);
}

function myFunction()
{
    var x = x++;
    Debug.Log("Invoked " + x);
}

The result is:

Invoked 0
Invoked 1
Invoked 2
Invoked 3
… to infinity and beyond!

By |Unity3D|Commenti disabilitati su Unity – Scripting – Delay Execution Time