TMPro_EditorShaderUtilities.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Linq;
  4. using System.Collections;
  5. namespace TMPro.EditorUtilities
  6. {
  7. public static class EditorShaderUtilities
  8. {
  9. /// <summary>
  10. /// Copy Shader properties from source to destination material.
  11. /// </summary>
  12. /// <param name="source"></param>
  13. /// <returns></returns>
  14. public static void CopyMaterialProperties(Material source, Material destination)
  15. {
  16. MaterialProperty[] source_prop = MaterialEditor.GetMaterialProperties(new Material[] { source });
  17. for (int i = 0; i < source_prop.Length; i++)
  18. {
  19. int property_ID = Shader.PropertyToID(source_prop[i].name);
  20. if (destination.HasProperty(property_ID))
  21. {
  22. //Debug.Log(source_prop[i].name + " Type:" + ShaderUtil.GetPropertyType(source.shader, i));
  23. switch (ShaderUtil.GetPropertyType(source.shader, i))
  24. {
  25. case ShaderUtil.ShaderPropertyType.Color:
  26. destination.SetColor(property_ID, source.GetColor(property_ID));
  27. break;
  28. case ShaderUtil.ShaderPropertyType.Float:
  29. destination.SetFloat(property_ID, source.GetFloat(property_ID));
  30. break;
  31. case ShaderUtil.ShaderPropertyType.Range:
  32. destination.SetFloat(property_ID, source.GetFloat(property_ID));
  33. break;
  34. case ShaderUtil.ShaderPropertyType.TexEnv:
  35. destination.SetTexture(property_ID, source.GetTexture(property_ID));
  36. break;
  37. case ShaderUtil.ShaderPropertyType.Vector:
  38. destination.SetVector(property_ID, source.GetVector(property_ID));
  39. break;
  40. }
  41. }
  42. }
  43. }
  44. }
  45. }