Unity—ParticleSystem(粒子系统)与Animator(动画状态机)批量管理器

Unity——ParticleSystem(粒子系统)与Animator(动画状态机)批量管理器


该脚本可以在Unity运行时同时播放多个粒子与动画,方便美工、特效师反复查看特效和动画细节。
(注:该脚本只做了简单的循环播放控制与单次播放控制,如有其他需求可自行扩展。)

using UnityEngine;
using System.Collections;
using UnityEditor;

public class ParticleAndAnimation : MonoBehaviour
{
   
    void Start() {
        
        PlayOnce();
    }
    [ContextMenu("Play Loop")]
    public void PlayLoop() {
        ParticleSystem[] pss = this.GetComponentsInChildren<ParticleSystem> (true);
        foreach (ParticleSystem ps in pss) {
            ps.loop = true;
            ps.Play();

        }
        print("粒子系统开启循环成功");
        Animator[] anis = this.GetComponentsInChildren<Animator>(true);
        foreach (Animator an in anis)
        {

            //string animString = an.GetCurrentAnimatorClipInfo(0)[0].clip.name;
            string animString = an.runtimeAnimatorController.animationClips[0].name;
            AnimationClip clip = an.runtimeAnimatorController.animationClips[0];// an.GetCurrentAnimatorClipInfo(0)[0].clip;
            AnimationClipSettings clipsetting = AnimationUtility.GetAnimationClipSettings(clip);
            clipsetting.loopTime = true;
            AnimationUtility.SetAnimationClipSettings(clip, clipsetting);
            an.Play(animString, 0,0f);
           

        }
     
        print("动画系统开启循环成功");
    }
    [ContextMenu("Play Once")]
    public void PlayOnce() {
        ParticleSystem[] pss = this.GetComponentsInChildren< ParticleSystem> (true);

        foreach (ParticleSystem ps in pss)
        {
            ps.loop = false;
            ps.Play();


        }
        Animator[] anis = this.GetComponentsInChildren<Animator>(true);
        foreach (Animator an in anis)
        {
           
          
           
           // string animString = an.GetCurrentAnimatorClipInfo(0)[0].clip.name;
            string animString = an.runtimeAnimatorController.animationClips[0].name;
            AnimationClip clip = an.runtimeAnimatorController.animationClips[0];//an.GetCurrentAnimatorClipInfo(0)[0].clip;
            AnimationClipSettings clipsetting = AnimationUtility.GetAnimationClipSettings(clip);
            clipsetting.loopTime = false;
            AnimationUtility.SetAnimationClipSettings(clip, clipsetting);
            an.Play(animString, 0, 0f);

        }
       
    }  

}

下面是在该代码模块下生成可视化按钮方便操作。
(注意:需要添加按钮的脚本,在该创建代码中的名称、方法名都需要确保一致才能正确生成与调用)

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(ParticleAndAnimation))]//需要添加按钮的脚本
public class AnimatorPlayEditor : Editor { 
    public override void OnInspectorGUI(){
    DrawDefaultInspector();

        ParticleAndAnimation myScript = (ParticleAndAnimation)target;//获取要添加按钮的脚本
        if (GUILayout.Button("播放一次"))
        {

            myScript.PlayOnce();//调用添加按钮脚本中的方法
        }
        if (GUILayout.Button("循环播放"))
        {
            myScript.PlayLoop();
        }
    }

}

完成效果图如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/LCF_CSharp/article/details/120155793