Merged in whatwapp/unity-ui-extensions (pull request #10)

Unity API auto-update in UIParticleSystem

Merging as it seems a good way to stop the ever evolving errors.  Will improve the script at a later date.
release
Mauro Ronchi 2017-06-30 16:12:14 +00:00 committed by Simon Jackson
commit d0836e3130
1 changed files with 297 additions and 292 deletions

View File

@ -1,293 +1,298 @@
/// Credit glennpow /// Credit glennpow
/// Sourced from - http://forum.unity3d.com/threads/free-script-particle-systems-in-ui-screen-space-overlay.406862/ /// Sourced from - http://forum.unity3d.com/threads/free-script-particle-systems-in-ui-screen-space-overlay.406862/
/// *Note - experimental. Currently renders in scene view and not game view. /// *Note - experimental. Currently renders in scene view and not game view.
namespace UnityEngine.UI.Extensions namespace UnityEngine.UI.Extensions
{ {
#if UNITY_5_3_OR_NEWER #if UNITY_5_3_OR_NEWER
[ExecuteInEditMode] [ExecuteInEditMode]
[RequireComponent(typeof(CanvasRenderer))] [RequireComponent(typeof(CanvasRenderer))]
[RequireComponent(typeof(ParticleSystem))] [RequireComponent(typeof(ParticleSystem))]
public class UIParticleSystem : MaskableGraphic public class UIParticleSystem : MaskableGraphic
{ {
public Texture particleTexture; public Texture particleTexture;
public Sprite particleSprite; public Sprite particleSprite;
private Transform _transform; private Transform _transform;
private ParticleSystem _particleSystem; private ParticleSystem _particleSystem;
private ParticleSystem.Particle[] _particles; private ParticleSystem.Particle[] _particles;
private UIVertex[] _quad = new UIVertex[4]; private UIVertex[] _quad = new UIVertex[4];
private Vector4 _uv = Vector4.zero; private Vector4 _uv = Vector4.zero;
private ParticleSystem.TextureSheetAnimationModule _textureSheetAnimation; private ParticleSystem.TextureSheetAnimationModule _textureSheetAnimation;
private int _textureSheetAnimationFrames; private int _textureSheetAnimationFrames;
private Vector2 _textureSheedAnimationFrameSize; private Vector2 _textureSheedAnimationFrameSize;
public override Texture mainTexture public override Texture mainTexture
{ {
get get
{ {
if (particleTexture) if (particleTexture)
{ {
return particleTexture; return particleTexture;
} }
if (particleSprite) if (particleSprite)
{ {
return particleSprite.texture; return particleSprite.texture;
} }
return null; return null;
} }
} }
protected bool Initialize() protected bool Initialize()
{ {
// initialize members // initialize members
if (_transform == null) if (_transform == null)
{ {
_transform = transform; _transform = transform;
} }
// prepare particle system // prepare particle system
ParticleSystemRenderer renderer = GetComponent<ParticleSystemRenderer>(); ParticleSystemRenderer renderer = GetComponent<ParticleSystemRenderer>();
bool setParticleSystemMaterial = false; bool setParticleSystemMaterial = false;
if (_particleSystem == null) if (_particleSystem == null)
{ {
_particleSystem = GetComponent<ParticleSystem>(); _particleSystem = GetComponent<ParticleSystem>();
if (_particleSystem == null) if (_particleSystem == null)
{ {
return false; return false;
} }
// get current particle texture // get current particle texture
if (renderer == null) if (renderer == null)
{ {
renderer = _particleSystem.gameObject.AddComponent<ParticleSystemRenderer>(); renderer = _particleSystem.gameObject.AddComponent<ParticleSystemRenderer>();
} }
Material currentMaterial = renderer.sharedMaterial; Material currentMaterial = renderer.sharedMaterial;
if (currentMaterial && currentMaterial.HasProperty("_MainTex")) if (currentMaterial && currentMaterial.HasProperty("_MainTex"))
{ {
particleTexture = currentMaterial.mainTexture; particleTexture = currentMaterial.mainTexture;
} }
// automatically set scaling // automatically set scaling
_particleSystem.scalingMode = ParticleSystemScalingMode.Local; var main = _particleSystem.main;
main.scalingMode = ParticleSystemScalingMode.Local;
_particles = null;
setParticleSystemMaterial = true; _particles = null;
} setParticleSystemMaterial = true;
else }
{ else
if (Application.isPlaying) {
{ if (Application.isPlaying)
setParticleSystemMaterial = (renderer.material == null); {
} setParticleSystemMaterial = (renderer.material == null);
#if UNITY_EDITOR }
else #if UNITY_EDITOR
{ else
setParticleSystemMaterial = (renderer.sharedMaterial == null); {
} setParticleSystemMaterial = (renderer.sharedMaterial == null);
#endif }
} #endif
}
// automatically set material to UI/Particles/Hidden shader, and get previous texture
if (setParticleSystemMaterial) // 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) Material material = new Material(Shader.Find("UI/Particles/Hidden"));
{ if (Application.isPlaying)
renderer.material = material; {
} renderer.material = material;
#if UNITY_EDITOR }
else #if UNITY_EDITOR
{ else
material.hideFlags = HideFlags.DontSave; {
renderer.sharedMaterial = material; material.hideFlags = HideFlags.DontSave;
} renderer.sharedMaterial = material;
#endif }
} #endif
}
// prepare particles array
if (_particles == null) // prepare particles array
{ if (_particles == null)
_particles = new ParticleSystem.Particle[_particleSystem.maxParticles]; {
} _particles = new ParticleSystem.Particle[_particleSystem.main.maxParticles];
}
// prepare uvs
if (particleTexture) // prepare uvs
{ if (particleTexture)
_uv = new Vector4(0, 0, 1, 1); {
} _uv = new Vector4(0, 0, 1, 1);
else if (particleSprite) }
{ else if (particleSprite)
_uv = UnityEngine.Sprites.DataUtility.GetOuterUV(particleSprite); {
} _uv = UnityEngine.Sprites.DataUtility.GetOuterUV(particleSprite);
}
// prepare texture sheet animation
_textureSheetAnimation = _particleSystem.textureSheetAnimation; // prepare texture sheet animation
_textureSheetAnimationFrames = 0; _textureSheetAnimation = _particleSystem.textureSheetAnimation;
_textureSheedAnimationFrameSize = Vector2.zero; _textureSheetAnimationFrames = 0;
if (_textureSheetAnimation.enabled) _textureSheedAnimationFrameSize = Vector2.zero;
{ if (_textureSheetAnimation.enabled)
_textureSheetAnimationFrames = _textureSheetAnimation.numTilesX * _textureSheetAnimation.numTilesY; {
_textureSheedAnimationFrameSize = new Vector2(1f / _textureSheetAnimation.numTilesX, 1f / _textureSheetAnimation.numTilesY); _textureSheetAnimationFrames = _textureSheetAnimation.numTilesX * _textureSheetAnimation.numTilesY;
} _textureSheedAnimationFrameSize = new Vector2(1f / _textureSheetAnimation.numTilesX, 1f / _textureSheetAnimation.numTilesY);
}
return true;
} return true;
}
protected override void Awake()
{ protected override void Awake()
base.Awake(); {
base.Awake();
if (!Initialize())
{ if (!Initialize())
enabled = false; {
} enabled = false;
} }
}
protected override void OnPopulateMesh(VertexHelper vh)
{ protected override void OnPopulateMesh(VertexHelper vh)
#if UNITY_EDITOR {
if (!Application.isPlaying) #if UNITY_EDITOR
{ if (!Application.isPlaying)
if (!Initialize()) {
{ if (!Initialize())
return; {
} return;
} }
#endif }
#endif
// prepare vertices
vh.Clear(); // prepare vertices
vh.Clear();
if (!gameObject.activeInHierarchy)
{ if (!gameObject.activeInHierarchy)
return; {
} return;
}
// iterate through current particles
int count = _particleSystem.GetParticles(_particles); // iterate through current particles
int count = _particleSystem.GetParticles(_particles);
for (int i = 0; i < count; ++i)
{ for (int i = 0; i < count; ++i)
ParticleSystem.Particle particle = _particles[i]; {
ParticleSystem.Particle particle = _particles[i];
// get particle properties
Vector2 position = (_particleSystem.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position)); // get particle properties
float rotation = -particle.rotation * Mathf.Deg2Rad; Vector2 position = (_particleSystem.main.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position));
float rotation90 = rotation + Mathf.PI / 2; float rotation = -particle.rotation * Mathf.Deg2Rad;
Color32 color = particle.GetCurrentColor(_particleSystem); float rotation90 = rotation + Mathf.PI / 2;
float size = particle.GetCurrentSize(_particleSystem) * 0.5f; Color32 color = particle.GetCurrentColor(_particleSystem);
float size = particle.GetCurrentSize(_particleSystem) * 0.5f;
// apply scale
if (_particleSystem.scalingMode == ParticleSystemScalingMode.Shape) // apply scale
{ if (_particleSystem.main.scalingMode == ParticleSystemScalingMode.Shape)
position /= canvas.scaleFactor; {
} position /= canvas.scaleFactor;
}
// apply texture sheet animation
Vector4 particleUV = _uv; // apply texture sheet animation
if (_textureSheetAnimation.enabled) 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 #if UNITY_5_5_OR_NEWER
frameProgress = Mathf.Repeat(frameProgress * _textureSheetAnimation.cycleCount, 1); float frameProgress = 1 - (particle.remainingLifetime / particle.startLifetime);
int frame = 0; #else
float frameProgress = 1 - (particle.lifetime / particle.startLifetime);
switch (_textureSheetAnimation.animation) #endif
{ // float frameProgress = textureSheetAnimation.frameOverTime.curveMin.Evaluate(1 - (particle.lifetime / particle.startLifetime)); // TODO - once Unity allows MinMaxCurve reading
frameProgress = Mathf.Repeat(frameProgress * _textureSheetAnimation.cycleCount, 1);
case ParticleSystemAnimationType.WholeSheet: int frame = 0;
frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimationFrames);
break; switch (_textureSheetAnimation.animation)
{
case ParticleSystemAnimationType.SingleRow:
frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimation.numTilesX); case ParticleSystemAnimationType.WholeSheet:
frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimationFrames);
int row = _textureSheetAnimation.rowIndex; break;
// if (textureSheetAnimation.useRandomRow) { // FIXME - is this handled internally by rowIndex?
// row = Random.Range(0, textureSheetAnimation.numTilesY, using: particle.randomSeed); case ParticleSystemAnimationType.SingleRow:
// } frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimation.numTilesX);
frame += row * _textureSheetAnimation.numTilesX;
break; int row = _textureSheetAnimation.rowIndex;
// if (textureSheetAnimation.useRandomRow) { // FIXME - is this handled internally by rowIndex?
} // row = Random.Range(0, textureSheetAnimation.numTilesY, using: particle.randomSeed);
// }
frame %= _textureSheetAnimationFrames; frame += row * _textureSheetAnimation.numTilesX;
break;
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; frame %= _textureSheetAnimationFrames;
}
particleUV.x = (frame % _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.x;
_quad[0] = UIVertex.simpleVert; particleUV.y = Mathf.FloorToInt(frame / _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.y;
_quad[0].color = color; particleUV.z = particleUV.x + _textureSheedAnimationFrameSize.x;
_quad[0].uv0 = new Vector2(particleUV.x, particleUV.y); particleUV.w = particleUV.y + _textureSheedAnimationFrameSize.y;
}
_quad[1] = UIVertex.simpleVert;
_quad[1].color = color; _quad[0] = UIVertex.simpleVert;
_quad[1].uv0 = new Vector2(particleUV.x, particleUV.w); _quad[0].color = color;
_quad[0].uv0 = new Vector2(particleUV.x, particleUV.y);
_quad[2] = UIVertex.simpleVert;
_quad[2].color = color; _quad[1] = UIVertex.simpleVert;
_quad[2].uv0 = new Vector2(particleUV.z, particleUV.w); _quad[1].color = color;
_quad[1].uv0 = new Vector2(particleUV.x, particleUV.w);
_quad[3] = UIVertex.simpleVert;
_quad[3].color = color; _quad[2] = UIVertex.simpleVert;
_quad[3].uv0 = new Vector2(particleUV.z, particleUV.y); _quad[2].color = color;
_quad[2].uv0 = new Vector2(particleUV.z, particleUV.w);
if (rotation == 0)
{ _quad[3] = UIVertex.simpleVert;
// no rotation _quad[3].color = color;
Vector2 corner1 = new Vector2(position.x - size, position.y - size); _quad[3].uv0 = new Vector2(particleUV.z, particleUV.y);
Vector2 corner2 = new Vector2(position.x + size, position.y + size);
if (rotation == 0)
_quad[0].position = new Vector2(corner1.x, corner1.y); {
_quad[1].position = new Vector2(corner1.x, corner2.y); // no rotation
_quad[2].position = new Vector2(corner2.x, corner2.y); Vector2 corner1 = new Vector2(position.x - size, position.y - size);
_quad[3].position = new Vector2(corner2.x, corner1.y); Vector2 corner2 = new Vector2(position.x + size, position.y + size);
}
else _quad[0].position = new Vector2(corner1.x, corner1.y);
{ _quad[1].position = new Vector2(corner1.x, corner2.y);
// apply rotation _quad[2].position = new Vector2(corner2.x, corner2.y);
Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size; _quad[3].position = new Vector2(corner2.x, corner1.y);
Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size; }
else
_quad[0].position = position - right - up; {
_quad[1].position = position - right + up; // apply rotation
_quad[2].position = position + right + up; Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size;
_quad[3].position = position + right - up; Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size;
}
_quad[0].position = position - right - up;
vh.AddUIVertexQuad(_quad); _quad[1].position = position - right + up;
} _quad[2].position = position + right + up;
} _quad[3].position = position + right - up;
}
void Update()
{ vh.AddUIVertexQuad(_quad);
if (Application.isPlaying) }
{ }
// unscaled animation within UI
_particleSystem.Simulate(Time.unscaledDeltaTime, false, false); void Update()
{
SetAllDirty(); if (Application.isPlaying)
} {
} // unscaled animation within UI
_particleSystem.Simulate(Time.unscaledDeltaTime, false, false);
#if UNITY_EDITOR
void LateUpdate() SetAllDirty();
{ }
if (!Application.isPlaying) }
{
SetAllDirty(); #if UNITY_EDITOR
} void LateUpdate()
} {
#endif if (!Application.isPlaying)
} {
#endif SetAllDirty();
}
}
#endif
}
#endif
} }