将Particle转成UGUI
在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的更多相关文章
- [unity]UGUI界面滑动,ScrollRect嵌套滑动
原因:老板蛋痛,让我去抄皇室战争. 思路:我大概知道ngui(后来改成UGUI的)里面有个ScrollView.于是我就想一个横着的SV加上5个竖的SV不就好了吗. 过程: 于是 但是有个问题就是UI ...
- Unity UGUI 实现简单拖拽功能
说到拖拽,那必然离不开坐标,UGUI 的坐标有点不一样,它有两种坐标,一种是屏幕坐标,还有一种就是 UI 在Canvas内的坐标(暂时叫做ugui坐标),这两个坐标是不一样的,所以拖拽就需要转换. 因 ...
- [Unity UGUI图集系统]浅谈UGUI图集使用
**写在前面,下面是自己做Demo的时候一些记录吧,参考了很多网上分享的资源 一.打图集 1.准备好素材(建议最好是根据图集名称按文件夹分开) 2.创建一个SpriteAtlas 3.将素材添加到图集 ...
- (转)u3d设计模式
Unity3d中UI开发的MVC模式 ,和游戏开发的其他模块类似,UI一般需要通过多次迭代开发,直到用户体验近似OK.另外至关重要的是, 我们想尽快加速迭代的过程.使用MVC模式来进行设计,已经被业界 ...
- 爬虫_python3_requests
Requests 网络资源(URLs)撷取套件 改善Urllib2的缺点,让使用者以最简单的方式获取网络资源 可以使用REST操作(POST,PUT,GET,DELETE)存取网络资源 import ...
- Unity4.6 UGUI 图片打包设置(小图打包成图集 SpritePacker)
版权声明:本文转自http://blog.csdn.net/huutu 转载请带上 http://www.liveslives.com/ 在学习UGUI的过程中,一直使用小图也就是散图,一个按钮一个图 ...
- UGUI加载图片优化方法之一:打包成图集
打包后的: 直接改变sprite中的packing tag,相同的packing tag就是同一张图集中的.改完运行会自动帮你打包
- Unity NGUI和UGUI与模型、特效的层级关系
目录 1.介绍两大UI插件NGUI和UGUI 2.unity渲染顺序控制方式 3.NGUI的控制 4.UGUI的控制 5.模型深度的控制 6.粒子特效深度控制 7.NGUI与模型和粒子特效穿插层级管理 ...
- 在Unity中使用UGUI修改Mesh绘制几何图形
在商店看到这样一个例子,表示很有兴趣,他们说是用UGUI做的.我想,像这种可以随便变形的图形,我第一个就想到了网格变形. 做法1: 细心的朋友应该会发现,每个UGUI可见元素,都有一个‘Canvas ...
- UGUI 学习笔记
1.UGUI中是没有depth的概念,那要怎么在脚本中动态的改变一个UI元素在hierarchy中的排序位置呢? 放到最上面 Transform.SetAsFirstSibling最下面Transfo ...
随机推荐
- Java中的流操作
1. 字符流 1.1字符输入流 - Reader - FileReader 涉及到连接的,用完了就要关闭. **为什么read方法 返回的值是 int,而不是char?因为读到结尾的时候,cha ...
- 常见的HTML特殊字符:对钩与叉号,五角星
表示"对钩","叉号"的特殊转义符号汇总: 对钩符号 编码 描述 叉号符号 编码 描述 ✓ ✓ CHECK MARK 手写体对钩(细) ✗ ✗ BALLOT X ...
- 文章学习|开放,让5G网络更智能
学习文章:开放,让5G网络更智能 介绍 从2G到5G,网络在不断发展,通信行业的生态系统在不断演进,运营商的角色也发生着改变. 在2G和3G时代,运营商作为服务提供商为用户提供通信业务和互联网业务,获 ...
- 在日常工作和生活中使用Linux-开篇
前言 欢迎来到<在日常工作和生活中使用Linux>的系列分享.在这个系列中,我们将探讨为什么选择Linux,以及如何在日常工作和生活中高效地使用它.无论你是刚刚接触Linux的新手,还是希 ...
- 阻止(禁止)textare回车换行
<van-field v-model="form.messageCont" rows="3" label="口号" autosize ...
- FreeSql学习笔记——0.FreeSql启动!
FreeSql FreeSql是功能强大的 .NET ORM,支持 .NetFramework 4.0+..NetCore 2.1+.Xamarin等支持 NetStandard 所有运行平台.支持 ...
- 个人文件转移工具-来自某位大神的C盘清理神器
软件名称:个人文件转移工具 软件功能:文件转移 支持平台:Windows 软件简介:一款文件转移工具,也可用作C盘瘦身. 软件特点: ◉ "个人文件转移工具"可以把"我的 ...
- 大数据之路Week08_day06 (Zookeeper搭建)
Zookeeper集群搭建 在本文中Zookeeper节点个数(奇数)为3个.Zookeeper默认对外提供服务的端口号2181 .Zookeeper集群内部3个节点之间通信默认使用2888:3888 ...
- 2024.11.12随笔&联考总结
前言 心情不好,因为考试时 T2T3 全看错题了,导致 T2 没做出来,T3 一份没得.然后下午打球眼镜架子坏了,回机房才发现被高二的盒了. 但还是稍微写一下总结吧. 总结 感觉我今天做题状态还行,思 ...
- 数据挖掘 | 数据隐私(2) | 差分隐私 | 数据重构化攻击(Reconstruction Attacks)
L2-Reconstruction Attacks 本节课的目的在于正式地讨论隐私,但是我们不讨论算法本身有多隐私,取而代之去讨论一个算法隐私性有多么的不可靠.并且聚焦于 Dinur 与 Nissim ...