将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 ...
随机推荐
- C 将十进制数转换成二~十六进制数中的任意一种
问题:将一个十进制整数转换成二~十六进制数中的任意一种进制数 代码: #include <stdio.h> #include <stdlib.h> int b; int i = ...
- 还堵在高速路上吗?带你进入Scratch世界带你飞
国庆假期高速路的风景 国庆假期正式启动人从众模式,无论是高速公路还是景区,不管是去程还是回程,每一次都堪称经典. 一些网友在经历漫长的拥堵后 哭笑不得地表示 "假期都在堵车中度过了" ...
- JavaDoc文档的介绍及生成方法
javaDoc命令是用来生成自己的API文档的 参数信息 @author 作者名 @version 版本号 @since 指明需要最早使用的jdk版本 @param 参数名 @return 返回值情况 ...
- 百万架构师第四十一课:RabbitMq:可靠性投递和实践经验|JavaGuide
来源:https://javaguide.net RabbitMQ 2-可靠性投递与生产实践 可靠性投递 首先需要明确,效率与可靠性是无法兼得的,如果要保证每一个环节都成功,势必会对消息的收发效率 ...
- ollama-deepseek 部署
选择云资源 选用智星云 4090 高性能 1.57 一小时 windows操作系统 可以修改带宽来增加下载速度 使用mstsc远程登录 使用ollama https://ollama.com/ oll ...
- 企业付款到零钱(微信小程序提现,用户提现到零钱)
pom 增加 <dependency> <groupId>com.github.binarywang</groupId> <artifactId>wei ...
- Linux - 搭建一套Apache大数据集群
一.服务器操作系统 主机名 操作系统 node01 Centos 7.9 node02 Centos 7.9 node03 Centot 7.9 二.大数据服务版本 服务 版本 下载 JDK jdk- ...
- Arduino LED流水灯·基础实验
Arduino初学IO控制,流水灯实验是很好的学习对象.分两个进程学习. 一.假流水灯,即基础效果实现 二.真流水灯,即采用PWM模拟真实流水渐变效果 我们设立5盏灯,正极分别连接数字口(Digita ...
- luogu-P3262题解
简要题意 有一棵不超过十层的满二叉树,需要对每个节点进行染色.每个叶子节点会对其颜色相同的祖先节点产生贡献且黑白贡献不同.求最大贡献. 题解 首先我会暴力!我如果直接暴力枚举每个节点的颜色,复杂度就是 ...
- 面试题30. 包含min函数的栈
地址:https://leetcode-cn.com/problems/bao-han-minhan-shu-de-zhan-lcof/ <?php /** 定义栈的数据结构,请在该类型中实现一 ...