Unity教程之-Unity5中的particle(粒子)缩放

关于Unity 5 中的例子缩放,搜索了半天竟然还有人说:As far as I know that is not possible to do from code. 后来在assetstore找到一款插件Particle Scaler,这货居然还需要10刀, 居然只能在编辑器中使用,在运行中然并卵的节奏还是让人疼,我表示很不满啊!

然后功夫不负有心人!!!找到了解决方案。废话不说,贴上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class ScaleParticles : MonoBehaviour
{
// @zpj default scale size;
public float ScaleSize = 1.0f;
private List< float > initialSizes = new List< float >();
 
void Awake()
{
// Save off all the initial scale values at start.
ParticleSystem[] particles = gameObject.GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem particle in particles)
{
initialSizes.Add(particle.startSize);
ParticleSystemRenderer renderer = particle.GetComponent<ParticleSystemRenderer>();
if (renderer)
{
initialSizes.Add(renderer.lengthScale);
initialSizes.Add(renderer.velocityScale);
}
}
}
 
void Start()
{
gameObject.transform.localScale = new Vector3(ScaleSize, ScaleSize, ScaleSize);
// Scale all the particle components based on parent.
int arrayIndex = 0;
ParticleSystem[] particles = gameObject.GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem particle in particles)
{
particle.startSize = initialSizes[arrayIndex++] * gameObject.transform.localScale.magnitude;
ParticleSystemRenderer renderer = particle.GetComponent<ParticleSystemRenderer>();
if (renderer)
{
renderer.lengthScale = initialSizes[arrayIndex++] *
gameObject.transform.localScale.magnitude;
renderer.velocityScale = initialSizes[arrayIndex++] *
gameObject.transform.localScale.magnitude;
}
}
}
 
}

使用:

建立一个空对象,把上面的名字为ScaleParticles.cs的拖拽到空对象上。把你需要的粒子效果作为一个子对象挂载到空对象上。

如下所示;

修改检视板中的scale size 大小,来修改粒子大小。

是不是很简单实用呢。好了这就是今天的类容,本篇unity3d教程到此结束,下篇我们再会!

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值