static private TCP2_RampGenerator GetWindowTCP2() { TCP2_RampGenerator window = EditorWindow.GetWindow <TCP2_RampGenerator>(true, "TCP2 : Ramp Generator", true); window.minSize = new Vector2(352f, 132f); window.maxSize = new Vector2(352f, 132f); return(window); }
static private TCP2_RampGenerator GetWindowTCP2() { TCP2_RampGenerator window = GetWindow <TCP2_RampGenerator>(true, "TCP2 : Ramp Generator", true); window.editMode = false; window.linkedTexture = null; window.minSize = new Vector2(352f, 168f); window.maxSize = new Vector2(352f, 168f); return(window); }
static public void OpenForEditing(Texture2D texture, Object[] materials, bool openedFromMaterial) { TCP2_RampGenerator window = GetWindow <TCP2_RampGenerator>(true, "TCP2 : Ramp Generator", true); window.minSize = new Vector2(352f, 194f); window.maxSize = new Vector2(352f, 194f); var matList = new List <Material>(); if (materials != null) { foreach (var o in materials) { if (o is Material) { matList.Add(o as Material); } } } window.editModeFromMaterial = openedFromMaterial; window.InitEditMode(texture, matList.ToArray()); }
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 path = EditorUtility.SaveFilePanel("Create New Ramp Texture", TCP2_GradientManager.LAST_SAVE_PATH, "TCP2_CustomRamp", "png"); if (!string.IsNullOrEmpty(path)) { TCP2_GradientManager.LAST_SAVE_PATH = System.IO.Path.GetDirectoryName(path); //Create texture and save PNG var projectPath = path.Replace(Application.dataPath, "Assets"); TCP2_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 TCP2_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)) { TCP2_RampGenerator.OpenForEditing((Texture2D)prop.textureValue, editor.targets, true); } GUI.enabled = enabled; }
public static void RampProperty(MaterialProperty prop, string label, MaterialEditor editor, Gradient gradient, AssetImporter assetImporter, string defaultFileName = "JTRP_RampMap") { if (_iconAdd == null || _iconEdit == null) { _iconAdd = EditorGUIUtility.IconContent("d_Toolbar Plus"); _iconEdit = EditorGUIUtility.IconContent("editicon.sml"); } //Label var position = EditorGUILayout.GetControlRect(); var labelRect = position; labelRect.height = EditorGUIUtility.singleLineHeight; var space = labelRect.height + 4; position.y += space - 3; position.height -= space; EditorGUI.PrefixLabel(labelRect, new GUIContent(label)); //Texture object field var w = EditorGUIUtility.labelWidth; var indentLevel = EditorGUI.indentLevel; editor.SetDefaultGUIWidths(); var buttonRect = MaterialEditor.GetRectAfterLabelWidth(labelRect); EditorGUIUtility.labelWidth = 0; EditorGUI.indentLevel = 0; var textureRect = MaterialEditor.GetRectAfterLabelWidth(labelRect); textureRect.xMax -= buttonRect.width; var newTexture = (Texture)EditorGUI.ObjectField(textureRect, prop.textureValue, typeof(Texture2D), false); EditorGUIUtility.labelWidth = w; EditorGUI.indentLevel = indentLevel; if (newTexture != prop.textureValue) { prop.textureValue = newTexture; assetImporter = null; } //Preview texture override (larger preview, hides texture name) var previewRect = new Rect(textureRect.x + 1, textureRect.y + 1, textureRect.width - 19, textureRect.height - 2); if (prop.hasMixedValue) { EditorGUI.DrawPreviewTexture(previewRect, Texture2D.grayTexture); GUI.Label(new Rect(previewRect.x + previewRect.width * 0.5f - 10, previewRect.y, previewRect.width * 0.5f, previewRect.height), "―"); } else if (prop.textureValue != null) EditorGUI.DrawPreviewTexture(previewRect, prop.textureValue); if (prop.textureValue != null) { assetImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(prop.textureValue)); } var buttonRectL = new Rect(buttonRect.x, buttonRect.y, buttonRect.width * 0.5f, buttonRect.height); var buttonRectR = new Rect(buttonRectL.xMax, buttonRect.y, buttonRect.width * 0.5f, buttonRect.height); bool needCreat = false; if (GUI.Button(buttonRectL, _iconEdit)) { if ((assetImporter != null) && (assetImporter.userData.StartsWith("GRADIENT") || assetImporter.userData.StartsWith("gradient:")) && !prop.hasMixedValue) { TCP2_RampGenerator.OpenForEditing((Texture2D)prop.textureValue, editor.targets, true, false); } else { needCreat = true; } } if (GUI.Button(buttonRectR, _iconAdd) || needCreat) { var lastSavePath = GradientManager.LAST_SAVE_PATH; if (!lastSavePath.Contains(Application.dataPath)) lastSavePath = Application.dataPath; var path = EditorUtility.SaveFilePanel("Create New Ramp Texture", lastSavePath, defaultFileName, "png"); if (!string.IsNullOrEmpty(path)) { bool overwriteExistingFile = File.Exists(path); GradientManager.LAST_SAVE_PATH = Path.GetDirectoryName(path); //Create texture and save PNG var projectPath = path.Replace(Application.dataPath, "Assets"); GradientManager.CreateAndSaveNewGradientTexture(256, projectPath); //Load created texture var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(projectPath); assetImporter = AssetImporter.GetAtPath(projectPath); //Assign to material(s) foreach (var item in prop.targets) { ((Material)item).SetTexture(prop.name, texture); } //Open for editing TCP2_RampGenerator.OpenForEditing(texture, editor.targets, true, !overwriteExistingFile); } } }
void OnGUI() { EditorGUILayout.BeginHorizontal(); TCP2_GUI.HeaderBig(editMode ? "TCP 2 - RAMP EDITOR" : "TCP 2 - RAMP GENERATOR"); TCP2_GUI.HelpButton("Ramp Generator"); EditorGUILayout.EndHorizontal(); TCP2_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, TCP2_GUI.GetHelpBoxIcon(MessageType.Warning)), TCP2_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 = TCP2_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 { TCP2_RampGenerator.OpenTool(); } } if (GUILayout.Button("Apply", GUILayout.Width(90f), GUILayout.Height(20f))) { SaveEditedTexture(); if (this.editModeFromMaterial) { this.Close(); } else { TCP2_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", TCP2_GradientManager.LAST_SAVE_PATH, editMode ? linkedTexture.name : "TCP2_CustomRamp", "png"); if (!string.IsNullOrEmpty(path)) { TCP2_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(); } }