From 8e7c51d32ef36df8878565f11afcfaeb930c3d29 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Tue, 31 Jul 2018 19:31:11 +0900 Subject: [PATCH] Remove 'additional shadow' in UIShadow component #110 --- .../UIExtensions/UIEffect/Scripts/UIEffect.cs | 31 +++- .../UIExtensions/UIEffect/Scripts/UIShadow.cs | 146 +++++++++++++----- 2 files changed, 129 insertions(+), 48 deletions(-) diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs index 855ffe4a..ba088afb 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIEffect.cs @@ -333,13 +333,30 @@ protected override void UpgradeIfNeeded() if (m_ShadowStyle != ShadowStyle.None || m_AdditionalShadows.Any(x=>x.style != ShadowStyle.None)) { - var shadow = gameObject.GetComponent() ?? gameObject.AddComponent(); - shadow.style = m_ShadowStyle; - shadow.effectDistance = m_EffectDistance; - shadow.effectColor = m_ShadowColor; - shadow.useGraphicAlpha = m_UseGraphicAlpha; - shadow.blur = m_ShadowBlur; - shadow.additionalShadows.AddRange(m_AdditionalShadows); + if (m_ShadowStyle != ShadowStyle.None) + { + var shadow = gameObject.GetComponent() ?? gameObject.AddComponent(); + shadow.style = m_ShadowStyle; + shadow.effectDistance = m_EffectDistance; + shadow.effectColor = m_ShadowColor; + shadow.useGraphicAlpha = m_UseGraphicAlpha; + shadow.blur = m_ShadowBlur; + } + + foreach(var s in m_AdditionalShadows) + { + if (s.style == ShadowStyle.None) + { + continue; + } + + var shadow = gameObject.AddComponent(); + shadow.style = s.style; + shadow.effectDistance = s.effectDistance; + shadow.effectColor = s.effectColor; + shadow.useGraphicAlpha = s.useGraphicAlpha; + shadow.blur = s.blur; + } m_ShadowStyle = ShadowStyle.None; m_AdditionalShadows = null; diff --git a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs index d869c868..0e7170ee 100644 --- a/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs +++ b/Assets/Coffee/UIExtensions/UIEffect/Scripts/UIShadow.cs @@ -16,13 +16,18 @@ namespace Coffee.UIExtensions /// UIEffect. /// // [ExecuteInEditMode] -// [RequireComponent(typeof(Graphic))] + [RequireComponent(typeof(Graphic))] // [DisallowMultipleComponent] public class UIShadow : Shadow, IParameterTexture +#if UNITY_EDITOR + , ISerializationCallbackReceiver +#endif { + /// /// Additional shadow. /// + [System.Obsolete] [System.Serializable] public class AdditionalShadow { @@ -58,19 +63,15 @@ public class AdditionalShadow //################################ // Serialize Members. //################################ - [SerializeField][Range(0, 1)] float m_Blur = 0.25f; + [SerializeField][Range(0, 1)] float m_Blur = 1; [SerializeField] ShadowStyle m_Style = ShadowStyle.Shadow; + [HideInInspector][System.Obsolete] [SerializeField] List m_AdditionalShadows = new List(); //################################ // Public Members. //################################ - /// - /// Graphic affected by the UIEffect. - /// - new public Graphic graphic { get { return base.graphic; } } - /// /// How far is the blurring shadow from the graphic. /// @@ -80,11 +81,6 @@ public class AdditionalShadow /// Shadow effect mode. /// public ShadowStyle style { get { return m_Style; } set { m_Style = value; _SetDirty(); } } - - /// - /// Additional Shadows. - /// - public List additionalShadows { get { return m_AdditionalShadows; } } /// /// Gets or sets the parameter index. @@ -96,9 +92,21 @@ public class AdditionalShadow /// public ParameterTexture ptex{get;private set;} + int _graphicVertexCount; + int _start; + int _end; + static readonly List tmpShadows = new List(); + protected override void OnEnable() { base.OnEnable(); + +// GetComponents(_shadows); +// foreach(var s in _shadows) +// { +// s._shadows = _shadows; +// } + _uiEffect = GetComponent(); if(_uiEffect) { @@ -118,27 +126,54 @@ protected override void OnDisable() } } +// protected override void OnDestroy() +// { +// _shadows.Remove(this); +// foreach(var s in _shadows) +// { +// s._shadows = _shadows; +// } +// _shadows = null; +// } + /// /// Modifies the mesh. /// public override void ModifyMesh(VertexHelper vh) { - if (!isActiveAndEnabled || vh.currentVertCount <= 0) + if (!isActiveAndEnabled || vh.currentVertCount <= 0 || m_Style == ShadowStyle.None) { return; } vh.GetUIVertexStream(s_Verts); + GetComponents(tmpShadows); + + foreach (var s in tmpShadows) + { + if (s.isActiveAndEnabled) + { + if (s == this) + { + foreach (var s2 in tmpShadows) + { + s2._graphicVertexCount = s_Verts.Count; + } + } + break; + } + } + + tmpShadows.Clear(); + //================================ // Append shadow vertices. //================================ { _uiEffect = GetComponent(); - var inputVertCount = s_Verts.Count; - var start = 0; - var end = inputVertCount; - var toneLevel = _uiEffect && _uiEffect.isActiveAndEnabled ? _uiEffect.toneLevel : 0; + var start = s_Verts.Count - _graphicVertexCount; + var end = s_Verts.Count; if(ptex != null && _uiEffect && _uiEffect.isActiveAndEnabled) { @@ -147,16 +182,6 @@ public override void ModifyMesh(VertexHelper vh) ptex.SetData(this, 2, m_Blur); // param.z : blur factor } -// // Additional Shadows. -// for (int i = additionalShadows.Count - 1; 0 <= i; i--) -// { -// AdditionalShadow shadow = additionalShadows[i]; -// UpdateFactor(toneLevel, shadow.blur, shadow.effectColor); -// _ApplyShadow(s_Verts, shadow.effectColor, ref start, ref end, shadow.effectDistance, shadow.style, shadow.useGraphicAlpha); -// } - - // Shadow. -// UpdateFactor(toneLevel, blur, effectColor); _ApplyShadow(s_Verts, effectColor, ref start, ref end, effectDistance, style, useGraphicAlpha); } @@ -167,21 +192,12 @@ public override void ModifyMesh(VertexHelper vh) } UIEffect _uiEffect; -// Vector2 _factor; //################################ // Private Members. //################################ static readonly List s_Verts = new List(); -// void UpdateFactor(float tone, float blur, Color color) -// { -// if (_uiEffect && _uiEffect.isActiveAndEnabled) -// { -// _factor = new Vector2(Packer.ToFloat(tone, 1, blur, 0), 0); -// } -// } - /// /// Append shadow vertices. /// * It is similar to Shadow component implementation. @@ -229,7 +245,8 @@ void _ApplyShadow(List verts, Color color, ref int start, ref int end, void _ApplyShadowZeroAlloc(List verts, Color color, ref int start, ref int end, float x, float y, bool useGraphicAlpha) { // Check list capacity. - var neededCapacity = verts.Count + end - start; + int count = end - start; + var neededCapacity = verts.Count + count; if (verts.Capacity < neededCapacity) verts.Capacity = neededCapacity; @@ -237,13 +254,24 @@ void _ApplyShadowZeroAlloc(List verts, Color color, ref int start, ref ? ptex.GetNormalizedIndex(this) : -1; + // Add + UIVertex vt = default(UIVertex); + for (int i = 0; i < count; i++) + { + verts.Add(vt); + } + + // Move + for (int i = verts.Count-1; count <= i; i--) + { + verts[i] = verts[i - count]; + } + // Append shadow vertices to the front of list. // * The original vertex is pushed backward. - UIVertex vt; - for (int i = start; i < end; ++i) + for (int i = 0; i < count; ++i) { - vt = verts[i]; - verts.Add(vt); + vt = verts[i + start + count]; Vector3 v = vt.position; vt.position.Set(v.x + x, v.y + y, v.z); @@ -279,5 +307,41 @@ void _SetDirty() if(graphic) graphic.SetVerticesDirty(); } + +#if UNITY_EDITOR + public void OnBeforeSerialize() + { + } + + public void OnAfterDeserialize() + { + EditorApplication.delayCall += UpgradeIfNeeded; + } + + + #pragma warning disable 0612 + void UpgradeIfNeeded() + { + if (0 < m_AdditionalShadows.Count) + { + foreach (var s in m_AdditionalShadows) + { + if (s.style == ShadowStyle.None) + { + continue; + } + + var shadow = gameObject.AddComponent(); + shadow.style = s.style; + shadow.effectDistance = s.effectDistance; + shadow.effectColor = s.effectColor; + shadow.useGraphicAlpha = s.useGraphicAlpha; + shadow.blur = s.blur; + } + m_AdditionalShadows = null; + } + } + #pragma warning restore 0612 +#endif } }