前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >unity3d粒子缩放

unity3d粒子缩放

作者头像
立羽
发布2023-08-24 14:15:56
1840
发布2023-08-24 14:15:56
举报
文章被收录于专栏:Unity3d程序开发
代码语言:javascript
复制
using UnityEngine;
using System.Collections;
using UnityEditor;

public class ParticleScaler : EditorWindow
{


    float scaleMultiplier = 1.0f;
    [MenuItem("ParticleScaler/OpenPanel")]
    static void OpenPanel()
    {
        var win = EditorWindow.GetWindow<ParticleScaler>();
        win.minSize = new Vector2(340, 110);
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("Particle Scale Tool", EditorStyles.boldLabel);

        string selectedParticleString = "Select a particle or weather system";
        if (Selection.gameObjects.Length == 1)
            selectedParticleString = "Selected Particle : " + Selection.gameObjects[0].name;
        else if (Selection.gameObjects.Length > 1)
            selectedParticleString = "Selected Particle : " + Selection.gameObjects.Length + " particles selected";
        EditorGUILayout.LabelField(selectedParticleString, EditorStyles.miniLabel);
        EditorGUILayout.Space();
        scaleMultiplier = EditorGUILayout.Slider("Scale Multiplier :", scaleMultiplier, 0.1f, 5.0f);

        if (GUILayout.Button("Scale", EditorStyles.miniButton))
        {
            foreach (GameObject gameObj in Selection.gameObjects)
            {
                ParticleSystem[] selectedParticles;
                selectedParticles = gameObj.GetComponentsInChildren<ParticleSystem > ();
                foreach (ParticleSystem thisParticle in selectedParticles)
                {
                    thisParticle.Stop();
                    scaleParticle(gameObj, thisParticle);
                    thisParticle.Play();
                }
            }
        }

        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("Play", EditorStyles.miniButtonLeft))
        {
            playParticles();
        }
        if (GUILayout.Button("Pause", EditorStyles.miniButtonMid))
        {
            pauseParticles();
        }
        if (GUILayout.Button("Stop", EditorStyles.miniButtonMid))
        {
            stopParticles();
        }
        if (GUILayout.Button("Clear", EditorStyles.miniButtonRight))
        {
            clearParticles();
        }
        EditorGUILayout.EndHorizontal();
    }

    void playParticles()
    {
        foreach (GameObject gameObj in Selection.gameObjects)
        {
            ParticleSystem[] selectedParticles;
            selectedParticles = gameObj.GetComponentsInChildren< ParticleSystem > ();
            foreach (ParticleSystem thisParticle in selectedParticles)
            {
                thisParticle.Play();
            }
        }
    }

    void pauseParticles()
    {
        foreach (GameObject gameObj in Selection.gameObjects)
        {
            ParticleSystem[] selectedParticles;
            selectedParticles = gameObj.GetComponentsInChildren< ParticleSystem >();
            foreach (ParticleSystem thisParticle in selectedParticles)
            {
                thisParticle.Pause();
            }
        }
    }

    void stopParticles()
    {
        foreach (GameObject gameObj in Selection.gameObjects)
        {
            ParticleSystem[] selectedParticles;
            selectedParticles = gameObj.GetComponentsInChildren< ParticleSystem >();
            foreach (ParticleSystem thisParticle in selectedParticles)
            {
                thisParticle.Pause();
                thisParticle.Stop();
            }
        }
    }

    void clearParticles()
    {
        foreach (GameObject gameObj in Selection.gameObjects)
        {
            ParticleSystem[] selectedParticles;
            selectedParticles = gameObj.GetComponentsInChildren< ParticleSystem >();
            foreach (ParticleSystem thisParticle in selectedParticles)
            {
                thisParticle.Stop();
                thisParticle.Clear();
            }
        }
    }

    void scaleParticle(GameObject _gameObj, ParticleSystem _thisParticle)
    {
        if (_gameObj != _thisParticle.gameObject)
        {
            _thisParticle.transform.localPosition *= scaleMultiplier;
        }

        _thisParticle.startSize *= scaleMultiplier;
        _thisParticle.gravityModifier *= scaleMultiplier;
        _thisParticle.startSpeed *= scaleMultiplier;

        SerializedObject serObj = new SerializedObject(_thisParticle);
        serObj.FindProperty("ShapeModule.boxX").floatValue *= scaleMultiplier;
        serObj.FindProperty("ShapeModule.boxY").floatValue *= scaleMultiplier;
        serObj.FindProperty("ShapeModule.boxZ").floatValue *= scaleMultiplier;
        serObj.FindProperty("ShapeModule.radius").floatValue *= scaleMultiplier;

        scaleACV(serObj.FindProperty("VelocityModule.x.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("VelocityModule.x.maxCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("VelocityModule.y.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("VelocityModule.y.maxCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("VelocityModule.z.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("VelocityModule.z.maxCurve").animationCurveValue);

        serObj.FindProperty("ClampVelocityModule.x.scalar").floatValue *= scaleMultiplier;
        serObj.FindProperty("ClampVelocityModule.y.scalar").floatValue *= scaleMultiplier;
        serObj.FindProperty("ClampVelocityModule.z.scalar").floatValue *= scaleMultiplier;
        serObj.FindProperty("ClampVelocityModule.magnitude.scalar").floatValue *= scaleMultiplier;

        scaleACV(serObj.FindProperty("ClampVelocityModule.x.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ClampVelocityModule.x.maxCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ClampVelocityModule.y.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ClampVelocityModule.y.maxCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ClampVelocityModule.z.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ClampVelocityModule.z.maxCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ClampVelocityModule.magnitude.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ClampVelocityModule.magnitude.maxCurve").animationCurveValue);

        scaleACV(serObj.FindProperty("ForceModule.x.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ForceModule.x.maxCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ForceModule.y.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ForceModule.y.maxCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ForceModule.z.minCurve").animationCurveValue);
        scaleACV(serObj.FindProperty("ForceModule.z.maxCurve").animationCurveValue);

        serObj.FindProperty("VelocityModule.x.scalar").floatValue *= scaleMultiplier;
        serObj.FindProperty("VelocityModule.y.scalar").floatValue *= scaleMultiplier;
        serObj.FindProperty("VelocityModule.z.scalar").floatValue *= scaleMultiplier;

        serObj.FindProperty("ForceModule.x.scalar").floatValue *= scaleMultiplier;
        serObj.FindProperty("ForceModule.y.scalar").floatValue *= scaleMultiplier;
        serObj.FindProperty("ForceModule.z.scalar").floatValue *= scaleMultiplier;

        serObj.ApplyModifiedProperties();
    }

    void scaleACV(AnimationCurve curve)
    {
        for (int i = 0; i < curve.keys.Length; i++)
        {
            curve.keys[i].value *= scaleMultiplier;
        }
    }
}

面板中Scale Multiplier填入倍数,例如0.5即是缩小一倍,大于1即是放大

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-04-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档