API update guarded with #if directive

pull/413/head
Mauro Ronchi 2017-06-29 15:07:29 +02:00
parent 42c2a5dace
commit 78c2d42a9a
1 changed files with 296 additions and 292 deletions

View File

@ -1,293 +1,297 @@
/// 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; _particleSystem.scalingMode = ParticleSystemScalingMode.Local;
_particles = null; _particles = null;
setParticleSystemMaterial = true; setParticleSystemMaterial = true;
} }
else else
{ {
if (Application.isPlaying) if (Application.isPlaying)
{ {
setParticleSystemMaterial = (renderer.material == null); setParticleSystemMaterial = (renderer.material == null);
} }
#if UNITY_EDITOR #if UNITY_EDITOR
else else
{ {
setParticleSystemMaterial = (renderer.sharedMaterial == null); setParticleSystemMaterial = (renderer.sharedMaterial == null);
} }
#endif #endif
} }
// automatically set material to UI/Particles/Hidden shader, and get previous texture // automatically set material to UI/Particles/Hidden shader, and get previous texture
if (setParticleSystemMaterial) if (setParticleSystemMaterial)
{ {
Material material = new Material(Shader.Find("UI/Particles/Hidden")); Material material = new Material(Shader.Find("UI/Particles/Hidden"));
if (Application.isPlaying) if (Application.isPlaying)
{ {
renderer.material = material; renderer.material = material;
} }
#if UNITY_EDITOR #if UNITY_EDITOR
else else
{ {
material.hideFlags = HideFlags.DontSave; material.hideFlags = HideFlags.DontSave;
renderer.sharedMaterial = material; renderer.sharedMaterial = material;
} }
#endif #endif
} }
// prepare particles array // prepare particles array
if (_particles == null) if (_particles == null)
{ {
_particles = new ParticleSystem.Particle[_particleSystem.maxParticles]; _particles = new ParticleSystem.Particle[_particleSystem.maxParticles];
} }
// prepare uvs // prepare uvs
if (particleTexture) 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 // prepare texture sheet animation
_textureSheetAnimation = _particleSystem.textureSheetAnimation; _textureSheetAnimation = _particleSystem.textureSheetAnimation;
_textureSheetAnimationFrames = 0; _textureSheetAnimationFrames = 0;
_textureSheedAnimationFrameSize = Vector2.zero; _textureSheedAnimationFrameSize = Vector2.zero;
if (_textureSheetAnimation.enabled) if (_textureSheetAnimation.enabled)
{ {
_textureSheetAnimationFrames = _textureSheetAnimation.numTilesX * _textureSheetAnimation.numTilesY; _textureSheetAnimationFrames = _textureSheetAnimation.numTilesX * _textureSheetAnimation.numTilesY;
_textureSheedAnimationFrameSize = new Vector2(1f / _textureSheetAnimation.numTilesX, 1f / _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 UNITY_EDITOR
if (!Application.isPlaying) if (!Application.isPlaying)
{ {
if (!Initialize()) if (!Initialize())
{ {
return; return;
} }
} }
#endif #endif
// prepare vertices // prepare vertices
vh.Clear(); vh.Clear();
if (!gameObject.activeInHierarchy) if (!gameObject.activeInHierarchy)
{ {
return; return;
} }
// iterate through current particles // iterate through current particles
int count = _particleSystem.GetParticles(_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 // get particle properties
Vector2 position = (_particleSystem.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position)); Vector2 position = (_particleSystem.simulationSpace == ParticleSystemSimulationSpace.Local ? particle.position : _transform.InverseTransformPoint(particle.position));
float rotation = -particle.rotation * Mathf.Deg2Rad; float rotation = -particle.rotation * Mathf.Deg2Rad;
float rotation90 = rotation + Mathf.PI / 2; float rotation90 = rotation + Mathf.PI / 2;
Color32 color = particle.GetCurrentColor(_particleSystem); Color32 color = particle.GetCurrentColor(_particleSystem);
float size = particle.GetCurrentSize(_particleSystem) * 0.5f; float size = particle.GetCurrentSize(_particleSystem) * 0.5f;
// apply scale // apply scale
if (_particleSystem.scalingMode == ParticleSystemScalingMode.Shape) if (_particleSystem.scalingMode == ParticleSystemScalingMode.Shape)
{ {
position /= canvas.scaleFactor; position /= canvas.scaleFactor;
} }
// apply texture sheet animation // apply texture sheet animation
Vector4 particleUV = _uv; Vector4 particleUV = _uv;
if (_textureSheetAnimation.enabled) if (_textureSheetAnimation.enabled)
{ {
float frameProgress = 1 - (particle.remainingLifetime / particle.startLifetime); #if UNITY_5_5_OR_NEWER
// float frameProgress = textureSheetAnimation.frameOverTime.curveMin.Evaluate(1 - (particle.lifetime / particle.startLifetime)); // TODO - once Unity allows MinMaxCurve reading float frameProgress = 1 - (particle.remainingLifetime / particle.startLifetime);
frameProgress = Mathf.Repeat(frameProgress * _textureSheetAnimation.cycleCount, 1); #else
int frame = 0; float frameProgress = 1 - (particle.lifetime / particle.startLifetime);
#endif
switch (_textureSheetAnimation.animation) // 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;
case ParticleSystemAnimationType.WholeSheet:
frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimationFrames); switch (_textureSheetAnimation.animation)
break; {
case ParticleSystemAnimationType.SingleRow: case ParticleSystemAnimationType.WholeSheet:
frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimation.numTilesX); frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimationFrames);
break;
int row = _textureSheetAnimation.rowIndex;
// if (textureSheetAnimation.useRandomRow) { // FIXME - is this handled internally by rowIndex? case ParticleSystemAnimationType.SingleRow:
// row = Random.Range(0, textureSheetAnimation.numTilesY, using: particle.randomSeed); frame = Mathf.FloorToInt(frameProgress * _textureSheetAnimation.numTilesX);
// }
frame += row * _textureSheetAnimation.numTilesX; int row = _textureSheetAnimation.rowIndex;
break; // if (textureSheetAnimation.useRandomRow) { // FIXME - is this handled internally by rowIndex?
// row = Random.Range(0, textureSheetAnimation.numTilesY, using: particle.randomSeed);
} // }
frame += row * _textureSheetAnimation.numTilesX;
frame %= _textureSheetAnimationFrames; break;
particleUV.x = (frame % _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.x; }
particleUV.y = Mathf.FloorToInt(frame / _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.y;
particleUV.z = particleUV.x + _textureSheedAnimationFrameSize.x; frame %= _textureSheetAnimationFrames;
particleUV.w = particleUV.y + _textureSheedAnimationFrameSize.y;
} particleUV.x = (frame % _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.x;
particleUV.y = Mathf.FloorToInt(frame / _textureSheetAnimation.numTilesX) * _textureSheedAnimationFrameSize.y;
_quad[0] = UIVertex.simpleVert; particleUV.z = particleUV.x + _textureSheedAnimationFrameSize.x;
_quad[0].color = color; particleUV.w = particleUV.y + _textureSheedAnimationFrameSize.y;
_quad[0].uv0 = new Vector2(particleUV.x, particleUV.y); }
_quad[1] = UIVertex.simpleVert; _quad[0] = UIVertex.simpleVert;
_quad[1].color = color; _quad[0].color = color;
_quad[1].uv0 = new Vector2(particleUV.x, particleUV.w); _quad[0].uv0 = new Vector2(particleUV.x, particleUV.y);
_quad[2] = UIVertex.simpleVert; _quad[1] = UIVertex.simpleVert;
_quad[2].color = color; _quad[1].color = color;
_quad[2].uv0 = new Vector2(particleUV.z, particleUV.w); _quad[1].uv0 = new Vector2(particleUV.x, particleUV.w);
_quad[3] = UIVertex.simpleVert; _quad[2] = UIVertex.simpleVert;
_quad[3].color = color; _quad[2].color = color;
_quad[3].uv0 = new Vector2(particleUV.z, particleUV.y); _quad[2].uv0 = new Vector2(particleUV.z, particleUV.w);
if (rotation == 0) _quad[3] = UIVertex.simpleVert;
{ _quad[3].color = color;
// no rotation _quad[3].uv0 = new Vector2(particleUV.z, particleUV.y);
Vector2 corner1 = new Vector2(position.x - size, position.y - size);
Vector2 corner2 = new Vector2(position.x + size, position.y + size); if (rotation == 0)
{
_quad[0].position = new Vector2(corner1.x, corner1.y); // no rotation
_quad[1].position = new Vector2(corner1.x, corner2.y); Vector2 corner1 = new Vector2(position.x - size, position.y - size);
_quad[2].position = new Vector2(corner2.x, corner2.y); Vector2 corner2 = new Vector2(position.x + size, position.y + size);
_quad[3].position = new Vector2(corner2.x, corner1.y);
} _quad[0].position = new Vector2(corner1.x, corner1.y);
else _quad[1].position = new Vector2(corner1.x, corner2.y);
{ _quad[2].position = new Vector2(corner2.x, corner2.y);
// apply rotation _quad[3].position = new Vector2(corner2.x, corner1.y);
Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size; }
Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size; else
{
_quad[0].position = position - right - up; // apply rotation
_quad[1].position = position - right + up; Vector2 right = new Vector2(Mathf.Cos(rotation), Mathf.Sin(rotation)) * size;
_quad[2].position = position + right + up; Vector2 up = new Vector2(Mathf.Cos(rotation90), Mathf.Sin(rotation90)) * size;
_quad[3].position = position + right - up;
} _quad[0].position = position - right - up;
_quad[1].position = position - right + up;
vh.AddUIVertexQuad(_quad); _quad[2].position = position + right + up;
} _quad[3].position = position + right - up;
} }
void Update() vh.AddUIVertexQuad(_quad);
{ }
if (Application.isPlaying) }
{
// unscaled animation within UI void Update()
_particleSystem.Simulate(Time.unscaledDeltaTime, false, false); {
if (Application.isPlaying)
SetAllDirty(); {
} // unscaled animation within UI
} _particleSystem.Simulate(Time.unscaledDeltaTime, false, false);
#if UNITY_EDITOR SetAllDirty();
void LateUpdate() }
{ }
if (!Application.isPlaying)
{ #if UNITY_EDITOR
SetAllDirty(); void LateUpdate()
} {
} if (!Application.isPlaying)
#endif {
} SetAllDirty();
#endif }
}
#endif
}
#endif
} }