2020-08-28 13:38:13 +08:00
using System.Collections.Generic ;
using System.Runtime.CompilerServices ;
2018-06-22 18:48:14 +08:00
using UnityEngine ;
2020-08-20 03:42:16 +08:00
using UnityEngine.Rendering ;
2020-08-28 13:38:13 +08:00
using UnityEngine.Serialization ;
2018-06-22 18:48:14 +08:00
using UnityEngine.UI ;
2020-08-28 13:38:13 +08:00
[assembly: InternalsVisibleTo("Coffee.UIParticle.Editor")]
2018-06-22 18:48:14 +08:00
namespace Coffee.UIExtensions
{
2020-02-12 20:38:06 +08:00
/// <summary>
/// Render maskable and sortable particle effect ,without Camera, RenderTexture or Canvas.
/// </summary>
[ExecuteInEditMode]
2020-08-28 13:38:13 +08:00
[RequireComponent(typeof(RectTransform))]
2020-08-11 23:09:55 +08:00
[RequireComponent(typeof(CanvasRenderer))]
2020-02-12 20:38:06 +08:00
public class UIParticle : MaskableGraphic
{
[HideInInspector] [ SerializeField ] bool m_IsTrail = false ;
2020-08-28 13:38:13 +08:00
[Tooltip("Ignore canvas scaler")] [ SerializeField ] [ FormerlySerializedAs ( "m_IgnoreParent" ) ]
bool m_IgnoreCanvasScaler = true ;
2020-08-12 22:19:08 +08:00
2020-08-20 03:42:16 +08:00
[Tooltip("Particle effect scale")] [ SerializeField ]
2020-08-28 13:38:13 +08:00
float m_Scale = 100 ;
2020-02-12 20:38:06 +08:00
2020-08-20 03:42:16 +08:00
[Tooltip("Animatable material properties. If you want to change the material properties of the ParticleSystem in Animation, enable it.")] [ SerializeField ]
internal AnimatableProperty [ ] m_AnimatableProperties = new AnimatableProperty [ 0 ] ;
2020-02-12 20:38:06 +08:00
2020-08-28 13:38:13 +08:00
[Tooltip("Particles")] [ SerializeField ]
private List < ParticleSystem > m_Particles = new List < ParticleSystem > ( ) ;
2020-02-12 20:38:06 +08:00
2020-08-20 03:42:16 +08:00
private DrivenRectTransformTracker _tracker ;
private Mesh _bakedMesh ;
2020-08-28 13:38:13 +08:00
private readonly List < Material > _modifiedMaterials = new List < Material > ( ) ;
private uint _activeMeshIndices ;
private Vector3 _cachedPosition ;
private static readonly List < Material > s_TempMaterials = new List < Material > ( 2 ) ;
2020-08-20 03:42:16 +08:00
/// <summary>
/// Should this graphic be considered a target for raycasting?
/// </summary>
public override bool raycastTarget
2020-02-12 20:38:06 +08:00
{
2020-08-20 03:42:16 +08:00
get { return false ; }
2020-08-28 13:38:13 +08:00
set { }
2020-08-20 03:42:16 +08:00
}
2020-02-12 20:38:06 +08:00
2020-08-20 03:42:16 +08:00
public bool ignoreCanvasScaler
{
get { return m_IgnoreCanvasScaler ; }
set { m_IgnoreCanvasScaler = value ; }
2020-02-12 20:38:06 +08:00
}
/// <summary>
/// Particle effect scale.
/// </summary>
2020-08-12 22:19:08 +08:00
public float scale
{
2020-08-28 13:38:13 +08:00
get { return m_Scale ; }
set { m_Scale = Mathf . Max ( 0.001f , value ) ; }
2020-08-12 22:19:08 +08:00
}
2020-02-12 20:38:06 +08:00
2020-08-28 13:38:13 +08:00
internal Mesh bakedMesh
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
get { return _bakedMesh ; }
2020-02-12 20:38:06 +08:00
}
2020-08-28 13:38:13 +08:00
public List < ParticleSystem > particles
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
get { return m_Particles ; }
2020-02-12 20:38:06 +08:00
}
2020-08-28 13:38:13 +08:00
public IEnumerable < Material > materials
2020-08-12 22:19:08 +08:00
{
2020-08-28 13:38:13 +08:00
get { return _modifiedMaterials ; }
2020-08-12 22:19:08 +08:00
}
2020-02-12 20:38:06 +08:00
2020-08-28 13:38:13 +08:00
internal uint activeMeshIndices
2020-08-12 22:19:08 +08:00
{
2020-08-28 13:38:13 +08:00
get { return _activeMeshIndices ; }
set
{
if ( _activeMeshIndices = = value ) return ;
_activeMeshIndices = value ;
UpdateMaterial ( ) ;
}
2020-08-12 22:19:08 +08:00
}
2020-02-12 20:38:06 +08:00
2020-08-28 13:38:13 +08:00
internal Vector3 cachedPosition
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
get { return _cachedPosition ; }
set { _cachedPosition = value ; }
2020-08-20 03:42:16 +08:00
}
2020-02-12 20:38:06 +08:00
2020-08-28 13:38:13 +08:00
public void Play ( )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
particles . Exec ( p = > p . Play ( ) ) ;
2020-02-12 20:38:06 +08:00
}
2020-08-28 13:38:13 +08:00
public void Pause ( )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
particles . Exec ( p = > p . Pause ( ) ) ;
2020-08-20 03:42:16 +08:00
}
2020-08-28 13:38:13 +08:00
public void Stop ( )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
particles . Exec ( p = > p . Stop ( ) ) ;
2020-08-20 03:42:16 +08:00
}
2020-08-28 13:38:13 +08:00
public void RefreshParticles ( )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
GetComponentsInChildren ( particles ) ;
particles . Exec ( p = > p . GetComponent < ParticleSystemRenderer > ( ) . enabled = ! enabled ) ;
particles . SortForRendering ( transform ) ;
SetMaterialDirty ( ) ;
2020-08-20 03:42:16 +08:00
}
protected override void UpdateMaterial ( )
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
// Clear modified materials.
for ( var i = 0 ; i < _modifiedMaterials . Count ; i + + )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
StencilMaterial . Remove ( _modifiedMaterials [ i ] ) ;
DestroyImmediate ( _modifiedMaterials [ i ] ) ;
_modifiedMaterials [ i ] = null ;
2020-08-20 03:42:16 +08:00
}
2020-08-28 13:38:13 +08:00
_modifiedMaterials . Clear ( ) ;
// Recalculate stencil value.
if ( m_ShouldRecalculateStencil )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
var rootCanvas = MaskUtilities . FindRootSortOverrideCanvas ( transform ) ;
m_StencilValue = maskable ? MaskUtilities . GetStencilDepth ( transform , rootCanvas ) : 0 ;
m_ShouldRecalculateStencil = false ;
2020-08-20 03:42:16 +08:00
}
2020-08-28 13:38:13 +08:00
// No mesh to render.
if ( activeMeshIndices = = 0 | | ! isActiveAndEnabled | | particles . Count = = 0 )
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
_activeMeshIndices = 0 ;
canvasRenderer . Clear ( ) ;
return ;
2020-02-12 20:38:06 +08:00
}
2020-08-12 22:19:08 +08:00
2020-08-28 13:38:13 +08:00
/ /
var materialCount = Mathf . Max ( 8 , activeMeshIndices . BitCount ( ) ) ;
canvasRenderer . materialCount = materialCount ;
var j = 0 ;
for ( var i = 0 ; i < particles . Count ; i + + )
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
if ( materialCount < = j ) break ;
var ps = particles [ i ] ;
if ( ! ps ) continue ;
2020-08-20 03:42:16 +08:00
2020-08-28 13:38:13 +08:00
var r = ps . GetComponent < ParticleSystemRenderer > ( ) ;
r . GetSharedMaterials ( s_TempMaterials ) ;
// Main
var bit = 1 < < ( i * 2 ) ;
if ( 0 < ( activeMeshIndices & bit ) & & 0 < s_TempMaterials . Count )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
var mat = GetModifiedMaterial ( s_TempMaterials [ 0 ] , ps . GetTextureForSprite ( ) ) ;
canvasRenderer . SetMaterial ( mat , j + + ) ;
2020-08-20 03:42:16 +08:00
}
2020-08-28 13:38:13 +08:00
// Trails
if ( materialCount < = j ) break ;
bit < < = 1 ;
if ( 0 < ( activeMeshIndices & bit ) & & 1 < s_TempMaterials . Count )
2020-08-20 03:42:16 +08:00
{
2020-08-28 13:38:13 +08:00
var mat = GetModifiedMaterial ( s_TempMaterials [ 1 ] , null ) ;
canvasRenderer . SetMaterial ( mat , j + + ) ;
2020-08-20 03:42:16 +08:00
}
2020-02-12 20:38:06 +08:00
}
2020-08-28 13:38:13 +08:00
}
private Material GetModifiedMaterial ( Material baseMaterial , Texture2D texture )
{
if ( 0 < m_StencilValue )
{
baseMaterial = StencilMaterial . Add ( baseMaterial , ( 1 < < m_StencilValue ) - 1 , StencilOp . Keep , CompareFunction . Equal , ColorWriteMask . All , ( 1 < < m_StencilValue ) - 1 , 0 ) ;
_modifiedMaterials . Add ( baseMaterial ) ;
}
2020-08-12 22:19:08 +08:00
2020-08-28 13:38:13 +08:00
if ( texture = = null & & m_AnimatableProperties . Length = = 0 ) return baseMaterial ;
2020-08-20 03:42:16 +08:00
2020-08-28 13:38:13 +08:00
baseMaterial = new Material ( baseMaterial ) ;
_modifiedMaterials . Add ( baseMaterial ) ;
if ( texture )
baseMaterial . mainTexture = texture ;
2020-08-20 03:42:16 +08:00
2020-08-28 13:38:13 +08:00
return baseMaterial ;
2020-08-20 03:42:16 +08:00
}
/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
protected override void OnEnable ( )
{
2020-08-28 13:38:13 +08:00
InitializeIfNeeded ( ) ;
2020-02-12 20:38:06 +08:00
2020-08-28 13:38:13 +08:00
_cachedPosition = transform . localPosition ;
_activeMeshIndices = 0 ;
2020-08-20 03:42:16 +08:00
2020-08-28 13:38:13 +08:00
UIParticleUpdater . Register ( this ) ;
particles . Exec ( p = > p . GetComponent < ParticleSystemRenderer > ( ) . enabled = false ) ;
_tracker . Add ( this , rectTransform , DrivenTransformProperties . Scale ) ;
2020-02-12 20:38:06 +08:00
// Create objects.
2020-08-20 03:42:16 +08:00
_bakedMesh = new Mesh ( ) ;
_bakedMesh . MarkDynamic ( ) ;
2020-02-12 20:38:06 +08:00
base . OnEnable ( ) ;
}
/// <summary>
/// This function is called when the behaviour becomes disabled.
/// </summary>
protected override void OnDisable ( )
{
2020-08-28 13:38:13 +08:00
UIParticleUpdater . Unregister ( this ) ;
particles . Exec ( p = > p . GetComponent < ParticleSystemRenderer > ( ) . enabled = true ) ;
2020-08-20 03:42:16 +08:00
_tracker . Clear ( ) ;
2020-08-12 22:19:08 +08:00
2020-08-20 03:42:16 +08:00
// Destroy object.
DestroyImmediate ( _bakedMesh ) ;
_bakedMesh = null ;
2020-02-12 20:38:06 +08:00
2020-08-20 03:42:16 +08:00
base . OnDisable ( ) ;
2020-02-12 20:38:06 +08:00
}
2019-02-26 10:26:56 +08:00
2020-02-12 20:38:06 +08:00
/// <summary>
/// Call to update the geometry of the Graphic onto the CanvasRenderer.
/// </summary>
protected override void UpdateGeometry ( )
{
}
/// <summary>
/// This function is called when the parent property of the transform of the GameObject has changed.
/// </summary>
protected override void OnTransformParentChanged ( )
{
}
/// <summary>
/// Callback for when properties have been changed by animation.
/// </summary>
protected override void OnDidApplyAnimationProperties ( )
{
}
2018-11-28 13:19:33 +08:00
2020-08-28 13:38:13 +08:00
private void InitializeIfNeeded ( )
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
if ( 0 < particles . Count ) return ;
2020-08-12 22:19:08 +08:00
2020-08-28 13:38:13 +08:00
if ( m_IsTrail
| | transform . parent & & transform . parent . GetComponentInParent < UIParticle > ( ) )
2020-02-12 20:38:06 +08:00
{
2020-08-28 13:38:13 +08:00
gameObject . SetActive ( false ) ;
if ( Application . isPlaying )
Destroy ( gameObject ) ;
2020-08-20 03:42:16 +08:00
else
2020-08-28 13:38:13 +08:00
DestroyImmediate ( gameObject ) ;
return ;
2020-02-12 20:38:06 +08:00
}
2020-08-20 03:42:16 +08:00
2020-08-28 13:38:13 +08:00
// refresh.
#if UNITY_EDITOR
if ( ! Application . isPlaying )
UnityEditor . EditorApplication . delayCall + = RefreshParticles ;
2020-08-20 03:42:16 +08:00
else
2020-08-28 13:38:13 +08:00
# endif
RefreshParticles ( ) ;
2020-08-12 22:19:08 +08:00
}
2020-02-12 20:38:06 +08:00
}
2020-04-30 11:28:48 +08:00
}