Wednesday, May 31, 2017

Roll Ball Unity 5.6 Part 6 - Display Text, Play Fireworks & Sound

Part 5 bisa dilihat dilink ini...

Di part kelima ini kita akan nambain text dilayar yg isinya jumlah cube yg udah dihit dan text buat nandain klu udah menang... :)
So pertama-tama tambain empty_object buat nyimpent guitextnya. Terus ubah nama empty gameobjectnya jadi Display Text kek gini :

Terus buat empty gameobject lagi terus namain jadi hit Text kek gini :
Ters reset posisi Hit Text ke origin point :

Teruuus... tambain GUI Text ke Hit Textnya...
Teruuuss... nambain "Point: " di kolom Text dan ubah posisinya sesuai selera

Terus...... Hit Text-nya dijadiin child dari Display Text (drag and drop kek dipostingan sblmnya)..
Trs Hit Text diduplicate dan namanya diganti jadi Win Text kek gini :
Yang Win Text ini propertinya kyk gini :
Siiipsss... tampilannya udh siap...berikutnya adalaaah... scriptnya... so silahkan buka SphereController ters tambain ginian...
Untuk ngeliad perubahan yg lebih jelas, silahkan buka reponya di github disinih....
Eeeeh... ada yg luphaa..... lupa masukin fireworks pas menang... :v
Soo...tambain lagi kode dibagian showText() ahahaha.... kek gini ajjah...

Github commit...
mmmm....kekna gini ajjah... klu g' ngerti kodenya silahkan ditanyakan...ehehe... :D
Teruuuss... setelah fireworksnya udah ada...berikutnya adalah  suaranya.... So mari kita buat 1 biji folder buat nyimpen suara...terus drag and drop file mp3 ke dalam foldernya kek gini :
Terus tambain 1 biji gameobject audio source dan masukin fireworks2 ke audio source ini
Siiipsss.... berikutnya trigger soundsnya pas udh menang pake script (tutorial buat ngejalanin suara bisa dilihat disini...) . So tambain kode ini...
Perubahan kode digthub bisa diliat disini.... Klu g' ngerti kodenya silahkan ditanyakan... :)
Siiipsss.... Gitu ajjah buat yg part 6.... :)
Demonya seperti ini....

Part 7 bisa dilihat disni.....

Tuesday, May 30, 2017

Finding UI Button from Canvas Gameobject in Unity 5.6

So in this scene, I have canvas with two buttons as shown in the picture below
This restart button has a tag 'restart_tag' and exit with 'exit_tag' button...

And in this scene, I want to reference to my buttons using variabel in my script and this variabel is a private var not a public, so I can't drag and drop button because it's a private. So what we can do for achieving this ?
We have several ways... :)
First... We can use a public function from canvas class namely GetComponentsInChildren<Button>(), this function will gonna return all of the button within this canvas as an array. Here is the example code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class canvas_sc1 : MonoBehaviour {

	private Button[] buttons;
	private Canvas cvs;
	// Use this for initialization
	void Start () {
		cvs = GetComponent<Canvas>();
		buttons = cvs.GetComponentsInChildren<Button> ();
		Debug.Log (buttons[0].tag);
		Debug.Log (buttons[1].tag);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

When we run the game, we'll see that button[0] contains button for restart and another for exit
Another function that we can use is GetComponents. Here is the example :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class canvas_sc1 : MonoBehaviour {

	private Button[] buttons;
	private Button button;
	private Canvas cvs;
	// Use this for initialization
	void Start () {
		cvs = GetComponent<Canvas>();
		buttons = cvs.GetComponents<Button>();
		Debug.Log (buttons);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}
but unfortunately, for unity 5.6, GetComponents doesn't work... :v
So we can just use GetComponentsInChildrend ... :)

Video :