Example #1
0
 public void OnAfterDeserialize()
 {
     EditorApplication.delayCall += () =>
     {
         var old = m_EffectMaterial;
         m_EffectMaterial = UIEffect.GetMaterial(Shader.Find(shaderName), toneMode, colorMode, blurMode);
         if (old != m_EffectMaterial)
         {
             EditorUtility.SetDirty(this);
             EditorApplication.delayCall += AssetDatabase.SaveAssets;
         }
     };
 }
        /// <summary>
        /// Mark the UIEffect as dirty.
        /// </summary>
        /// <param name="isMaterialDirty">If set to true material dirty.</param>
        void SetDirty(bool isMaterialDirty = false)
        {
            //
            if (!mainEffect)
            {
                mainEffect = GetComponent <UIEffect>();
            }

            // Only main effect update material.
            if (mainEffect != this)
            {
                m_ToneMode  = mainEffect.m_ToneMode;
                m_ColorMode = mainEffect.m_ColorMode;
                m_BlurMode  = mainEffect.m_BlurMode;
                return;
            }

            // Update material if needed.
            if (isMaterialDirty && mainEffect == this)
            {
                const int TONE_SHIFT     = 0;
                const int TONE_GRAYSCALE = (int)ToneMode.Grayscale;
                const int TONE_SEPIA     = (int)ToneMode.Sepia;
                const int TONE_NEGA      = (int)ToneMode.Nega;

                const int COLOR_SHIFT = 2;
                const int COLOR_SET   = (int)ColorMode.Set;
                const int COLOR_ADD   = (int)ColorMode.Add;
                const int COLOR_SUB   = (int)ColorMode.Sub;

                const int BLUR_SHIFT  = 4;
                const int BLUR_FAST   = (int)BlurMode.Fast;
                const int BLUR_DETAIL = (int)BlurMode.Detail;

                // Calculate shader keyword identifier from effect modes.
                int identifier = ((int)m_ToneMode << TONE_SHIFT) | ((int)m_ColorMode << COLOR_SHIFT) | ((int)m_BlurMode << BLUR_SHIFT);

                // When all effect modes are disable(None), graphic uses default material.
                if (identifier == 0)
                {
                    graphic.material = null;
                    graphic.SetVerticesDirty();
                    return;
                }

                // Generate and cache new material by given identifier.
                if (!s_SharedMaterials[identifier])
                {
                    if (!s_SharedMaterials[0])
                    {
                        s_SharedMaterials[0] = new Material(shader);
                    }
                    Material mat = new Material(s_SharedMaterials[0]);

                    // Bits for tone effect.
                    int toneBits = identifier >> TONE_SHIFT;
                    mat.EnableKeyword(
                        TONE_NEGA == (toneBits & TONE_NEGA) ? "UI_TONE_NEGA"
                                                : TONE_SEPIA == (toneBits & TONE_SEPIA) ? "UI_TONE_SEPIA"
                                                : TONE_GRAYSCALE == (toneBits & TONE_GRAYSCALE) ? "UI_TONE_GRAYSCALE"
                                                : "UI_TONE_OFF"
                        );

                    // Bits for color effect.
                    int colorBits = identifier >> COLOR_SHIFT;
                    mat.EnableKeyword(
                        COLOR_SUB == (colorBits & COLOR_SUB) ? "UI_COLOR_SUB"
                                                : COLOR_ADD == (colorBits & COLOR_ADD) ? "UI_COLOR_ADD"
                                                : COLOR_SET == (colorBits & COLOR_SET) ? "UI_COLOR_SET"
                                                : "UI_COLOR_OFF"
                        );

                    // Bits for blur effect.
                    int blurBits = identifier >> BLUR_SHIFT;
                    mat.EnableKeyword(
                        BLUR_DETAIL == (blurBits & BLUR_DETAIL) ? "UI_BLUR_DETAIL"
                                                : BLUR_FAST == (blurBits & BLUR_FAST) ? "UI_BLUR_FAST"
                                                : "UI_BLUR_OFF"
                        );

                    mat.name += identifier.ToString();
                    s_SharedMaterials[identifier] = mat;
                }

                graphic.material = s_SharedMaterials[identifier];
            }
            graphic.SetVerticesDirty();
        }