public GameObject PlaySound(string _sound_name, Vector3 _pos, bool _loop, float _volume) { GameObject kGO = AddClone(_sound_name, _pos); if (kGO == null) { return(null); } kGO.transform.parent = gameObject.transform; // AudioListener kGO.transform.position = new Vector3(0, 0, -10); if (_loop) { kGO.GetComponent <AudioSource>().loop = true; } else { kGO.GetComponent <AudioSource>().loop = false; CSndInfo kInfo = (CSndInfo)kGO.GetComponent("CSndInfo"); kInfo.DeathTime = kInfo.audiolength; // 끝나면 삭제. kInfo.bRemove = true; } kGO.GetComponent <AudioSource>().playOnAwake = true; kGO.GetComponent <AudioSource>().volume = _volume; kGO.GetComponent <AudioSource>().Play(); //kGO.audio.PlayOneShot( kGO.audio.clip ); //print("sound: " + _index + " _volume: " + _volume); return(kGO); }
GameObject GetSource(string _sound_name) { foreach (GameObject kGO in SourceArray) { if (kGO == null) { continue; } CSndInfo kObject = (CSndInfo)kGO.GetComponent("CSndInfo"); if (kObject.name == _sound_name) { return(kGO); } } return(null); }
// Add Source ---------------------------------------------------------------- GameObject AddSource(string _sound_name) { GameObject kGO = new GameObject(); //kGO.tag = "SoundObject"; kGO.transform.parent = gameObject.transform; kGO.transform.position = new Vector3(0, 0, 0); kGO.name = _sound_name; string szPrefab = ""; szPrefab = _sound_name; // AudioSource //if( kGO.audio == null) kGO.AddComponent(typeof(AudioSource)); AudioClip audio = (AudioClip)Resources.Load("Sound/" + szPrefab, typeof(AudioClip)); //사운드 폴더 if (audio == null) { Debug.Log("ERROR: CGameSound AddSource Load Failed : " + szPrefab); Destroy(kGO); return(null); } kGO.GetComponent <AudioSource>().clip = audio; kGO.GetComponent <AudioSource>().playOnAwake = false; //kGO.audio.volume = 1.0f; // CSndInfo CSndInfo kInfo = (CSndInfo)kGO.GetComponent("CSndInfo"); if (kInfo == null) { kInfo = (CSndInfo)kGO.AddComponent <CSndInfo>(); } kInfo.iID = kGO.GetInstanceID(); kInfo.audiolength = kGO.GetComponent <AudioSource>().clip.length; kInfo.Index = (int)0; // Add List SourceArray.Add(kGO); return(kGO); }
// Play BGM ---------------------------------------------------------------- public void PlayBGM(string _sound_name) { if (kBgm != null) { CSndInfo kInfo = (CSndInfo)kBgm.GetComponent("CSndInfo"); //print (kInfo._index + " " + _index); if (kInfo.name == _sound_name) { return; } StopBGM(); } kBgm = PlaySound(_sound_name, Vector3.zero, true, fVolume_bgm); kBgm.transform.parent = gameObject.transform; //print("PlayBGM " + _index); }
// Add Fx Clone GameObject AddClone(string _sound_name, Vector3 pos) { GameObject kGO = GetSource(_sound_name); if (kGO == null) { kGO = AddSource(_sound_name); //없으면 추가 } if (kGO) { GameObject kGOClone = (GameObject)Instantiate(kGO); kGOClone.transform.position = pos; CSndInfo kInfo = (CSndInfo)kGOClone.GetComponent("CSndInfo"); kInfo.iID = kGOClone.GetInstanceID(); kInfo.bRemove = false; kInfo.Index = 0; CloneArray.Add(kGOClone); return(kGOClone); } return(null); }
int GetID(GameObject kGO) { CSndInfo kInfo = (CSndInfo)kGO.GetComponent("CSndInfo"); return(kInfo.iID); }