public static void SetMaterialPropertyValue(MaterialProperty p, Material[] materials, string value) { if (p.type == MaterialProperty.PropType.Texture) { Texture tex = AssetDatabase.LoadAssetAtPath <Texture>(value); if (tex != null) { foreach (Material m in materials) { m.SetTexture(p.name, tex); } } } else if (p.type == MaterialProperty.PropType.Float || p.type == MaterialProperty.PropType.Range) { float f_value; if (float.TryParse(value, out f_value)) { p.floatValue = f_value; string[] drawer = ShaderHelper.GetDrawer(p); if (drawer != null && drawer.Length > 1 && drawer[0] == "Toggle" && drawer[1] != "__") { MaterialHelper.ToggleKeyword(p, drawer[1], f_value == 1); } } } else if (p.type == MaterialProperty.PropType.Vector) { string[] xyzw = value.Split(",".ToCharArray()); Vector4 vector = new Vector4(float.Parse(xyzw[0]), float.Parse(xyzw[1]), float.Parse(xyzw[2]), float.Parse(xyzw[3])); foreach (Material m in materials) { m.SetVector(p.name, vector); } } else if (p.type == MaterialProperty.PropType.Color) { Color col = Converter.stringToColor(value); foreach (Material m in materials) { m.SetColor(p.name, col); } } }
public void applyPreset(string presetName, MaterialProperty[] props, Material[] materials) { ThryEditor.addUndo(Locale.editor.Get("apply_preset") + ": " + presetName); List <string[]> sets; if (presets.TryGetValue(presetName, out sets)) { foreach (string[] set in sets) { MaterialProperty p = ThryEditor.FindProperty(props, set[0]); if (p != null) { if (p.type == MaterialProperty.PropType.Texture) { Texture tex = AssetDatabase.LoadAssetAtPath <Texture>(set[1]); if (tex != null) { foreach (Material m in materials) { m.SetTexture(Shader.PropertyToID(set[0]), tex); } } } else if (p.type == MaterialProperty.PropType.Float || p.type == MaterialProperty.PropType.Range) { float value; if (float.TryParse(set[1], out value)) { foreach (Material m in materials) { m.SetFloat(Shader.PropertyToID(set[0]), value); } } } else if (p.type == MaterialProperty.PropType.Vector) { string[] xyzw = set[1].Split(",".ToCharArray()); Vector4 vector = new Vector4(float.Parse(xyzw[0]), float.Parse(xyzw[1]), float.Parse(xyzw[2]), float.Parse(xyzw[3])); foreach (Material m in materials) { m.SetVector(Shader.PropertyToID(set[0]), vector); } } else if (p.type == MaterialProperty.PropType.Color) { Color col = Converter.stringToColor(set[1]); foreach (Material m in materials) { m.SetColor(Shader.PropertyToID(set[0]), col); } } } else if (set[0] == "render_queue") { int q = 0; Debug.Log(set[0] + "," + set[1]); if (int.TryParse(set[1], out q)) { foreach (Material m in materials) { m.renderQueue = q; } } } } } ThryEditor.loadValuesFromMaterial(); ThryEditor.repaint(); }