Skip to content

Instantly share code, notes, and snippets.

@Nyoho
Forked from mahm/AudioManager.cs
Created February 16, 2014 05:34
Show Gist options
  • Save Nyoho/9029756 to your computer and use it in GitHub Desktop.
Save Nyoho/9029756 to your computer and use it in GitHub Desktop.
Unity de sashimi
using UnityEngine;
using System.Collections;
public class AudioManager : SingletonMonoBehaviour<AudioManager> {
AudioClip bgm_Game;
AudioClip se_Tanpopo;
private const int source_bgm_Game = 0;
private const int maxAudio = 10;
private int audioCount = 2;
AudioSource[] audioSource = new AudioSource[10];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void Awake() {
bgm_Game = Resources.Load ("Audio/game", typeof(AudioClip)) as AudioClip;
se_Tanpopo = Resources.Load ("Audio/tanpopo", typeof(AudioClip)) as AudioClip;
for (int i = 0; i < maxAudio; i++) {
audioSource[i] = gameObject.AddComponent<AudioSource>();
audioSource[i].dopplerLevel = 0;
}
GameObject.DontDestroyOnLoad(this.gameObject);
}
public void PlayBgm() {
if (!audioSource[source_bgm_Game].isPlaying) {
audioSource[source_bgm_Game].volume = 1.0f;
audioSource[source_bgm_Game].clip = bgm_Game;
audioSource[source_bgm_Game].loop = true;
audioSource[source_bgm_Game].Play();
}
}
public void PauseBgm() {
if (audioSource[source_bgm_Game].isPlaying) {
audioSource[source_bgm_Game].Pause();
}
}
public void StopBgm() {
if (audioSource[source_bgm_Game].isPlaying) {
audioSource[source_bgm_Game].Stop();
}
}
public void PlayTanpopo() {
PlaySound(se_Tanpopo);
}
private void PlaySound(AudioClip audioClip) {
audioSource[audioCount].volume = 1.0f;
audioSource[audioCount].clip = audioClip;
audioSource[audioCount].Play();
addAudioCount();
}
private void addAudioCount() {
audioCount++;
if (audioCount >= maxAudio) {
audioCount = 2;
}
}
}
using UnityEngine;
using System.Collections;
public class Circle : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// 当たり判定のある間実行される
void OnTriggerStay2D (Collider2D coll) {
if (coll.gameObject.tag == "Sashimi") {
Sashimi sashimi = coll.gameObject.GetComponent<Sashimi>();
if (Input.GetMouseButtonDown(0)) {
sashimi.TanpopoGot();
}
}
}
}
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public AudioManager sm;
private GameObject pauseText;
private bool pause = false;
// Use this for initialization
void Start () {
// BGMの再生
GameObject go = GameObject.FindGameObjectWithTag("AudioManager");
if (go == null) {
go = GameObject.Instantiate(Resources.Load("Prefab/AudioManager", typeof(GameObject))) as GameObject;
}
sm = go.GetComponent<AudioManager> ();
sm.PlayBgm ();
// ゲームオブジェクトの生成
GameObject.Instantiate(Resources.Load("Prefab/ScorePrefab", typeof(GameObject)));
GameObject.Instantiate(Resources.Load("Prefab/CirclePrefab", typeof(GameObject)));
GameObject.Instantiate(Resources.Load("Prefab/ShooterPrefab", typeof(GameObject)));
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape)) {
if (this.pause) {
if (this.pauseText) {
Destroy(this.pauseText);
this.pauseText = null;
}
Time.timeScale = 1;
this.pause = false;
sm.PlayBgm ();
} else {
this.pauseText = GameObject.Instantiate(Resources.Load("Prefab/PauseTextPrefab", typeof(GameObject))) as GameObject;
Time.timeScale = 0;
this.pause = true;
sm.PauseBgm ();
}
}
}
}
using UnityEngine;
using System.Collections;
public class Sashimi : MonoBehaviour {
public float velocity;
public bool isDone = false; // ヒット判定用
private SpriteRenderer myRenderer;
private Score score;
private GameController gameController;
public Sprite tanpopoSprite;
// Use this for initialization
void Start () {
GameObject sgo = GameObject.FindGameObjectWithTag("Score");
this.score = sgo.GetComponent<Score>();
GameObject ggo = GameObject.FindGameObjectWithTag("GameController");
this.gameController = ggo.GetComponent<GameController>();
this.rigidbody2D.AddForce(-Vector2.right * velocity);
this.myRenderer = gameObject.GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update () {
}
void OnBecameInvisible() {
if (!this.isDone) this.score.RemoveScore();
Destroy(this.gameObject);
}
public void TanpopoGot() {
if (!this.isDone) {
Debug.Log("[Sashimi] たんぽぽが添えられた");
this.gameController.sm.PlayTanpopo();
this.score.AddScore();
this.isDone = true;
this.myRenderer.sprite = tanpopoSprite;
} else {
Debug.Log("[Sashimi] すでにたんぽぽが添えられている");
}
}
}
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public GameObject sashimiPrefab;
private float timer = 0.0f;
private float interval = 1.0f;
private float intervalTimer = 0.0f;
void Start () {
timer = 0.0f;
intervalTimer = 0.0f;
}
// Update is called once per frame
void Update () {
createSashimiPrefab();
}
void createSashimiPrefab() {
timer -= Time.deltaTime;
if (timer < interval) {
Instantiate(this.sashimiPrefab);
timer = 0.0f;
interval = Random.Range(-1, -4);
}
intervalTimer += Time.deltaTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment