Unity—ParticleSystem (particle system) and Animator (animation state machine) batch manager

Unity——ParticleSystem (particle system) and Animator (animation state machine) batch manager


This script can play multiple particles and animations at the same time while Unity is running, making it easier for artists and special effects artists to repeatedly check special effects and animation details.
(Note: This script only performs simple loop playback control and single playback control. If you have other needs, you can expand it by yourself.)

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);

        }
       
    }  

}

The following is a visual button generated under this code module for easy operation.
(Note: If you need to add a button script, the name and method name in the creation code need to be consistent to be correctly generated and called)

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();
        }
    }

}

The completed rendering is as follows
insert image description here

Guess you like

Origin blog.csdn.net/LCF_CSharp/article/details/120155793