LayaEffectGUI.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #if UNITY_EDITOR
  2. using System;
  3. using UnityEngine;
  4. using UnityEditor;
  5. class LayaEffectGUI : ShaderGUI
  6. {
  7. public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
  8. {
  9. material.shader = newShader;
  10. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  11. }
  12. public enum RenderMode
  13. {
  14. Additive = 0,
  15. AlphaBlended = 1
  16. }
  17. MaterialProperty renderMode = null;
  18. MaterialProperty diffuseTexture = null;
  19. MaterialProperty diffuseColor = null;
  20. MaterialEditor m_MaterialEditor;
  21. public void FindProperties(MaterialProperty[] props)
  22. {
  23. diffuseTexture = FindProperty("_MainTex", props);
  24. diffuseColor = FindProperty("_TintColor", props);
  25. renderMode = FindProperty("_Mode", props);
  26. }
  27. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  28. {
  29. // render the default gui
  30. FindProperties(props);
  31. m_MaterialEditor = materialEditor;
  32. Material material = materialEditor.target as Material;
  33. //if (m_FirstTimeApply)
  34. //{
  35. // onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  36. // m_FirstTimeApply = false;
  37. //}
  38. ShaderPropertiesGUI(material);
  39. }
  40. public void ShaderPropertiesGUI(Material material,bool enablemode = false)
  41. {
  42. // Use default labelWidth
  43. EditorGUIUtility.labelWidth = 0f;
  44. // Detect any changes to the material
  45. EditorGUI.BeginChangeCheck();
  46. {
  47. //renderMode
  48. GUILayout.BeginHorizontal();
  49. GUILayout.Label(Styles.renderModeText, GUILayout.Width(120));
  50. var mode = (RenderMode)renderMode.floatValue;
  51. mode = (RenderMode)EditorGUILayout.Popup((int)mode, Styles.renderModeNames);
  52. GUILayout.EndHorizontal();
  53. //Diffuse
  54. m_MaterialEditor.ShaderProperty(diffuseColor, Styles.MainColorText, MaterialEditor.kMiniTextureFieldLabelIndentLevel);
  55. //Diffuse
  56. m_MaterialEditor.TexturePropertySingleLine(Styles.DiffuseTextureText, diffuseTexture);
  57. //scaleAndOffset
  58. m_MaterialEditor.TextureScaleOffsetProperty(diffuseTexture);
  59. if (EditorGUI.EndChangeCheck()||enablemode)
  60. {
  61. m_MaterialEditor.RegisterPropertyChangeUndo("Rendering Mode");
  62. //renderMode
  63. renderMode.floatValue = (float)mode;
  64. onChangeRender(material, (RenderMode)material.GetFloat("_Mode"));
  65. }
  66. }
  67. m_MaterialEditor.RenderQueueField();
  68. }
  69. public void onChangeRender(Material material, RenderMode mode)
  70. {
  71. switch (mode)
  72. {
  73. case RenderMode.Additive:
  74. material.SetInt("_Mode", 0);
  75. material.SetInt("_AlphaTest", 0);
  76. material.SetInt("_AlphaBlend", 1);
  77. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  78. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.One);
  79. material.SetInt("_ZWrite", 0);
  80. material.SetInt("_ZTest", 4);
  81. material.DisableKeyword("_ALPHATEST_ON");
  82. material.EnableKeyword("_ALPHABLEND_ON");
  83. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  84. material.EnableKeyword("ADDTIVEFOG");
  85. break;
  86. case RenderMode.AlphaBlended:
  87. material.SetInt("_Mode", 1);
  88. material.SetInt("_AlphaTest", 0);
  89. material.SetInt("_AlphaBlend", 1);
  90. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  91. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  92. material.SetInt("_ZWrite", 0);
  93. material.SetInt("_ZTest", 4);
  94. material.DisableKeyword("_ALPHATEST_ON");
  95. material.EnableKeyword("_ALPHABLEND_ON");
  96. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  97. material.DisableKeyword("ADDTIVEFOG");
  98. break;
  99. default:
  100. material.SetInt("_Mode", 1);
  101. material.SetInt("_AlphaTest", 0);
  102. material.SetInt("_AlphaBlend", 1);
  103. material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
  104. material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
  105. material.SetInt("_ZWrite", 0);
  106. material.SetInt("_ZTest", 4);
  107. material.DisableKeyword("_ALPHATEST_ON");
  108. material.EnableKeyword("_ALPHABLEND_ON");
  109. material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent;
  110. material.EnableKeyword("ADDTIVEFOG");
  111. break;
  112. }
  113. }
  114. public static class Styles
  115. {
  116. public static GUIStyle optionsButton = "PaneOptions";
  117. public static GUIContent uvSetLabel = new GUIContent("UV Set");
  118. public static GUIContent[] uvSetOptions = new GUIContent[] { new GUIContent("UV channel 0"), new GUIContent("UV channel 1") };
  119. public static string emptyTootip = "";
  120. public static GUIContent MainColorText = new GUIContent("Color", "Color");
  121. public static GUIContent DiffuseTextureText = new GUIContent("Texture", "Texture");
  122. public static readonly string[] renderModeNames = Enum.GetNames(typeof(RenderMode));
  123. public static GUIContent renderModeText = new GUIContent("RenderMode", "RenderMode");
  124. }
  125. }
  126. #endif