unity3d粒子缩放

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即是放大
这里写图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四夕立羽

你的鼓励将是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值