不需要在玩家身上添加AudioSource组件
资源管理类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//资源管理类
public class GameRes {
//标签
public const string player = "Player";
public const string dog = "Dog";
public const string nurse = "Nurse";
//名称
public const string canvas = "Canvas";
//声音路径
public const string ak_firePath = "Sounds/AK_fire";
public const string landDefaultPath = "Sounds/landDefault";
public const string ak_readyPath = "Sounds/AK_ready";
public const string shellFall1Path = "Sounds/ShellFall1";
public const string dgDiePath = "Sounds/DgDie";
public const string zombieattackPath = "Sounds/zombieattack";
public const string zombieDiePath = "Sounds/zombieDie";
public const string PropsPath = "Sounds/Se_UI_Props";
public const string NailPath = "Sounds/Se_UI_Nail";
public const string alarmPath = "Sounds/alarm_triggered";
//预设体路径
public const string holePath = "Prefabs/hole";
public const string player1Path = "Prefabs/Player1";
public const string player2Path = "Prefabs/Player2";
public const string nursePath = "Prefabs/Nurse";
public const string dogPath = "Prefabs/Dog";
//特效路径
public const string effectPath = "Effects/RIFLE1";
public const string explosoinPath = "Effects/WFX_Explosion";
//枚举
public enum PlayerState
{
Idle,
Fire,
Reload,//换子弹
Melee //换枪
}
public enum EnemyState
{
Idle,
Run,
Attack,
Death
}
}
玩家类
public class Player : MonoBehaviour
{
AudioSource audios;//1.资源组件
AudioClip fireclip;//2.开枪声音
void Start()
{
audios = GetComponent<AudioSource>();//3获取组件
fireclip = Resources.Load<AudioClip>(GameRes.ak_firePath);//4.获取声音
}
void Start()
{
Fire();
}
void Fire()
{
AudioSource.PlayClipAtPoint(fireclip, pos.position);//5.播放声音
}
}