Unity修改粒子特效大小脚本

Unity修改粒子特效大小脚本

挂到特效上即可修改,亲测可用

//This script will only work in editor mode. You cannot adjust the scale dynamically in-game!
using UnityEngine;
using System.Collections;

#if UNITY_EDITOR 
using UnityEditor;
#endif

[ExecuteInEditMode]
public class ParticleScaler : MonoBehaviour 
{
	public float particleScale = 1.0f;
	public bool alsoScaleGameobject = true;

	float prevScale;

	void Start()
	{
		prevScale = particleScale;
	}

	void Update () 
	{
#if UNITY_EDITOR 
		//check if we need to update
		if (prevScale != particleScale && particleScale > 0)
		{
			if (alsoScaleGameobject)
				transform.localScale = new Vector3(particleScale, particleScale, particleScale);

			float scaleFactor = particleScale / prevScale;

			//scale legacy particle systems
			ScaleLegacySystems(scaleFactor);

			//scale shuriken particle systems
			ScaleShurikenSystems(scaleFactor);

			//scale trail renders
			ScaleTrailRenderers(scaleFactor);

			prevScale = particleScale;
		}
#endif
	}

	void ScaleShurikenSystems(float scaleFactor)
	{
#if UNITY_EDITOR 
		//get all shuriken systems we need to do scaling on
		ParticleSystem[] systems = GetComponentsInChildren<ParticleSystem>();

		foreach (ParticleSystem system in systems)
		{
			system.startSpeed *= scaleFactor;
			system.startSize *= scaleFactor;
			system.gravityModifier *= scaleFactor;

			//some variables cannot be accessed through regular script, we will acces them through a serialized object
			SerializedObject so = new SerializedObject(system);
			
			//unity 4.0 and onwards will already do this one for us
#if UNITY_3_5 
			so.FindProperty("ShapeModule.radius").floatValue *= scaleFactor;
			so.FindProperty("ShapeModule.boxX").floatValue *= scaleFactor;
			so.FindProperty("ShapeModule.boxY").floatValue *= scaleFactor;
			so.FindProperty("ShapeModule.boxZ").floatValue *= scaleFactor;
#endif
			
			so.FindProperty("VelocityModule.x.scalar").floatValue *= scaleFactor;
			so.FindProperty("VelocityModule.y.scalar").floatValue *= scaleFactor;
			so.FindProperty("VelocityModule.z.scalar").floatValue *= scaleFactor;
			so.FindProperty("ClampVelocityModule.magnitude.scalar").floatValue *= scaleFactor;
			so.FindProperty("ClampVelocityModule.x.scalar").floatValue *= scaleFactor;
			so.FindProperty("ClampVelocityModule.y.scalar").floatValue *= scaleFactor;
			so.FindProperty("ClampVelocityModule.z.scalar").floatValue *= scaleFactor;
			so.FindProperty("ForceModule.x.scalar").floatValue *= scaleFactor;
			so.FindProperty("ForceModule.y.scalar").floatValue *= scaleFactor;
			so.FindProperty("ForceModule.z.scalar").floatValue *= scaleFactor;
			so.FindProperty("ColorBySpeedModule.range").vector2Value *= scaleFactor;
			so.FindProperty("SizeBySpeedModule.range").vector2Value *= scaleFactor;
			so.FindProperty("RotationBySpeedModule.range").vector2Value *= scaleFactor;

			so.ApplyModifiedProperties();
		}
#endif
	}

	void ScaleLegacySystems(float scaleFactor)
	{
#if UNITY_EDITOR 
		//get all emitters we need to do scaling on
		ParticleEmitter[] emitters = GetComponentsInChildren<ParticleEmitter>();

		//get all animators we need to do scaling on
		ParticleAnimator[] animators = GetComponentsInChildren<ParticleAnimator>();

		//apply scaling to emitters
		foreach (ParticleEmitter emitter in emitters)
		{
			emitter.minSize *= scaleFactor;
			emitter.maxSize *= scaleFactor;
			emitter.worldVelocity *= scaleFactor;
			emitter.localVelocity *= scaleFactor;
			emitter.rndVelocity *= scaleFactor;

			//some variables cannot be accessed through regular script, we will acces them through a serialized object
			SerializedObject so = new SerializedObject(emitter);

			so.FindProperty("m_Ellipsoid").vector3Value *= scaleFactor;
			so.FindProperty("tangentVelocity").vector3Value *= scaleFactor;
			so.ApplyModifiedProperties();
		}

		//apply scaling to animators
		foreach (ParticleAnimator animator in animators)
		{
			animator.force *= scaleFactor;
			animator.rndForce *= scaleFactor;
		}
#endif
	}

	void ScaleTrailRenderers(float scaleFactor)
	{
		//get all animators we need to do scaling on
		TrailRenderer[] trails = GetComponentsInChildren<TrailRenderer>();

		//apply scaling to animators
		foreach (TrailRenderer trail in trails)
		{
			trail.startWidth *= scaleFactor;
			trail.endWidth *= scaleFactor;
		}
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Unity的星星粒子特效可以通过点击事件进行实现。首先,需要在Unity中创建一个粒子系统,并将其粒子形状设置为点。然后,通过调整粒子大小、颜色和发射速度等属性,可以制造出类似于星星的效果。 接下来,需要在Unity中创建一个脚本,用于控制点击事件。在该脚本中,可以使用Raycast射线检测来判断是否点击了特定的粒子。当检测到鼠标点击事件时,可以使用Raycast射线检测到的信息,将点击的粒子大小、颜色或透明度等属性进行修改。 除了使用射线检测外,也可以使用Collider组件来检测点击事件。在每个粒子对象上添加Collider组件,并将其设置为非物理模式。当检测到鼠标点击事件时,可以使用OnMouseDown或OnMouseUp等函数来响应点击事件,并进行相应的处理。 通过在脚本中添加点击事件的处理逻辑,可以实现点击星星粒子特效时的效果,比如改变颜色、放大或缩小等等。可以根据自己的需求来调整点击事件的具体效果。 总之,Unity的星星粒子特效可以通过使用射线检测或Collider组件来实现点击效果。通过响应点击事件,可以对粒子的属性进行修改,从而实现丰富多样的点击效果。 ### 回答2: Unity是一款跨平台的游戏开发引擎,它支持3D和2D游戏开发,并具有强大的建模、动画和渲染功能。在Unity中,可以使用粒子系统来创建各种特效,其中包括星星粒子特效。 点击是指在游戏中鼠标左键或触摸屏幕的操作。在Unity中,可以通过编写脚本来实现点击事件的响应。当玩家点击屏幕上的某个物体时,可以通过脚本来检测鼠标或触摸的位置,并触发相应的行为。 在创建星星粒子特效时,可以使用Unity粒子系统组件。通过调整粒子系统的参数,可以控制粒子的产生、移动、旋转、大小、颜色等属性,从而实现各种效果。例如,可以通过设置粒子的形状为点状,然后将其发射方向调整为向上,使得粒子像星星一样向上飘动。同时,可以调整粒子的颜色、大小等属性,使其更加逼真。 在实现点击事件响应时,可以在脚本中添加一个函数,用来处理点击事件。当玩家点击屏幕时,该函数会被触发,并执行相应的逻辑,比如播放粒子动画、改变粒子特效的属性等。通过编写脚本,我们可以自定义点击事件的逻辑,实现更多个性化的交互效果。 总而言之,Unity中的星星粒子特效点击指的是通过粒子系统和脚本来控制星星粒子特效的产生和交互。通过调整粒子的属性和编写脚本,我们可以创建各种各样的星星粒子特效,并实现点击事件的响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值