public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        CCGradient gradient = (CCGradient)target;

        if (r.width <= 1f)
        {
            // events, no drawing
            return;
        }
        if (r.width == 40f)
        {
            // draw the thumbnail
            gradient.WriteToTexture(0f, 1f, thumbTexture);
            thumbTexture.Apply();
            CCEditorUtility.DrawTexture(r, thumbTexture, previewMode);
            return;
        }

        // draw the preview
        if ((int)r.width != previewTexture.width)
        {
            previewTexture.Resize((int)r.width, 1);
        }
        gradient.WriteToTexture(previewFrom, previewTo, previewTexture);
        previewTexture.Apply();
        CCEditorUtility.DrawTexture(r, previewTexture, previewMode);
    }
Beispiel #2
0
    void OnEnable()
    {
        gradient = Selection.activeObject as CCGradient;

        minimum         = EditorPrefs.GetFloat(minimumKey);
        maximum         = EditorPrefs.GetFloat(maximumKey, 1f);
        pixels          = EditorPrefs.GetInt(pixelsKey, 16);
        bilinearPreview = EditorPrefs.GetBool(bilinearPreviewKey, true);

        texture            = new Texture2D(pixels, 1, TextureFormat.ARGB32, false);
        texture.hideFlags  = HideFlags.HideAndDontSave;
        texture.wrapMode   = TextureWrapMode.Clamp;
        texture.filterMode = bilinearPreview ? FilterMode.Bilinear : FilterMode.Point;

        if (gradient != null)
        {
            gradient.WriteToTexture(minimum, maximum, texture);
            texture.Apply();
        }
    }
Beispiel #3
0
    void OnGUI()
    {
        CCGradient oldGradient = gradient;

        gradient = (CCGradient)EditorGUILayout.ObjectField("Gradient", gradient, typeof(CCGradient), false);
        bool updateTexture = gradient != oldGradient;

        if (gradient == null)
        {
            return;
        }

        float oldValue = minimum;

        minimum = EditorGUILayout.FloatField("Minimum", minimum);
        if (minimum != oldValue)
        {
            EditorPrefs.SetFloat(minimumKey, minimum);
            updateTexture = true;
        }
        oldValue = maximum;
        maximum  = EditorGUILayout.FloatField("Maximum", maximum);
        if (maximum != oldValue)
        {
            EditorPrefs.SetFloat(maximumKey, maximum);
            updateTexture = true;
        }

        int oldPixels = pixels;

        pixels = Mathf.Max(1, EditorGUILayout.IntField("Pixels", pixels));
        if (pixels != oldPixels)
        {
            texture.Resize(pixels, 1);
            EditorPrefs.SetInt(pixelsKey, pixels);
            updateTexture = true;
        }

        bool oldToggle = bilinearPreview;

        bilinearPreview = EditorGUILayout.Toggle("Bilinear Preview", bilinearPreview);
        if (bilinearPreview != oldToggle)
        {
            EditorPrefs.SetBool(bilinearPreviewKey, bilinearPreview);
            texture.filterMode = bilinearPreview ? FilterMode.Bilinear : FilterMode.Point;
        }

        if (updateTexture)
        {
            gradient.WriteToTexture(minimum, maximum, texture);
            texture.Apply();
        }

        if (GUILayout.Button("Export PNG file"))
        {
            string filePath = CCEditorUtility.SaveFilePanelInSameFolder("Save Color Gradient", gradient.name + " map", "png", gradient);
            if (filePath.Length > 0)
            {
                File.WriteAllBytes(filePath, texture.EncodeToPNG());
                AssetDatabase.Refresh();
                Close();
            }
        }
        CCEditorUtility.DrawTexture(new Rect(2f, 130f, position.width - 4f, position.height - 132f), texture);
    }