programming

Unity3D Game Engine – Materials Array

Unity3D Game Engine – Materials Array

Array of Materials

Create a scene with:

– Main Camera

– 3 Materials blue-green-red

– Cube, assign the script SwitchMaterials.js:


#pragma strict
 
var materials : Material[];
var materials_counter : int = 0; // 1. Setup this in Inspector! It setup the array lenght!
                                 // 2. DRAG AND DROP materials inside slots
function Update() 
{
    if(Input.GetKeyDown("f")) // press f key to switch materials
    {
        renderer.material = materials[GetNextMaterialIndex()]; // richiama l'indice del materiale da visualizzare
                                                               // risulta -> render.material = materials[0]
    }
}
 
function GetNextMaterialIndex() : int
{
    materials_counter++; // incrementa di 1 l'indice
    if (materials_counter==materials.Length)
    {
        materials_counter = 0; // se l'indice è già uguale alla lunghezza massima riporta il valore a 0
    }
    return materials_counter; // ritorna l'indice dell'array
}

Cube> Inspector> SwitchMaterials.js>
1. Dare un valore a Materials Size (è la lunghezza dell’indice)
2. DRAG AND DROP i materiali sugli slot Element 0-1-2 etc…

Play> press f to switch materials.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D Game Engine – Materials Array

Unity3D – 2D Games – Pixel Perfect 2D Game

Unity3D – 2D Games – Pixel Perfect 2D Game

Un rendering ‘pixel perfect’ lo abbiamo quando la risoluzione in uscita di un gioco e la dimensione di uno sprite corrispondono in pixel.
Immaginiamo di avere un progetto in WebPlayer di 800×600 pixel, di importare uno sprite di 400×600, se lo sprite ricopre metà del display allora è un rendering ‘pixel perfect’

Unity3D isn’t designed for pixel perfect rendering, this cause artifacts, and pixel blurring.

Perfect user interface buttons

To remove artifacts you have to do:

1. Import Texture 512×512
Migliora l’interpolazione di Unity3D

2. Texture> Inspector>
– Filter Mode: Bilinear
Pulisce l’interno della texture

Format TrueColor
Elimina la compressione del colore per far scomparire l’effetto squadrettato tipo JPG

3. Material> Shader Transparent/Cutout/Soft Edge Unlit
Rende più morbido il bordo della maschera alfa

Perfect spritesheet

To remove artifacts you have to do:

– You will likely need a texture with “power of 2” dimensions, e.g. 256 – 512 – 1024px.
– Texture filter mode should be “point”
– Turn off texture compression
– If you are using Unity ‘Sprite’ texture type, take care with your sprite pixels to units setting
– Take care with your orthographic camera scale relative to your screen dimensions

If you want the orthographic camera to match your screen resolution, then set the camera Size to be half of your screen height. So, in this case, if your window is 600 pixels, set your camera Size value to be 300.

For italian people:

– Importare texture multiple di 2px ‘128 – 256 – 512 – 1024px’
-> per evitare l’interpolazione applicata da Unity automaticamente per portarle a dimensioni multiple di 2

– Project> my-spritesheet> Inspector> Filter Mode> Point
-> RENDE I PIXEL PIU’ DEFINITI perchè toglie i filtri bilineari e trilineari

– Project> my-spritesheet> Inspector> Default> Format> Truecolor
-> elimina la compressione dei colori della texture

– Project> my-spritesheet> Inspector> Pixels to Units
-> definisce la corrispondenza tra il numero di pixel e le unità Unity3D ad esempio: 100px per 1 unità Unity3D
Ottimizzare questo parametro per gestire la dimensione degli sprite a video.

– Main Camera> Projection> Orthographic
-> per utilizzare una camera senza deformazioni prospettiche.

– Main Camera> Size
-> definisce quante unità Unity3D in altezza sarà la vista della camera, ad esempio: 15 significa che la camera in altezza inquadrerà lo spazio di 15 unità.
Ottimizzare questo parametro per gestire la dimensione degli sprite a video.

– MAIN TOP MENU> Edit> Project Settings> Quality> Texture Quality> Full Res

Se si vuole che la camera ortografica corrisponda alla risoluzione a video, settare il camera ‘Size’ alla metà dell’altezza della risoluzione a video. Ad esempio se l’altezza del video è 600px il camera ‘Size’ sarà 300.

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – 2D Games – Pixel Perfect 2D Game

Unity3D – Texture Mover – Javascript

Unity3D – Texture Mover – Javascript

How to move texture via code.

Create a Plane with
– Material with a _MainTex (if you use a diffuse material it is the main color texture)
– TextureMover.js

Simple code:


#pragma strict

    // Scroll main texture based on time
	var scrollSpeed : float; // Assign in Inspector, start try 0.5

function Start () {
    // setup Main Texture (scaleX,scaleY) 
    // x = 0.5 perchè la texture che uso è il doppio in larghezza della dimensione del piano
    // il piano è 16:9, ta texture 32:9 per avere un landscape più ampio
	renderer.material.SetTextureScale ("_MainTex", Vector2(0.5,1));
}// END Start()

function Update () {
        var offset : float = Time.time * scrollSpeed;
		renderer.material.SetTextureOffset ("_MainTex", Vector2(offset,0));
}// END Update()

Play to see the final result

Pro code:


#pragma strict

    var isVertical : boolean = false; // check in Inspector if you need a vertical scrool 
    
    var invertDirection : boolean = false; // check in Inspector if you need change scrool direction
    
    // Scroll main texture based on time
	var scrollSpeed : float; // Assign in Inspector, start try 0.5
	
	var scaleX : float; // X scale of the texture
	var scaleY : float; // X scale of the texture

function Start () {

    // setup Main Texture (scaleX,scaleY) START ###############################################
    // ad esempio scaleX = 0.5 se la texture che uso è il doppio in larghezza della dimensione del piano
    // il piano è 16:9, la texture 32:9 
	renderer.material.SetTextureScale ("_MainTex", Vector2(scaleX,scaleY));
	// setup Main Texture (scaleX,scaleY) END #################################################
	
}// END Start()

function Update () {

        MoveTexture(); // devo richiamarla da update perchè si deve aggiornare ad ogni frame
        
}// END Update()

function MoveTexture(){

        var offset : float; // lo spostamento della texture...
        
        // left->right or right->left or up->down or down->up ----------------------------------------------
	    if (invertDirection){ // positivo
	    offset = Time.time * -scrollSpeed; 
	    } 
	    
	     if (!invertDirection){ // negativo
	    offset = Time.time * scrollSpeed; 
	    }
        // vertical or horizontal  -------------------------------------------------------------------------
	    if (isVertical) { // in verticale 
	        renderer.material.mainTextureOffset = Vector2 (0, offset);  
	    }else{  // in orizzontale
	        renderer.material.mainTextureOffset = Vector2 (offset, 0);  
	    } 
  
}// END MoveTexture()


Setup in Inspector the value of variables

Play, you can change values on the fly into Inspector!

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Texture Mover – Javascript

Unity3D – Mouse Buttons Codes

Unity3D – Mouse Buttons Codes


        // Detects clicks from the mouse and prints a message
	// depending on the click detected.

	function Update() {
		if(Input.GetMouseButtonDown(0))
			Debug.Log("Pressed left click.");

		if(Input.GetMouseButtonDown(1))
			Debug.Log("Pressed right click.");

		if(Input.GetMouseButtonDown(2))
			Debug.Log("Pressed middle click.");
	}

By |Unity3D, Video Games Development|Commenti disabilitati su Unity3D – Mouse Buttons Codes

Unity 3D – Key Sequence Input – Basics – JavaScript

Unity 3D – Key Sequence Input – Basics – JavaScript

NOTA BENE: in tutti gli script seguenti la sequenza NON VERRA’ RESETTATA in caso di errore!

Attach this script to a Gameobject in the scene

(KeyCode.) Sequence of 2 Keys – Basic

KeySequenceInput.js


// Key Sequence Input Controller
// Attach this script to a Gameobject in the scene

private var firstDown : boolean = false; // false at the beginning, I have no typed yet
private var allDown : boolean = false;   // false at the beginning, I have no typed yet
 
function Update() {

// Key sequence input controller START ######################
if(Input.GetKeyDown(KeyCode.UpArrow)) {
// highlight the button or play a sound
firstDown = true; //1. return true if you push first key
}
                  //2. if the first key is true 
if(firstDown) {
if(Input.GetKeyDown(KeyCode.RightArrow)) {
// highlight the button or play a sound
allDown = true;   //3. return true if you push first and second key
}                 
}

//4. if first and second key are true do someting...
if(allDown) {
Debug.Log ("Sequence OK!"); // Debug Code
//Do Something...
firstDown = false; // reset variables
allDown = false;   
}
// Key sequence input controller END ########################

}

Play the game and type the keys: UpArrow – RightArrow

(KeyCode.) Sequence of 4 keys – Basic


// Key Sequence Input Controller
// Attach this script to a Gameobject in the scene

private var firstDown : boolean = false;  // false at the beginning, I have no typed yet
private var secondDown : boolean = false; // false at the beginning, I have no typed yet
private var thirdDown : boolean = false;  // false at the beginning, I have no typed yet
private var allDown : boolean = false;    // false at the beginning, I have no typed yet
 
function Update() {

// Key sequence input controller START ######################
if(Input.GetKeyDown(KeyCode.UpArrow)) {
// highlight the button or play a sound
firstDown = true; //1. return true if you push first key
}
                  //2. if the first key is true 

if(firstDown) {
if(Input.GetKeyDown(KeyCode.RightArrow)) {
// highlight the button or play a sound
secondDown = true;//3. return true if you push first and second key
}                 
}

if(secondDown) {
if(Input.GetKeyDown(KeyCode.DownArrow)) {
// highlight the button or play a sound
thirdDown = true; //4. return true if you push first and second key and third
}                 
}

if(thirdDown) {
if(Input.GetKeyDown(KeyCode.LeftArrow)) {
// highlight the button or play a sound
allDown = true;  //5. return true if you push first and second key and third and fourth
}                 
}

//4. if all sequence is true do someting...
if(allDown) {
Debug.Log ("Sequence OK!"); // Debug Code
//Do Something...
firstDown = false; // reset variables
secondDown = false;
thirdDown = false;
allDown = false;   
}
// Key sequence input controller END ########################

}

Play the game and type the keys: UpArrow – RightArrow – DownArrow – LeftArrow

(KeyCode.) Sequence of 4 keys – Basic + CountDown


// Key Sequence Input Controller
// Attach this script to a Gameobject in the scene

private var firstDown : boolean = false;  // false at the beginning, I have no typed yet
private var secondDown : boolean = false; // false at the beginning, I have no typed yet
private var thirdDown : boolean = false;  // false at the beginning, I have no typed yet
private var allDown : boolean = false;    // false at the beginning, I have no typed yet

private var endTime : float; // countdown variable

function Start()
{
    endTime = Time.time + 3; // countdown variable: 3 seconds to type the right sequence!
}
 
function Update() {

    // COUNTDOWN START #########################
    var timeLeft : int = endTime - Time.time; 
    // We do not need negative time
    if (timeLeft < 0){ 
        timeLeft = 0;
    	        firstDown = false;   // reset variables, You Fail!
		secondDown = false;
		thirdDown = false;
		allDown = false;
		// Retry or Abort...
		}
    // COUNTDOWN END ###########################

// Key sequence input controller START ######################
if(Input.GetKeyDown(KeyCode.UpArrow)) {
// highlight the button or play a sound
firstDown = true; //1. return true if you push first key
}
                  //2. if the first key is true 

if(firstDown) {
if(Input.GetKeyDown(KeyCode.RightArrow)) {
// highlight the button or play a sound
secondDown = true;//3. return true if you push first and second key
}                 
}

if(secondDown) {
if(Input.GetKeyDown(KeyCode.DownArrow)) {
// highlight the button or play a sound
thirdDown = true; //4. return true if you push first and second key and third
}                 
}

if(thirdDown) {
if(Input.GetKeyDown(KeyCode.LeftArrow)) {
// highlight the button or play a sound
allDown = true;  //5. return true if you push first and second key and third and fourth
}                 
}

//4. if all sequence is true do someting...
if(allDown) {
Debug.Log ("Sequence OK!"); // Debug Code
//Next Level...
firstDown = false; // reset variables
secondDown = false;
thirdDown = false;
allDown = false;   
}
// Key sequence input controller END ########################

}

(KeyCode.) Sequence of 4 keys – Basic + CountDown – Separate function()


// Key Sequence Input Controller
// Attach this script to a GameObject in the scene

private var firstDown : boolean = false;  // false at the beginning, I have no typed yet
private var secondDown : boolean = false; // false at the beginning, I have no typed yet
private var thirdDown : boolean = false;  // false at the beginning, I have no typed yet
private var allDown : boolean = false;    // false at the beginning, I have no typed yet

private var endTime : float;

function Start()
{
    endTime = Time.time + 3; // 3 seconds to type the right sequence!
}// End Start()
 
function Update() {
	KeySequenceControl(); // call the function to control the sequence

}// End Update()

// KeySequenceControl() START ##################################################################
function KeySequenceControl(){

    // COUNTDOWN START #########################
    var timeLeft : int = endTime - Time.time; 
    // We do not need negative time
    if (timeLeft < 0){ 
        timeLeft = 0;
    	        firstDown = false;   // reset variables, You Fail!
		secondDown = false;
		thirdDown = false;
		allDown = false;
		// Retry or Abort...
		}
    // COUNTDOWN END ###########################

// Key sequence input controller START ######################
if(Input.GetKeyDown(KeyCode.UpArrow)) {
// highlight the button or play a sound
firstDown = true; //1. return true if you push first key
}
                  //2. if the first key is true 

if(firstDown) {
if(Input.GetKeyDown(KeyCode.RightArrow)) {
// highlight the button or play a sound
secondDown = true;//3. return true if you push first and second key
}                 
}

if(secondDown) {
if(Input.GetKeyDown(KeyCode.DownArrow)) {
// highlight the button or play a sound
thirdDown = true; //4. return true if you push first and second key and third
}                 
}

if(thirdDown) {
if(Input.GetKeyDown(KeyCode.LeftArrow)) {
// highlight the button or play a sound
allDown = true;  //5. return true if you push first and second key and third and fourth
}                 
}

//4. if all sequence is true do someting...
if(allDown) {
Debug.Log ("Sequence OK!"); // Debug Code
//Next Level...
firstDown = false; // reset variables
secondDown = false;
thirdDown = false;
allDown = false;   
}
// Key sequence input controller END ########################
}// KeySequenceControl() END ####################################################################

(Input Manager) Sequence of 4 keys – Basic + CountDown – Separate function()

Control Keys using Unity3D Input Manager, so end users can setup keys:

(Input.GetKeyDown(KeyCode.UpArrow) -> (Input.GetKeyDown(“up”)


// Key Sequence Input Controller
// Attach this script to a GameObject in the scene

private var firstDown : boolean = false;  // false at the beginning, I have no typed yet
private var secondDown : boolean = false; // false at the beginning, I have no typed yet
private var thirdDown : boolean = false;  // false at the beginning, I have no typed yet
private var allDown : boolean = false;    // false at the beginning, I have no typed yet

private var endTime : float;

function Start()
{
    endTime = Time.time + 3; // 3 seconds to type the right sequence!
}// End Start()
 
function Update() {
	KeySequenceControl(); // call the function to control the sequence

}// End Update()

// KeySequenceControl() START ##################################################################
function KeySequenceControl(){

    // COUNTDOWN START #########################
    var timeLeft : int = endTime - Time.time; 
    // We do not need negative time
    if (timeLeft < 0){ 
        timeLeft = 0;
    	        firstDown = false;   // reset variables, You Fail!
		secondDown = false;
		thirdDown = false;
		allDown = false;
		// Retry or Abort...
		}
    // COUNTDOWN END ###########################

// Key sequence input controller START ######################
if(Input.GetKeyDown("up")) {
// highlight the button or play a sound
firstDown = true; //1. return true if you push first key
}
                  //2. if the first key is true 

if(firstDown) {
if(Input.GetKeyDown("right")) {
// highlight the button or play a sound
secondDown = true;//3. return true if you push first and second key
}                 
}

if(secondDown) {
if(Input.GetKeyDown("down")) {
// highlight the button or play a sound
thirdDown = true; //4. return true if you push first and second key and third
}                 
}

if(thirdDown) {
if(Input.GetKeyDown("left")) {
// highlight the button or play a sound
allDown = true;  //5. return true if you push first and second key and third and fourth
}                 
}

//4. if all sequence is true do someting...
if(allDown) {
Debug.Log ("Sequence OK!"); // Debug Code
//Next Level...
firstDown = false; // reset variables
secondDown = false;
thirdDown = false;
allDown = false;   
}
// Key sequence input controller END ########################
}// KeySequenceControl() END ####################################################################


(Input Manager) Sequence of 4 keys – Basic + CountDown – Separate sequence variables


// Key Sequence Input Controller
// Attach this script to a GameObject in the scene

// Right Sequence
var firstKey  : String = "up";
var secondKey : String = "right";
var thirdKey  : String = "down";
var fourthKey : String = "left";

private var firstDown : boolean = false;  // false at the beginning, I have no typed yet
private var secondDown : boolean = false; // false at the beginning, I have no typed yet
private var thirdDown : boolean = false;  // false at the beginning, I have no typed yet
private var allDown : boolean = false;    // false at the beginning, I have no typed yet

private var endTime : float;

function Start()
{
    endTime = Time.time + 3; // 3 seconds to type the right sequence!
}// End Start()
 
function Update() {
	KeySequenceControl(); // call the function to control the sequence

}// End Update()

// KeySequenceControl() START ##################################################################
function KeySequenceControl(){

    // COUNTDOWN START #########################
    var timeLeft : int = endTime - Time.time; 
    // We do not need negative time
    if (timeLeft < 0){ 
        timeLeft = 0;
    	        firstDown = false;   // reset variables, You Fail!
		secondDown = false;
		thirdDown = false;
		allDown = false;
		// Retry or Abort...
		}
    // COUNTDOWN END ###########################

// Key sequence input controller START ######################
if(Input.GetKeyDown(firstKey)) {
// highlight the button or play a sound
firstDown = true; //1. return true if you push first key
}
                  //2. if the first key is true 

if(firstDown) {
if(Input.GetKeyDown(secondKey)) {
// highlight the button or play a sound
secondDown = true;//3. return true if you push first and second key
}                 
}

if(secondDown) {
if(Input.GetKeyDown(thirdKey)) {
// highlight the button or play a sound
thirdDown = true; //4. return true if you push first and second key and third
}                 
}

if(thirdDown) {
if(Input.GetKeyDown(fourthKey)) {
// highlight the button or play a sound
allDown = true;  //5. return true if you push first and second key and third and fourth
}                 
}

//4. if all sequence is true do someting...
if(allDown) {
Debug.Log ("Sequence OK!"); // Debug Code
//Next Level...
firstDown = false; // reset variables
secondDown = false;
thirdDown = false;
allDown = false;   
}
// Key sequence input controller END ########################
}// KeySequenceControl() END ####################################################################

(Input Manager) Sequence of 4 keys – Basic + CountDown – Arrays


// Key Sequence Input Controller
// Attach this script to a GameObject in the scene
     
    // array START #######################
    var i : int = 0; // array index start value
    var sequenceKey = new Array (); 
    // Right Sequence
    sequenceKey[0] = "up";
    sequenceKey[1] = "right";
    sequenceKey[2] = "down";
    sequenceKey[3] = "left";
    // array END #########################

private var firstDown : boolean = false;  // false at the beginning, I have no typed yet
private var secondDown : boolean = false; // false at the beginning, I have no typed yet
private var thirdDown : boolean = false;  // false at the beginning, I have no typed yet
private var allDown : boolean = false;    // false at the beginning, I have no typed yet

private var endTime : float;

function Start()
{
    endTime = Time.time + 3; // 3 seconds to type the right sequence!
}// End Start()
 
function Update() {
	KeySequenceControl(); // call the function to control the sequence

}// End Update()

// KeySequenceControl() START ##################################################################
function KeySequenceControl(){

    // COUNTDOWN START #########################
    var timeLeft : int = endTime - Time.time; 
    // We do not need negative time
    if (timeLeft < 0){ 
        timeLeft = 0;
    	        firstDown = false;   // reset variables, You Fail!
		secondDown = false;
		thirdDown = false;
		allDown = false;
		// Retry or Abort...
		}
    // COUNTDOWN END ###########################

// Key sequence input controller START ######################
if(Input.GetKeyDown(sequenceKey[0])) {
// highlight the button or play a sound
firstDown = true; //1. return true if you push first key
}
                  //2. if the first key is true 

if(firstDown) {
if(Input.GetKeyDown(sequenceKey[1])) {
// highlight the button or play a sound
secondDown = true;//3. return true if you push first and second key
}                 
}

if(secondDown) {
if(Input.GetKeyDown(sequenceKey[2])) {
// highlight the button or play a sound
thirdDown = true; //4. return true if you push first and second key and third
}                 
}

if(thirdDown) {
if(Input.GetKeyDown(sequenceKey[3])) {
// highlight the button or play a sound
allDown = true;  //5. return true if you push first and second key and third and fourth
}                 
}

//4. if all sequence is true do someting...
if(allDown) {
Debug.Log ("Sequence OK!"); // Debug Code
//Next Level...
firstDown = false; // reset variables
secondDown = false;
thirdDown = false;
allDown = false;   
}
// Key sequence input controller END ########################
}// KeySequenceControl() END ####################################################################

By |Unity3D, Video Games Development|Commenti disabilitati su Unity 3D – Key Sequence Input – Basics – JavaScript