示例#1
0
    // static MapPreview mapPrev = null;

    public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
    {
        Event            guiEvent   = Event.current;
        MaterialGradient grad       = (MaterialGradient)fieldInfo.GetValue(prop.serializedObject.targetObject);
        float            labelWidth = GUI.skin.label.CalcSize(label).x + 5;
        Rect             textRect   = new Rect(pos.x + labelWidth, pos.y, pos.width - labelWidth, pos.height);

        // if (!mapPrev) mapPrev = GameObject.Find("MapPreview").GetComponent<MapPreview>();

        if (guiEvent.type == EventType.Repaint)
        {
            GUIStyle gradStyle = new GUIStyle();

            GUI.Label(pos, label);
            // There is an issue with printing the texture
            gradStyle.normal.background = grad.GetTexture((int)pos.width);
            GUI.Label(textRect, GUIContent.none, gradStyle);

            // if (mapPrev && mapPrev.autoUpdate) mapPrev.DrawMapInEditorGrad();
        }
        else if (guiEvent.type == EventType.MouseDown && guiEvent.button == 0 && textRect.Contains(guiEvent.mousePosition))
        {
            // Open the window when clicked on
            MaterialGradientEditor window = EditorWindow.GetWindow <MaterialGradientEditor>();

            window.Gradient = grad;
        }
    }
    // Need a way to update both the min and max height through keys
    private void Draw()
    {
        gradPrevRect = new Rect(borderSize, borderSize, position.width - borderSize * 2, 25);
        GUI.DrawTexture(gradPrevRect, gradient.GetTexture((int)gradPrevRect.width));
        matRects = new Rect[gradient.NumMats * 2];
        for (int i = 0; i < gradient.NumMats * 2; i++)
        {
            MaterialGradient.MaterialLevel mat = gradient.GetMatLevel(Mathf.FloorToInt(i * 0.5f));
            Rect matRect = new Rect(gradPrevRect.x + gradPrevRect.width * ((i % 2 == 0) ? mat.MinHeight : mat.MaxHeight) - keyWidth / 2.0f, gradPrevRect.yMax + borderSize, keyWidth, keyHeight);

            if (i == selectedKeyIndex)
            {
                EditorGUI.DrawRect(new Rect(matRect.x - 2, matRect.y - 2, matRect.width + 4, matRect.height + 4), Color.black);
            }
            EditorGUI.DrawRect(matRect, mat.Tint);
            matRects[i] = matRect;
        }

        Rect settingsRect = new Rect(borderSize, matRects[0].yMax + borderSize, position.width - borderSize * 2, position.height - borderSize);

        GUILayout.BeginArea(settingsRect);

        EditorGUI.BeginChangeCheck();

        Texture2D newText             = (Texture2D)EditorGUILayout.ObjectField("Texture", gradient.GetMatLevel(MatIndex).Texture, typeof(Texture2D), false);
        Color     newTint             = EditorGUILayout.ColorField(gradient.GetMatLevel(MatIndex).Tint);
        float     newTintStrength     = EditorGUILayout.Slider("Tint Strength", gradient.GetMatLevel(MatIndex).TintStrength, 0.0f, 1.0f);
        float     newMinSlope         = EditorGUILayout.Slider("Min Slope", gradient.GetMatLevel(MatIndex).MinSlope, 0.0f, 90.0f);
        float     newMaxSlope         = EditorGUILayout.Slider("Max Slope", gradient.GetMatLevel(MatIndex).MaxSlope, 0.0f, 90.0f);
        float     newBlendStrength    = EditorGUILayout.Slider("Blend Strength", gradient.GetMatLevel(MatIndex).BlendStrength, 0.0f, 1.0f);
        Vector2   newTileOffset       = EditorGUILayout.Vector2Field("Tex Offset", gradient.GetMatLevel(MatIndex).TileOffset);
        Vector2   newTileScale        = EditorGUILayout.Vector2Field("Tex Scale", gradient.GetMatLevel(MatIndex).TileScale);
        Vector2   newSplatNoiseVScale = EditorGUILayout.Vector2Field("Blend Noise Vector Scale", gradient.GetMatLevel(MatIndex).SplatNoiseVScale);
        float     newSplatNoiseScaler = EditorGUILayout.FloatField("Blend Noise Scaler", gradient.GetMatLevel(MatIndex).SplatNoiseScaler);

        if (EditorGUI.EndChangeCheck())
        {
            gradient.UpdateMatTexture(MatIndex, newText);
            gradient.UpdateMatTint(MatIndex, newTint);
            gradient.UpdateMatTintStrength(MatIndex, newTintStrength);
            gradient.UpdateMatMinSlope(MatIndex, newMinSlope);
            gradient.UpdateMatMaxSlope(MatIndex, newMaxSlope);
            gradient.UpdateMatBlendStrength(MatIndex, newBlendStrength);
            gradient.UpdateMatTileOffset(MatIndex, newTileOffset);
            gradient.UpdateMatTileScale(MatIndex, newTileScale);
            gradient.UpdateMatSplatNoiseVScale(MatIndex, newSplatNoiseVScale);
            gradient.UpdateMatSplatNoiseScaler(MatIndex, newSplatNoiseScaler);
        }
        gradient.bRandomizeTint = EditorGUILayout.Toggle("Randomize Tint", gradient.bRandomizeTint);

        GUILayout.EndArea();
    }