在unity官方论坛看到的一个解决方案,可以将Particle直接转换成CanvasRenderer元素显示。
新建一个UIParticleSystem.cs脚本,将以下代码复制进去:

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 }

  脚本依赖ParticleSystem控件,只能挂载在Paricle物体上。新建一个ParticleSystem将脚本拖上去就能用了!

https://blog.csdn.net/dark00800/article/details/73729947

将Particle转成UGUI的更多相关文章

  1. [unity]UGUI界面滑动,ScrollRect嵌套滑动

    原因:老板蛋痛,让我去抄皇室战争. 思路:我大概知道ngui(后来改成UGUI的)里面有个ScrollView.于是我就想一个横着的SV加上5个竖的SV不就好了吗. 过程: 于是 但是有个问题就是UI ...

  2. Unity UGUI 实现简单拖拽功能

    说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui坐标),这两个坐标是不一样的,所以拖拽就需要转换. 因 ...

  3. [Unity UGUI图集系统]浅谈UGUI图集使用

    **写在前面,下面是自己做Demo的时候一些记录吧,参考了很多网上分享的资源 一.打图集 1.准备好素材(建议最好是根据图集名称按文件夹分开) 2.创建一个SpriteAtlas 3.将素材添加到图集 ...

  4. (转)u3d设计模式

    Unity3d中UI开发的MVC模式 ,和游戏开发的其他模块类似,UI一般需要通过多次迭代开发,直到用户体验近似OK.另外至关重要的是, 我们想尽快加速迭代的过程.使用MVC模式来进行设计,已经被业界 ...

  5. 爬虫_python3_requests

    Requests 网络资源(URLs)撷取套件 改善Urllib2的缺点,让使用者以最简单的方式获取网络资源 可以使用REST操作(POST,PUT,GET,DELETE)存取网络资源 import ...

  6. Unity4.6 UGUI 图片打包设置(小图打包成图集 SpritePacker)

    版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ 在学习UGUI的过程中,一直使用小图也就是散图,一个按钮一个图 ...

  7. UGUI加载图片优化方法之一:打包成图集

    打包后的: 直接改变sprite中的packing tag,相同的packing tag就是同一张图集中的.改完运行会自动帮你打包

  8. Unity NGUI和UGUI与模型、特效的层级关系

    目录 1.介绍两大UI插件NGUI和UGUI 2.unity渲染顺序控制方式 3.NGUI的控制 4.UGUI的控制 5.模型深度的控制 6.粒子特效深度控制 7.NGUI与模型和粒子特效穿插层级管理 ...

  9. 在Unity中使用UGUI修改Mesh绘制几何图形

    在商店看到这样一个例子,表示很有兴趣,他们说是用UGUI做的.我想,像这种可以随便变形的图形,我第一个就想到了网格变形. 做法1: 细心的朋友应该会发现,每个UGUI可见元素,都有一个‘Canvas ...

  10. UGUI 学习笔记

    1.UGUI中是没有depth的概念,那要怎么在脚本中动态的改变一个UI元素在hierarchy中的排序位置呢? 放到最上面 Transform.SetAsFirstSibling最下面Transfo ...

随机推荐

  1. 项目PMP之一项目管理介绍

    一.项目定义: 概要:为创造独特的产品.服务或成果而进行的临时性工作 组织创造价值和效益.项目驱动变更创造商业价值的主要方式 特性/要素: 独特的产品.服务或成果,即一个或多个可交付成果(范围.进度( ...

  2. Java底层知识面试题

    JVM内存结构class文件格式JVM不会理解我们写的Java源文件, 我们必须把Java源文件编译成class文件, 才能被JVM识别, 对于JVM而言,class文件相当于一个接口class文件是 ...

  3. Win10部分软件程序中输入中文变成问号??如何处理【详细步骤】

    近期在win10系统中,出现了一个问题,那就是在部分程序软件中输入中文之后,会直接显示问号,哪怕是更换输入法也没有任何用.那么遇到这个问题,我们要如何处理呢?下面IT百科分享一下Win10系统部分软件 ...

  4. h5按需引入Vant

    下载按需引入插件(推荐) babel-plugin-import 是一款 babel 插件, 它会在编译过程中将 import 的写法自动转换为按需引入的方式 # 安装插件 npm i babel-p ...

  5. 【译】融入人工智能的 eShop – 全面的智能应用示例

    原文 | Jeremy Likness 翻译 | 郑子铭 人工智能 (AI) 是一种强大的工具,它可以增强您的应用程序,提供更好的个性化定制体验,满足客户的独特需求,同时提高内部运营的质量和效率.虽然 ...

  6. 使用SOUI播放视频

    播放视频是一个常规需求. 如果将每一个视频帧转换成rgb格式,再使用gdi贴图,效率会很低,只能适合分辨率很低的视频,1080P全屏软渲染一般的电脑都撑不住. 因此渲染视频通常需要启用硬件渲染.开启硬 ...

  7. Django项目与Vue的集成

    Django项目与Vue的集成 在现代Web开发领域,前后端分离已成为一种主流趋势.Django,作为一个强大的Python Web框架,以其丰富的功能和高度的可扩展性而受到开发者的青睐.而Vue.j ...

  8. 割以咏志:Stoer–Wagner 算法求解全局最小割

    全局最小割问题(Global Min-Cut Problem)是图论中的一个经典问题,旨在通过切割图中的边来划分图的顶点集合.具体来说,给定一个加权无向图 $ G = (V, E) $,图中每条边 $ ...

  9. KUKA库卡机器人维修碰撞、电源、网络故障

    在进行库卡机器人的维修作业时,我们通常要遵循一系列经过精心设计和标准化的操作流程与步骤,以确保维修工作的切实有效以及机器人能够在安全的状态下运行.   针对库卡机器人维修中的故障原因分析,可以从以下几 ...

  10. 记一次golang项目context引发的OOM故障

    之前写过一篇一种基于etcd实践节点自动故障转移的思路, 程序经历过一次线上进程内存持续上涨终OOOM的小事故, 本次技术复盘导致本次内存泄露的完整起因. 提炼代码: 业务函数etcdWatchLoo ...