【Unity3d】将粒子转换成UGUI粒子

为了UI表现的更加炫酷,我们往往需要给UI层添加粒子表现效果。最简单的方法就是给粒子添加一个UI层的摄像机,像做3D UI一样来实现。但是这样会有很多问题,比如分辨率适配的问题,裁切问题等等。之前在unity官方论坛上看到一个将粒子转换成UGUI的脚本,感觉挺好用的,这里分享一下。

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;

[ExecuteInEditMode]
[RequireComponent(typeof(CanvasRenderer))]
[RequireComponent(typeof(ParticleSystem))]
public class UIParticleSystem : MaskableGraphic
{

    public Texture particleTexture;
    public Sprite particleSprite;

    private Transform _transform;
    private ParticleSystem _particleSystem;
    private ParticleSystem.Particle[] _particles;
    private UIVertex[] _quad = new UIVertex[4];
    private Vector4 _uv = Vector4.zero;
    private ParticleSystem.TextureSheetAnimationModule _textureSheetAnimation;
    private int _textureSheetAnimationFrames;
    private Vector2 _textureSheedAnimationFrameSize;

    public override Texture mainTexture
    {
        get
        {
            if (particleTexture)
            {
                return particleTexture;
            }

            if (particleSprite)
            {
                return particleSprite.texture;
            }

            return null;
        }
    }

    protected bool Initialize()
    {
        // initialize members
        if (_transform == null)
        {
            _transform = transform;
        }

        // prepare particle system
        ParticleSystemRenderer renderer = GetComponent<ParticleSystemRenderer>();
        bool setParticleSystemMaterial = false;

        if (_particleSystem == null)
        {
            _particleSystem = GetComponent<ParticleSystem>();

            if (_particleSystem == null)
            {
                return false;
            }

            // get current particle texture
            if (renderer == null)
            {
                renderer = _particleSystem.gameObject.AddComponent<ParticleSystemRenderer>();
            }
            Material currentMaterial = renderer.sharedMaterial;
            if (currentMaterial && currentMaterial.HasProperty("_MainTex"))
            {
                particleTexture = currentMaterial.mainTexture;
            }

            // automatically set scaling
            _particleSystem.scalingMode = ParticleSystemScalingMode.Local;

            _particles = null;
            setParticleSystemMaterial = true;
        }
        else
        {
            if (Application.isPlaying)
            {
                setParticleSystemMaterial = (renderer.material == null);
            }
#if UNITY_EDITOR
            else
            {
                setParticleSystemMaterial = (renderer.sharedMaterial == null);
            }
#endif
        }

        // automatically set material to UI/Particles/Hidden shader, and get previous texture
        if (setParticleSystemMaterial)
        {
            Material material = new Material(Shader.Find("UI/Particles/Hidden"));
            if (Application.isPlaying)
            {
                renderer.material = material;
            }
#if UNITY_EDITOR
            else
            {
                material.hideFlags = HideFlags.DontSave;
                renderer.sharedMaterial = material;
            }
#endif
        }

        // prepare particles array
        if (_particles == null)
        {
            _particles = new ParticleSystem.Particle[_particleSystem.maxParticles];
        }

        // prepare uvs
        if (particleTexture)
        {
            _uv = new Vector4(0, 0, 1, 1);
        }
        else if (particleSprite)
        {
            _uv = UnityEngine.Sprites.DataUtility.GetOuterUV(particleSprite);
        }

        // prepare texture sheet animation
        _textureSheetAnimation = _particleSystem.textureSheetAnimation;
        _textureSheetAnimationFrames = 0;
        _textureSheedAnimationFrameSize = Vector2.zero;
        if (_textureSheetAnimation.enabled)
        {
            _textureSheetAnimationFrames = _textureSheetAnimation.numTilesX * _textureSheetAnimation.numTilesY;
            _textureSheedAnimationFrameSize = new Vector2(1f / _textureSheetAnimation.numTilesX, 1f / _textureSheetAnimation.numTilesY);
        }

        return true;
    }

    protected override void Awake()
    {
        base.Awake();

        if (!Initialize())
        {
            enabled = false;
        }
    }

    protected override void OnPopulateMesh(VertexHelper vh)
    {
#if UNITY_EDITOR
        if (!Application.isPlaying)
        {
            if (!Initialize())
            {
                return;
            }
        }
#endif

        // prepare vertices
        vh.Clear();

        if (!gameObject.activeInHierarchy)
        {
            return;
        }

        // iterate through current particles
        int count = _particleSystem.GetParticles(_particles);

        for (int i = 0; i < count; ++i)
        {
            ParticleSystem.Particle particle = _particles[i];

            // get particle properties
            Vector2 position = (_particleSystem.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position));
            float rotation = -particle.rotation * Mathf.Deg2Rad;
            float rotation90 = rotation + Mathf.PI / 2;
            Color32 color = particle.GetCurrentColor(_particleSystem);
            float size = particle.GetCurrentSize(_particleSystem) * 0.5f;

            // apply scale
            if (_particleSystem.scalingMode == ParticleSystemScalingMode.Shape)
            {
                position /= canvas.scaleFactor;
            }

            // apply texture sheet animation
            Vector4 particleUV = _uv;
            if (_textureSheetAnimation.enabled)
            {
                float frameProgress = 1 - (particle.lifetime / particle.startLifetime);
                //                float frameProgress = textureSheetAnimation.frameOverTime.curveMin.Evaluate(1 - (particle.lifetime / particle.startLifetime)); // TODO - once Unity allows MinMaxCurve reading
                frameProgress = Mathf.Repeat(frameProgress * _textureSheetAnimation.cycleCount, 1);
                int frame = 0;

                switch (_textureSheetAnimation.animation)
                {

                    case ParticleSystemAnimationType.WholeSheet:
                        frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimationFrames);
                        break;

                    case ParticleSystemAnimationType.SingleRow:
                        frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimation.numTilesX);

                        int row = _textureSheetAnimation.rowIndex;
                        //                    if (textureSheetAnimation.useRandomRow) { // FIXME - is this handled internally by rowIndex?
                        //                        row = Random.Range(0, textureSheetAnimation.numTilesY, using: particle.randomSeed);
                        //                    }
                        frame += row * _textureSheetAnimation.numTilesX;
                        break;

                }

                frame %= _textureSheetAnimationFrames;

                particleUV.x = (frame % _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.x;
                particleUV.y = Mathf.FloorToInt(frame / _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.y;
                particleUV.z = particleUV.x + _textureSheedAnimationFrameSize.x;
                particleUV.w = particleUV.y + _textureSheedAnimationFrameSize.y;
            }

            _quad[0] = UIVertex.simpleVert;
            _quad[0].color = color;
            _quad[0].uv0 = new Vector2(particleUV.x, particleUV.y);

            _quad[1] = UIVertex.simpleVert;
            _quad[1].color = color;
            _quad[1].uv0 = new Vector2(particleUV.x, particleUV.w);

            _quad[2] = UIVertex.simpleVert;
            _quad[2].color = color;
            _quad[2].uv0 = new Vector2(particleUV.z, particleUV.w);

            _quad[3] = UIVertex.simpleVert;
            _quad[3].color = color;
            _quad[3].uv0 = new Vector2(particleUV.z, particleUV.y);

            if (rotation == 0)
            {
                // no rotation
                Vector2 corner1 = new Vector2(position.x - size, position.y - size);
                Vector2 corner2 = new Vector2(position.x + size, position.y + size);

                _quad[0].position = new Vector2(corner1.x, corner1.y);
                _quad[1].position = new Vector2(corner1.x, corner2.y);
                _quad[2].position = new Vector2(corner2.x, corner2.y);
                _quad[3].position = new Vector2(corner2.x, corner1.y);
            }
            else
            {
                // apply rotation
                Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size;
                Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size;

                _quad[0].position = position - right - up;
                _quad[1].position = position - right + up;
                _quad[2].position = position + right + up;
                _quad[3].position = position + right - up;
            }

            vh.AddUIVertexQuad(_quad);
        }
    }

    void Update()
    {
        if (Application.isPlaying)
        {
            // unscaled animation within UI
            _particleSystem.Simulate(Time.unscaledDeltaTime, false, false);

            SetAllDirty();
        }
    }

#if UNITY_EDITOR
    void LateUpdate()
    {
        if (!Application.isPlaying)
        {
            SetAllDirty();
        }
    }
#endif

}

在做UI粒子的时候需要注意调节一下粒子发射的方向和角度,因为这个脚本绘制是截取XY平面所看到的效果,所以比如如果我们做出来的粒子是一个XZ平面的面片的话,转换出来的粒子就看不到了。

By:蒋志杰

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: Unity3D是一个流行的游戏开发引擎,它可以将游戏素材、场景、代码等资源集为一个整体,并发布到多个平台上。UGUIUnity3D的一种图形用户界面系统,它提供了一组可自定义的控件,方便开发者创建交互式的用户界面。 在Unity3D中,开发者可以使用UGUI系统来创建游戏界面,UGUI提供了一系列的控件和布局方式。而对于界面美术设计师来说,他们可能对使用Photoshop(PSD)来进行设计更加熟悉。因此,如果能直接将PSD导出UGUI界面,将会极大的提高开发效率。 现在,Unity3D提供了可以将PSD直接导出UGUI的插件,该插件名为“Unity UI Exporter”。该插件可以直接将PSD中的设计元素,如文本、图像、按钮、滑块、滚动条等,转换UGUI界面控件,并且保留了原有的图层结构和命名规则。开发者只需在Unity3D中打开该插件,并将PSD文件导入即可。该插件可以自动识别PSD文件中的图层并转换对应的UGUI控件,并可以选择导出PNG资源或将PSD文件作为资源文件使用。 总之,使用Unity UI Exporter插件可以方便快捷地将PSD直接导出UGUI界面,帮助开发者提高开发效率,减少重复工作。 ### 回答2: Unity3D是一个功能强大的游戏引擎,它可以将PSD文件直接导出UGUI界面。UGUIUnity3D的用户界面系统,它提供了一系列功能用于创建2D和3D的用户界面。UGUI界面在游戏中非常重要,它为用户提供了快捷的控制方式和更好的交互体验。 在Unity3D中使用PSD文件创建UGUI界面非常方便。首先,我们需要在Unity3D中导入PSD文件。然后,通过双击导入的PSD文件打开它。在打开的界面中选择需要导出的层和UI元素。接下来,在菜单栏中选择“文件”->“导出”,然后选择“UGUI”作为导出格式。 导出后的UGUI界面可以在Unity3D中进行编辑和调整。我们可以将其添加到游戏场景中,并通过代码和脚本进行交互。UGUI界面的创建和编辑非常简单,即使没有编程经验的人也可以轻松上手。 总之,Unity3D将PSD直接导出UGUI界面是一种非常有用的功能。它提供了一种简便的方式来创建游戏界面,并帮助开发者更加高效地完自己的工作。即使没有专业的UI设计人员,开发者在使用Unity3D时也可以轻松创建出美丽的游戏界面。 ### 回答3: Unity3D是一款跨平台的游戏引擎,其支持直接将PSD文件导入到Unity编辑器中,并将其转化为UGUI界面。 在Unity中将PSD文件导入的步骤如下:首先在Unity编辑器中创建新的UI界面,然后将PSD文件拖拽到这个界面中,Unity会自动解析文件并将其转化为UGUI界面。这样可以省去UI制作过程中很多繁琐的操作。同时,Unity还提供了多种美术工具用于增强PSD文件的效果,例如粒子系统、材质编辑器等。 在转化为UGUI界面后,我们可以在编辑器中对其进行进一步的编辑和调整,例如增加按钮、调整字体颜色等。同时,Unity还支持脚本的编写,我们可以在脚本中对UI元素进行操作和控制。 PSD文件直接导出UGUI界面可以大大提高UI制作效率,同时也增加了可编辑性,让我们在编辑和调整UI时更加方便。然而,在使用这个功能时也需要注意遵循PSD文件的设计原则,避免在转化过程中出现不必要的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值