zoukankan      html  css  js  c++  java
  • 【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 }
  • 相关阅读:
    回溯算法总结
    第四章总结
    第四章编程总结
    动态规划总结:
    第三章实践心得
    分治算法体会
    第二章上机实践总结
    代码规范与《数学之美》读后感
    第二次c++作业
    第一次博客作业
  • 原文地址:https://www.cnblogs.com/dudu580231/p/5974604.html
Copyright © 2011-2022 走看看