Skip to content

Commit

Permalink
close #83; Refactoring to prepare v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Jun 14, 2018
1 parent 321a47b commit 481d4e8
Show file tree
Hide file tree
Showing 30 changed files with 669 additions and 456 deletions.
9 changes: 9 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/BlurMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Coffee.UIExtensions
{
/// <summary>
/// Blur effect mode.
/// </summary>
public enum BlurMode
{
None = 0,
FastBlur = 1,
MediumBlur = 2,
DetailBlur = 3,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/ColorMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Coffee.UIExtensions
{
/// <summary>
/// Color effect mode.
/// </summary>
public enum ColorMode
{
Multiply = 0,
Fill = 1,
Add = 2,
Subtract = 3,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/MaterialResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#if UNITY_EDITOR
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;

namespace Coffee.UIExtensions
{
public class MaterialResolver
{
static readonly StringBuilder s_StringBuilder = new StringBuilder ();

public static Material GetOrGenerateMaterialVariant (Shader shader, params object[] append)
{
if (!shader) {
return null;
}

string[] keywords = append.Where (x => 0 < (int)x)
.Select (x => x.ToString ().ToUpper ())
.ToArray ();
Material mat = GetMaterial (shader, append);
if (mat) {
if(!mat.shaderKeywords.OrderBy(x=>x).SequenceEqual(keywords.OrderBy(x=>x)))
{
mat.shaderKeywords = keywords;
EditorUtility.SetDirty (mat);
if (!Application.isPlaying)
{
EditorApplication.delayCall += AssetDatabase.SaveAssets;
}
}
return mat;
}

var variantName = GetVariantName (shader, append);
Debug.Log ("Generate material : " + variantName);
mat = new Material (shader);
mat.shaderKeywords = keywords;

mat.name = variantName;
mat.hideFlags |= HideFlags.NotEditable;

#if UIEFFECT_SEPARATE
string dir = Path.GetDirectoryName(GetDefaultMaterialPath (shader));
string materialPath = Path.Combine(Path.Combine(dir, "Separated"), mat.name + ".mat");

Directory.CreateDirectory (Path.GetDirectoryName (materialPath));
AssetDatabase.CreateAsset (mat, materialPath);
AssetDatabase.SaveAssets ();
#else
if (append.Cast<int> ().All (x => x == 0)) {
string materialPath = GetDefaultMaterialPath (shader);
Directory.CreateDirectory (Path.GetDirectoryName (materialPath));
AssetDatabase.CreateAsset (mat, materialPath);
AssetDatabase.SaveAssets ();
} else {
GetOrGenerateMaterialVariant (shader);
string materialPath = GetDefaultMaterialPath (shader);
mat.hideFlags |= HideFlags.HideInHierarchy;
AssetDatabase.AddObjectToAsset (mat, materialPath);
}
#endif
return mat;
}

public static Material GetMaterial (Shader shader, params object[] append)
{
string variantName = GetVariantName (shader, append);
return AssetDatabase.FindAssets ("t:Material " + Path.GetFileName (shader.name))
.Select (x => AssetDatabase.GUIDToAssetPath (x))
.SelectMany (x => AssetDatabase.LoadAllAssetsAtPath (x))
.OfType<Material> ()
.FirstOrDefault (x => x.name == variantName);
}

public static string GetDefaultMaterialPath (Shader shader)
{
var name = Path.GetFileName (shader.name);
return AssetDatabase.FindAssets ("t:Material " + name)
.Select (x => AssetDatabase.GUIDToAssetPath (x))
.FirstOrDefault (x => Path.GetFileNameWithoutExtension (x) == name)
?? ("Assets/" + name + ".mat");
}

public static string GetVariantName (Shader shader, params object[] append)
{
s_StringBuilder.Length = 0;

#if UIEFFECT_SEPARATE
s_StringBuilder.Append("[Separated] ");
#endif
s_StringBuilder.Append (Path.GetFileName (shader.name));
foreach (object mode in append.Where(x=>0<(int)x)) {
s_StringBuilder.Append ("-");
s_StringBuilder.Append (mode.ToString ());
}
return s_StringBuilder.ToString ();
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/ShadowStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Coffee.UIExtensions
{
/// <summary>
/// Shadow effect style.
/// </summary>
public enum ShadowStyle
{
None = 0,
Shadow,
Outline,
Outline8,
Shadow3,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Assets/Coffee/UIExtensions/UIEffect/Scripts/Common/ToneMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Coffee.UIExtensions
{
/// <summary>
/// Effect mode.
/// </summary>
public enum ToneMode
{
None = 0,
Grayscale = 1,
Sepia = 2,
Nega = 3,
Pixel = 4,
Mono = 5,
Cutoff = 6,
Hue = 7,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ public abstract class UIEffectBase : BaseMeshEffect, ISerializationCallbackRecei
/// </summary>
public Material effectMaterial { get { return m_EffectMaterial; } }

/// <summary>
/// Raises the validate event.
/// </summary>
protected override void OnValidate ()
{
base.OnValidate ();
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += () => UpdateMaterial(false);
#endif
}

/// <summary>
/// Raises the before serialize event.
/// </summary>
Expand All @@ -39,10 +50,41 @@ public virtual void OnBeforeSerialize()
public virtual void OnAfterDeserialize()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.delayCall += () => targetGraphic.material = m_EffectMaterial;
UnityEditor.EditorApplication.delayCall += () => UpdateMaterial(true);
#endif
}

#if UNITY_EDITOR
/// <summary>
/// Updates the material.
/// </summary>
/// <param name="ignoreInPlayMode">If set to <c>true</c> ignore in play mode.</param>
protected void UpdateMaterial(bool ignoreInPlayMode)
{
if(!this || ignoreInPlayMode && Application.isPlaying)
{
return;
}

var mat = GetMaterial();
if (m_EffectMaterial != mat || targetGraphic.material != mat)
{
targetGraphic.material = m_EffectMaterial = mat;
UnityEditor.EditorUtility.SetDirty(this);
UnityEditor.EditorUtility.SetDirty(targetGraphic);
}
}

/// <summary>
/// Gets the material.
/// </summary>
/// <returns>The material.</returns>
protected virtual Material GetMaterial()
{
return null;
}
#endif

/// <summary>
/// This function is called when the object becomes enabled and active.
/// </summary>
Expand Down
Loading

0 comments on commit 481d4e8

Please sign in to comment.