【unity3d study ---- 麦子学院】---------- unity3d常用组件及分析 ---------- 动作event实际应用
在代码中动态添加特效,其实是一个prefab
并且在播放特效的时候播放声音
代码:
1 using UnityEngine; 2 using System.Collections; 3 4 public class AnimatorEventTest : MonoBehaviour { 5 6 Animator animator; // 动作状态机组件 7 AudioSource audioSource; // 声音组件 8 9 // Use this for initialization 10 void Start () { 11 12 animator = GetComponent<Animator> (); 13 audioSource = GetComponent<AudioSource> (); 14 15 } 16 17 // Update is called once per frame 18 void Update () { 19 20 } 21 22 // 通过外部传来的 prefab的名字 来创建特效 23 void PlayEffect(string path){ 24 GameObject go = AssetDatabase.LoadAssestAtPath (string.Format ("Assets/{0}.prefab", path), typeof(GameObject)) as GameObject; 25 GameObject effect = GameObject.Instantiate (go); 26 effect.transform.parent = gameObject.transform; 27 effect.transform.localPosition = Vector3.zero; 28 effect.transform.localRotation = Vector3.zero; 29 } 30 31 void PlaySound(string path){ 32 audioSource.Stop (); 33 AudioClip ac = AssetDatabase.LoadAssestAtPath (string.Format ("Assets/{0}.wav", path), typeof(AudioClip)) as AudioClip; 34 audioSource.clip = ac; 35 audioSource.Play (); 36 } 37 38 39 40 }