概要:类吸血鬼实战(5):UI创建4(禁用人物)、音效与音乐管理
第十四课:UI创建4(禁用人物)
创建界面
AchiveManager脚本控制
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AchiveManager : MonoBehaviour
{
public GameObject[] lockCharacter;
public GameObject[] unCharacter;
enum Achive
{
UnlockPotato, UnlockBean
}
Achive[] achives;
void Awake()
{
achives = (Achive[])Enum.GetValues(typeof(Achive));
if (!PlayerPrefs.HasKey("Mydata"))
{
Init();
}
}
void Init()
{
PlayerPrefs.SetInt("MyData", 1);
foreach (Achive achive in achives)
{
PlayerPrefs.SetInt(achive.ToString(), 0);
}
}
void Start()
{
UnlockCharacter();
}
void UnlockCharacter()
{
for (int index = 0; index < lockCharacter.Length; index++)
{
string achiveName = achives[index].ToString();
bool isUnlock = PlayerPrefs.GetInt(achiveName) == 1;
lockCharacter[index].SetActive(!isUnlock);
unCharacter[index].SetActive(isUnlock);
}
}
}
通知提示
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class AchiveManager : MonoBehaviour
{
public GameObject uiNotice;
WaitForSecondsRealtime wait;
/*...*/
void CheckAchive(Achive achive)
{
/*...*/
switch (achive)
{
/*...*/
}
if (isAchive && PlayerPrefs.GetInt(achive.ToString()) == 0)
{
PlayerPrefs.SetInt(achive.ToString(), 1);
for (int index = 0; index < uiNotice.transform.childCount; index++)
{
bool isActive = index == (int)achive;
uiNotice.transform.GetChild(index).gameObject.SetActive(isActive);
}
StartCoroutine(NoticeRoutine());
}
}
IEnumerator NoticeRoutine()
{
uiNotice.SetActive(true);
yield return wait;
uiNotice.SetActive(false);
}
}
第十五课:音效
AudioManager音频管理
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
[Header("#BGM")]
public AudioClip bgmClip;
public float bgmVolume;
AudioSource bgmPlayer;
[Header("#SFX")]
public AudioClip[] sfxClips;
public float sfxVolume;
public int channels;
AudioSource[] sfxPlayers;
int channelIndex;
public enum Sfx { Dead, Hit, LevelUp = 3, Lose, Melee, Range = 7, Select, Win }
void Awake()
{
instance = this;
Init();
}
void Init()
{
GameObject bgmObject = new GameObject("BgmPlayer");
bgmObject.transform.parent = transform;
bgmPlayer = bgmObject.AddComponent<AudioSource>();
bgmPlayer.playOnAwake = false;
bgmPlayer.loop = true;
bgmPlayer.volume = bgmVolume;
bgmPlayer.clip = bgmClip;
GameObject sfxObject = new GameObject("sfxPlayer");
sfxObject.transform.parent = transform;
sfxPlayers = new AudioSource[channels];
for (int index = 0; index < sfxPlayers.Length; index++)
{
sfxPlayers[index] = sfxObject.AddComponent<AudioSource>();
sfxPlayers[index].playOnAwake = false;
sfxPlayers[index].volume = sfxVolume;
}
}
public void PlaySfx(Sfx sfx)
{
for (int index = 0; index < sfxPlayers.Length; index++)
{
int loopIndex = (index + channelIndex) % sfxPlayers.Length;
if (sfxPlayers[loopIndex].isPlaying) continue;
int ranIndex = 0;
if (sfx == Sfx.Hit || sfx == Sfx.Melee)
{
ranIndex = Random.Range(0, 2);
}
channelIndex = loopIndex;
sfxPlayers[channelIndex].clip = sfxClips[(int)sfx];
sfxPlayers[channelIndex].Play();
break;
}
}
}
音频唤醒
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
/*...*/
IEnumerator GameOverRoutine()
{
isLive = false;
yield return new WaitForSeconds(0.5f);
uiResult.gameObject.SetActive(true);
uiResult.Lose();
Stop();
AudioManager.instance.PlaySfx(AudioManager.Sfx.Lose);
}
/*...*/
IEnumerator GameVictoryRoutine()
{
isLive = false;
enemyCleaner.SetActive(true);
yield return new WaitForSeconds(0.5f);
uiResult.gameObject.SetActive(true);
uiResult.Win();
Stop();
AudioManager.instance.PlaySfx(AudioManager.Sfx.Win);
}
/*...*/
}
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
public class AchiveManager : MonoBehaviour
{
/*...*/
IEnumerator NoticeRoutine()
{
uiNotice.SetActive(true);
AudioManager.instance.PlaySfx(AudioManager.Sfx.LevelUp);
yield return wait;
uiNotice.SetActive(false);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelUp : MonoBehaviour
{
/*...*/
public void Show()
{
Next();
rect.localScale = Vector3.one;
GameManager.instance.Stop();
AudioManager.instance.PlaySfx(AudioManager.Sfx.LevelUp);
}
public void Hide()
{
rect.localScale = Vector3.zero;
GameManager.instance.Resume();
AudioManager.instance.PlaySfx(AudioManager.Sfx.Select);
}
/*...*/
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
/*...*/
void Fire()
{
/*...*/
bullet.GetComponent<Bullet>().Init(damage, count, dir);
AudioManager.instance.PlaySfx(AudioManager.Sfx.Range);
}
}
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
public class Enemy : MonoBehaviour
{
/*...*/
void OnTriggerEnter2D(Collider2D collider)
{
/*...*/
if (health > 0)
{
animator.SetTrigger("Hit");
AudioManager.instance.PlaySfx(AudioManager.Sfx.Hit);
}
else
{
/*...*/
GameManager.instance.GetExp();
if (GameManager.instance.isLive)
AudioManager.instance.PlaySfx(AudioManager.Sfx.Dead);
}
}
/*...*/
}
背景音乐、升级时暂停、过滤高频
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoBehaviour
{
public static AudioManager instance;
[Header("#BGM")]
/*...*/
AudioHighPassFilter bgmEffect;
/*...*/
void Init()
{
/*...*/
bgmEffect = Camera.main.GetComponent<AudioHighPassFilter>();
/*...*/
}
/*...*/
public void EffectBgm(bool isPlay)
{
bgmEffect.enabled = isPlay;
}
/*...*/
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
/*...*/
IEnumerator GameOverRoutine()
{
/*...*/
AudioManager.instance.PlayBgm(isPlay: false);
AudioManager.instance.PlaySfx(AudioManager.Sfx.Lose);
}
/*...*/
IEnumerator GameVictoryRoutine()
{
/*...*/
AudioManager.instance.PlayBgm(false);
AudioManager.instance.PlaySfx(AudioManager.Sfx.Win);
}
/*...*/
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LevelUp : MonoBehaviour
{
/*...*/
public void Show()
{
Next();
rect.localScale = Vector3.one;
GameManager.instance.Stop();
AudioManager.instance.PlaySfx(AudioManager.Sfx.LevelUp);
AudioManager.instance.EffectBgm(true);
}
public void Hide()
{
rect.localScale = Vector3.zero;
GameManager.instance.Resume();
AudioManager.instance.PlaySfx(AudioManager.Sfx.Select);
AudioManager.instance.EffectBgm(false);
}
/*...*/
}
不熟练
- 碰撞的力的理解,rigbody的理解不足,向量理解
- unity生命周期
- BroadcastMessage方法、ScriptableObject方法、IEnumerator深入了解
- UI布局
- 时间暂停
UI类型
- 物品
- 动画帧
- 人物与怪物
- 人物阴影
- 按钮
- 按钮背景
- 文字
- sliderBar背景颜色
- sliderBar内容颜色
- 物品外框
- Tiles 地板砖背景