programming

Unity 3D Game Engine – Materials – Fade Between 2 Materials

Unity 3D Game Engine – Materials – Switch Materials

1. Project> RMB> Create> Material> Create: material1 and material2

2. MAIN TOP MENU> Gameobject> Create Other> Cube, assign the script:

SwitchMaterial.JS


#pragma strict

	var material1 : Material; // assign in Inspector
	var material2 : Material; // assign in Inspector
	var transitionTime = 2.0;

	function Start () {
	}

	function Update () {
	ChangeMaterial();
	
	}
	
	function ChangeMaterial () {
		renderer.material = material1;
		yield WaitForSeconds (transitionTime); // wait seconds... -> non può essere inserito all'interno di Update()
		renderer.material = material2;         // switch to material2
		
	}

3. Project DRAG AND DROP material1 and material2 over Inspector> SwitchMaterial.JS> material1 slot | material2 slot

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Materials – Fade Between 2 Materials

Unity 3D Game Engine – Set Material Transparency

Unity 3D Game Engine – Set Material Transparency

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Transparent/Diffuse

NOTICE; YOu HAVE TO USE a Transparent shader, in Unity3D only Transparent Shaders support alpha channel

3. Select the Cube and assign SetTransparency.JS:


#pragma strict

	function Start () {
	}

	function Update () {
		gameObject.renderer.material.color.a = 0.5; // set transparency to 50%
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Set Material Transparency

Unity 3D Game Engine – Fade Out Transparency

Unity 3D Game Engine – Fade Out Transparency

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Transparent/Diffuse

NOTICE; YOu HAVE TO USE a Transparent shader, in Unity3D only Transparent Shaders support alpha channel

3. Select the Cube and assign FadeOutTransparency.JS:


#pragma strict

	// Fades from minimum to maximum in seconds
	var minimum = 1.0;
	var maximum = 0.0;
	var fadeoutSpeed = 1f; // 0.1 slow, 2 fast

	function Start () {
	}

	function Update () {	
		gameObject.renderer.material.color.a = Mathf.Lerp (minimum, maximum, fadeoutSpeed * Time.time);
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Fade Out Transparency

Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

1. MAIN TOP MENU> Gameobject> Create Other> Cube

2. Hierarchy, select the Cube> Inspector> materal slot, select Shader: Diffuse

NOTICE: the script will fade between ‘Shader> ‘Main Color’ values, you HAVE TO REMOVE all textures if you want to see ‘Main Color’!

3. Add to the Cube Fade.JS


#pragma strict

	// Fade the color from red to green
	// back and forth over the defined duration
	var colorStart : Color = Color.red;
	var colorEnd : Color = Color.green;
	var duration : float = 1.0;

function Start () {

}


	function Update () {
		var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
		renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
	}

4. Done!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D Game Engine – Materials – Fade Between 2 Base Colors

JS Basics – Passing variables between functions attached to different GameObjects

JS Basics – Passing variables between functions attached to different GameObjects

Hierarchy:

– Cube -> attach CubeScript.js

– GameController (Empty GameObject), Inspector> Tag rollout ‘GameController’ -> attach GameController.JS

CubeScript.js


#pragma strict

// definisco la variabile per il punteggio
var scoreValue : int; 

// variabile privata, non visibile in Inspector, mi serve per ottenere il componente GameController
private var gameController : GameController; 

function Start ()
{
    // inserisco in una variabile l'oggetto con tag GameController
    var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController");
    // se l'oggetto con tag GameController esiste lo inserisco in una variabile
    if (gameControllerObject != null)
    {
        gameController = gameControllerObject.GetComponent (GameController);
    }
    // se l'oggetto con tag GameController non esiste restituisce un messaggio di errore
    if (gameController == null)
    {
        Debug.Log ("Cannot find 'GameController' script");
    }
}

function Update () {
SendData();
}

function SendData() {
    yield WaitForSeconds (3); // aspetta 3 secondi
    scoreValue = 10;
    // invia scoreValue allo script taggato GameController -> funzione AddScore()
    gameController.AddScore (scoreValue);
    Destroy(gameObject); // distruggi l'oggetto corrente altrimenti l'invio di dati si ripete ad ogni frame

}

GameController.JS


#pragma strict

// contatore di punteggio, private perchè non vogliamo che sia modificabile da Inspector
private  var score : int;

function Start () {

}

function Update () {

}

function AddScore (newScoreValue : int) {
    // aggiorna score aggiungendo il valore newScoreValue
    // che gli viene inviato da CubeScript.js
    // alla riga:  - gameController.AddScore (scoreValue); -
    score += newScoreValue;
    Debug.Log(score);  
    
}

The final console result is: 10

Spiegazione:

1. CubeScript.js
a. cerca un GameObject con il tag GameController
b. se presente ottiene dall’oggetto GameController il componente GameController (che non è altro che GameController.JS)
c. invia a GameController.JS -> funzione ‘AddScore()’ la variabile ‘scoreValue’;

2. GameController.JS
a. riceve nella funzione ‘AddScore()’ la variabile ‘scoreValue’
b. assegna ‘scoreValue’ alla variabile ‘newScoreValue’
c. effettua i calcoli e visualizza in console il risultato finale.

By |Unity3D, Video Games Development|Commenti disabilitati su JS Basics – Passing variables between functions attached to different GameObjects