private static void CheckToSave() { MatEditData lData = GetMatEditData(focusMaterial); foreach (KeyValuePair <string, Texture2D> tex in lData.unsavedTextures) { if (lData.generatedTextures.ContainsKey(tex.Key)) { Texture2D oldTexture = lData.generatedTextures[tex.Key]; oldTexture.SetPixels(tex.Value.GetPixels()); EditorUtility.SetDirty(oldTexture); } else { tex.Value.name = tex.Key; lData.generatedTextures.Add(tex.Key, tex.Value); AssetDatabase.AddObjectToAsset(tex.Value, focusMaterial); } focusMaterial.SetTexture(tex.Key, lData.generatedTextures[tex.Key]); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } lData.unsavedTextures.Clear(); EditorUtility.SetDirty(lData); markedForSave = false; Selection.selectionChanged -= CheckToSave; }
private static MatEditData GetMatEditData(Material material) { string lMaterialPath = AssetDatabase.GetAssetPath(material); MatEditData lData = AssetDatabase.LoadAssetAtPath <MatEditData>(lMaterialPath); if (lData == null) { lData = ScriptableObject.CreateInstance(typeof(MatEditData)) as MatEditData; lData.name = "MatEditData"; AssetDatabase.AddObjectToAsset(lData, material); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } return(lData); }
public static void GradientField(GUIContent content, string property, int quality, Material material, bool debug = false) { MatEditData lData = GetMatEditData(material); Gradient gradient = new Gradient(); if (lData.gradients.ContainsKey(property)) { gradient = lData.gradients[property]; } else { lData.gradients.Add(property, gradient); } EditorGUI.BeginChangeCheck(); MethodInfo method = typeof(EditorGUILayout).GetMethod("GradientField", BindingFlags.Static | BindingFlags.NonPublic, null, new System.Type[] { typeof(GUIContent), typeof(Gradient), typeof(GUILayoutOption[]) }, null); if (method != null) { gradient = (Gradient)method.Invoke(null, new object[] { content, gradient, new GUILayoutOption[] { } }); } bool lEdited = EditorGUI.EndChangeCheck(); if (lEdited) { lData.gradients[property] = gradient; EditorUtility.SetDirty(lData); Texture2D mainTexture = GradientToTexture(gradient, quality, debug); if (lData.unsavedTextures.ContainsKey(property)) { lData.unsavedTextures[property] = mainTexture; } else { lData.unsavedTextures.Add(property, mainTexture); } material.SetTexture(property, mainTexture); } MarkForSave(material); }
public static bool BeginToggleGroup(GUIContent content, string toggleID, Material material, GroupStyles style = GroupStyles.Main, bool spacing = false, bool writeToShader = false) { MatEditData lData = GetMatEditData(material); if (!writeToShader) { if (!lData.toggles.ContainsKey(toggleID)) { lData.toggles.Add(toggleID, false); } } EditorGUILayout.BeginVertical(groupStyles[(int)style]); bool toggle = false; if (writeToShader) { toggle = material.GetInt(toggleID) == 1; } else { toggle = lData.toggles[toggleID]; } toggle = EditorGUILayout.BeginToggleGroup(content, toggle); EditorGUILayout.EndToggleGroup(); if (writeToShader) { material.SetInt(toggleID, toggle ? 1 : 0); } else { lData.toggles[toggleID] = toggle; } if (spacing) { EditorGUILayout.Space(); } return(toggle); }
public static bool BeginFoldGroup(GUIContent content, string toggleID, Material material, GroupStyles style = GroupStyles.Main, bool spacing = false, bool writeToShader = false) { MatEditData lData = GetMatEditData(material); if (!writeToShader) { if (!lData.toggles.ContainsKey(toggleID)) { lData.toggles.Add(toggleID, false); } } EditorGUILayout.BeginVertical(groupStyles[(int)style]); if (GUILayout.Button(content, EditorStyles.boldLabel)) { if (writeToShader) { material.SetInt(toggleID, scopeMaterial.GetInt(toggleID) == 1 ? 0 : 1); } else { lData.toggles[toggleID] = !lData.toggles[toggleID]; } } if (spacing) { EditorGUILayout.Space(); } if (writeToShader) { return(material.GetInt(toggleID) == 1); } else { return(lData.toggles[toggleID]); } }
public static void AnimationCurveField(GUIContent content, string property, int quality, Material material, bool debug = false) { MatEditData lData = GetMatEditData(material); AnimationCurve curve = new AnimationCurve(); if (lData.animationCurves.ContainsKey(property)) { curve = lData.animationCurves[property]; } else { lData.animationCurves.Add(property, curve); } EditorGUI.BeginChangeCheck(); curve = EditorGUILayout.CurveField(content, curve); bool lEdited = EditorGUI.EndChangeCheck(); lData.animationCurves[property] = curve; EditorUtility.SetDirty(lData); if (lEdited) { Texture2D mainTexture = AnimationCurveToTexture(curve, quality, debug); if (lData.unsavedTextures.ContainsKey(property)) { lData.unsavedTextures[property] = mainTexture; } else { lData.unsavedTextures.Add(property, mainTexture); } material.SetTexture(property, mainTexture); } MarkForSave(material); }