zoukankan      html  css  js  c++  java
  • Unity5中的粒子缩放(附测试源码)

    本文章由cartzhang编写,转载请注明出处。 所有权利保留。 

    文章链接:http://blog.csdn.net/cartzhang/article/details/49363241

    作者:cartzhang



    开始:

    关于Unity 5 中的例子缩放,搜索了半天竟然还有人说:

    As far as I know that is not possible to do from code. 


    然后到官方找来了插件

    https://www.assetstore.unity3d.com/cn/#!/content/4400

    Particle Scaler

    这货居然还需要10刀。我表示很不满啊!


    BTW: 官方下载10美刀的居然只能在编辑器中使用,在运行中然并卵的节奏还是让人疼啊!

    ---------

    方法:

    然后功夫不负有心人!!!找到了解决方案。

    之前代码上有个public void UpdateScale() 

    我不了解是不是之前的版本的函数。反正是现在没戏了。


    做了简单修改,然后就大功告成了。

    还是代码啊!

    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 大小,来修改粒子大小。


    是不是很简单实用呢。


    源码呢:

    免分下载地址如下:

    源码地址:http://download.csdn.net/detail/cartzhang/9207203


    -------------------------------------



    就是这样。

    若有问题,请随时联系!
    非常感谢!!!




  • 相关阅读:
    1029: [JSOI2007]建筑抢修
    1028: [JSOI2007]麻将
    1050 棋盘染色 2
    1026: [SCOI2009]windy数
    1074: [SCOI2007]折纸origami
    839. Optimal Marks
    1024: [SCOI2009]生日快乐
    1025: [SCOI2009]游戏
    1023: [SHOI2008]cactus仙人掌图
    对前面的总结
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461942.html
Copyright © 2011-2022 走看看