ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-09-07 14:32:24  阅读:212  来源: 互联网

标签:粒子系统 ps 管理器 clip 状态机 animString Animator clipsetting ParticleSystem


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

}

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

标签:粒子系统,ps,管理器,clip,状态机,animString,Animator,clipsetting,ParticleSystem
来源: https://blog.csdn.net/LCF_CSharp/article/details/120155793

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有