private void GenerateAndSaveTexture(string path) { if (string.IsNullOrEmpty(path)) { return; } FunDream_GradientManager.SaveGradientTexture(mGradient, textureWidth, path); }
void InitEditMode(Texture2D texture, Material[] materials) { this.textureEdited = false; this.editMode = true; this.linkedTexture = texture; this.linkedImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)); this.linkedMaterials = materials; FunDream_GradientManager.SetGradientFromUserData(this.linkedImporter.userData, this.mGradient); this.UpdateGradientPreview(); }
private void SaveEditedTexture() { if (textureEdited) { //Save data to file System.IO.File.WriteAllBytes(Application.dataPath + AssetDatabase.GetAssetPath(linkedTexture).Substring(6), linkedTexture.EncodeToPNG()); //Update linked texture userData linkedImporter.userData = FunDream_GradientManager.GradientToUserData(mGradient); } textureEdited = false; }
void OnGUI() { EditorGUILayout.BeginHorizontal(); FunDream_GUI.HeaderBig(editMode ? "TCP 2 - RAMP EDITOR" : "TCP 2 - RAMP GENERATOR"); FunDream_GUI.HelpButton("Ramp Generator"); EditorGUILayout.EndHorizontal(); FunDream_GUI.Separator(); if (editMode) { string msg = "This will affect <b>all materials</b> that use this texture!" + (this.editModeFromMaterial ? "\n\nSave as a new texture first if you want to affect this material only." : "\n\nSave as a new texture if you want to keep the original ramp."); EditorGUILayout.LabelField(GUIContent.none, new GUIContent(msg, FunDream_GUI.GetHelpBoxIcon(MessageType.Warning)), FunDream_GUI.HelpBoxRichTextStyle); var rect = EditorGUILayout.GetControlRect(GUILayout.Height(16f)); var lw = EditorGUIUtility.labelWidth; EditorGUIUtility.labelWidth = 50f; var enabled = GUI.enabled; GUI.enabled = false; EditorGUI.ObjectField(rect, "Editing: ", linkedTexture, typeof(Texture2D), false); EditorGUIUtility.labelWidth = lw; GUI.enabled = enabled; } GUILayout.Label("Click on the gradient to edit it:"); SerializedObject so = new SerializedObject(this); SerializedProperty sp = so.FindProperty("mGradient"); EditorGUILayout.PropertyField(sp, GUIContent.none); if (!editMode) { textureWidth = EditorGUILayout.IntField("TEXTURE SIZE:", textureWidth); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("64", EditorStyles.miniButtonLeft)) { textureWidth = 64; } if (GUILayout.Button("128", EditorStyles.miniButtonMid)) { textureWidth = 128; } if (GUILayout.Button("256", EditorStyles.miniButtonMid)) { textureWidth = 256; } if (GUILayout.Button("512", EditorStyles.miniButtonMid)) { textureWidth = 512; } if (GUILayout.Button("1024", EditorStyles.miniButtonRight)) { textureWidth = 1024; } EditorGUILayout.EndHorizontal(); } if (GUI.changed) { so.ApplyModifiedProperties(); mGradient.alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) }; if (editMode) { textureEdited = true; //Update linked texture var pixels = FunDream_GradientManager.GetPixelsFromGradient(mGradient, linkedTexture.width); linkedTexture.SetPixels(pixels); linkedTexture.Apply(true, false); } } GUILayout.Space(8f); EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (editMode) { if (GUILayout.Button("Discard", GUILayout.Width(90f), GUILayout.Height(20f))) { DiscardEditedTexture(); if (this.editModeFromMaterial) { this.Close(); } else { FunDream_RampGenerator.OpenTool(); } } if (GUILayout.Button("Apply", GUILayout.Width(90f), GUILayout.Height(20f))) { SaveEditedTexture(); if (this.editModeFromMaterial) { this.Close(); } else { FunDream_RampGenerator.OpenTool(); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); } bool saveButton = false; if (editMode) { saveButton = GUILayout.Button("Save as...", EditorStyles.miniButton, GUILayout.Width(120f), GUILayout.Height(16f)); } else { saveButton = GUILayout.Button("GENERATE", GUILayout.Width(120f), GUILayout.Height(34f)); } if (saveButton) { string path = EditorUtility.SaveFilePanel("Save Generated Ramp", FunDream_GradientManager.LAST_SAVE_PATH, editMode ? linkedTexture.name : "FunDream__CustomRamp", "png"); if (!string.IsNullOrEmpty(path)) { FunDream_GradientManager.LAST_SAVE_PATH = System.IO.Path.GetDirectoryName(path); var projectPath = path.Replace(Application.dataPath, "Assets"); GenerateAndSaveTexture(projectPath); if (editMode) { var newtexture = AssetDatabase.LoadAssetAtPath <Texture2D>(projectPath); if (newtexture != null) { foreach (var mat in linkedMaterials) { mat.SetTexture("_Ramp", newtexture); EditorUtility.SetDirty(mat); } } //Reinitialize edit mode InitEditMode(newtexture, linkedMaterials); } } } EditorGUILayout.EndHorizontal(); if (!editMode) { EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Load Texture", EditorStyles.miniButton, GUILayout.Width(120f))) { LoadTexture(); } EditorGUILayout.EndHorizontal(); } }
public override void OnGUI(Rect position, MaterialProperty prop, string label, MaterialEditor editor) { //Find default ramp texture if needed if (prop.textureValue == null) { if (DefaultRampTexture == null && !DefaultTextureSearched) { DefaultRampTexture = TryFindDefaultRampTexture(); DefaultTextureSearched = true; } if (DefaultRampTexture != null) { prop.textureValue = DefaultRampTexture; } } float indent = EditorGUI.indentLevel * 16; //Label var labelRect = position; labelRect.height = 16f; float space = labelRect.height + 4; position.y += space - 3; position.height -= space; EditorGUI.PrefixLabel(labelRect, new GUIContent(label)); //Texture object field position.height = 16f; var newTexture = (Texture)EditorGUI.ObjectField(position, prop.textureValue, typeof(Texture2D), false); if (newTexture != prop.textureValue) { prop.textureValue = newTexture; assetImporter = null; } //Preview texture override (larger preview, hides texture name) var previewRect = new Rect(position.x + indent - 1, position.y + 1, position.width - indent - 18, position.height - 2); if (prop.hasMixedValue) { var col = GUI.color; GUI.color = EditorGUIUtility.isProSkin ? new Color(.25f, .25f, .25f) : new Color(.85f, .85f, .85f); EditorGUI.DrawPreviewTexture(previewRect, Texture2D.whiteTexture); GUI.color = col; GUI.Label(previewRect, "―"); } else if (prop.textureValue != null) { EditorGUI.DrawPreviewTexture(previewRect, prop.textureValue); } if (prop.textureValue != null) { assetImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(prop.textureValue)); } //Edit button var buttonRect = labelRect; buttonRect.width = 70; buttonRect.x = labelRect.x + (labelRect.width - 150); if (GUI.Button(buttonRect, "Create New", EditorStyles.miniButtonLeft)) { string lastSavePath = FunDream_GradientManager.LAST_SAVE_PATH; if (!lastSavePath.Contains(Application.dataPath)) { lastSavePath = Application.dataPath; } string path = EditorUtility.SaveFilePanel("Create New Ramp Texture", lastSavePath, "FunDream__CustomRamp", "png"); if (!string.IsNullOrEmpty(path)) { FunDream_GradientManager.LAST_SAVE_PATH = System.IO.Path.GetDirectoryName(path); //Create texture and save PNG var projectPath = path.Replace(Application.dataPath, "Assets"); FunDream_GradientManager.CreateAndSaveNewGradientTexture(256, projectPath); //Load created texture var texture = AssetDatabase.LoadAssetAtPath <Texture2D>(projectPath); assetImporter = AssetImporter.GetAtPath(projectPath); //Assign to material(s) prop.textureValue = texture; //Open for editing FunDream_RampGenerator.OpenForEditing(texture, editor.targets, true); } } buttonRect.x += buttonRect.width; buttonRect.width = 80; bool enabled = GUI.enabled; GUI.enabled = (assetImporter != null) && assetImporter.userData.StartsWith("GRADIENT") && !prop.hasMixedValue; if (GUI.Button(buttonRect, GUI.enabled ? editButtonLabel : editButtonDisabledLabel, EditorStyles.miniButtonRight)) { FunDream_RampGenerator.OpenForEditing((Texture2D)prop.textureValue, editor.targets, true); } GUI.enabled = enabled; }