Ses açıp kapama
Bu dersimize sizlere Unity3D Engine oyun moturuyla bir sahnedeki ses kapatıp açma olayınadan bahsedeceğiz , sadece bununla kalmayıp butonlarımız resimlerini sesin açık veya kapalı olmasına göre değiştireceğiz.
Kodlamammıza geçmeden önce bu admları gerçekleştirelim
Hierarchy Panelimize sağ tıklayıp Create Empty diyip boş bir GameObject oluşturuyoruz ve GameObjectimize tıklıyoruz.
Tıklama işlemini gerçekleştirdikten sonra Inspector Paneline gelip Add Component diyip bir Audio Source oluşturalim akabinde AudioClip sekmesine projemize eklemiş olduğumuz ses dosyasını cağırıyoruz ses dosyamız uzantısı mp3 olması tercih edilebilir bir yoldur.
Şimdi kodlarımıza geçmenin vakti geldi.
Algoritması
- Image Componentini cağırmak lazım resimleri kullanmak için.
- AudioSource Componentini çağırmak lazım ses dosyası için.
- iki adet Sprite almak için Sprite Componentini çağrıyoruz.
- Sesin kontrol etmek için boolean değişkeni atadık.
- SesControl adında bir fonksiyon oluşturup buttondan cağırıyoruz.
- AudioSource'un mute özelliğini True False yaparak sesi kontrol ediyoruz.
------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Sound : MonoBehaviour {
Image _image;
public AudioSource audio;
[Header("ResimAcKapa")]
public Sprite OnSprite;
public Sprite OffSprite;
bool ses = true;
void Start() {
_image = GetComponent<Image>();
audio = GetComponent<AudioSource>();
}
void Update() {
if (ses == false) {
audio.mute = true;
}
if(ses == true){
audio.mute = false;
}
}
public void SesControl(){
if (audio.mute) {
ses = true;
_image.sprite = OnSprite;
}
else {
_image.sprite = OffSprite;
ses = false;
}
}
}
using System.Collections;
using UnityEngine.UI;
public class Sound : MonoBehaviour {
Image _image;
public AudioSource audio;
[Header("ResimAcKapa")]
public Sprite OnSprite;
public Sprite OffSprite;
bool ses = true;
void Start() {
_image = GetComponent<Image>();
audio = GetComponent<AudioSource>();
}
void Update() {
if (ses == false) {
audio.mute = true;
}
if(ses == true){
audio.mute = false;
}
}
public void SesControl(){
if (audio.mute) {
ses = true;
_image.sprite = OnSprite;
}
else {
_image.sprite = OffSprite;
ses = false;
}
}
}
Gani BASABAK











