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即是放大

unity3d粒子缩放_粒子缩放