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();
        }
    }
Example #2
0
 public override void OnGUI(Rect position, MaterialProperty prop, string label, MaterialEditor editor)
 {
     position.y      += 4;
     position.height -= 4;
     FunDream_GUI.Separator(position);
 }