简体   繁体   中英

How can I return a color in a get method using a bool?

The main goal is to be able to use either the custom or default colors.

using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;

// Implementation from UnityEditor.Graphs.GraphGUI
public static class GraphBackground
{
    public static Color CustomkGridMinorColorDark;
    public static Color CustomkGridMajorColorDark;
    public static Color CustomkGridMinorColorLight;
    public static Color CustomkGridMajorColorLight;

    public static bool defaultColors = true;

    private static readonly Color kGridMinorColorDark = new Color(0f, 0f, 0f, 0.18f);
    private static readonly Color kGridMajorColorDark = new Color(0f, 0f, 0f, 0.28f);
    private static readonly Color kGridMinorColorLight = new Color(0f, 0f, 0f, 0.1f);
    private static readonly Color kGridMajorColorLight = new Color(0f, 0f, 0f, 0.15f);

    private static Color gridMinorColor
    {
        get
        {
            if (EditorGUIUtility.isProSkin)
                return kGridMinorColorDark;
            else
                return kGridMinorColorLight;
        }
    }

    private static Color gridMajorColor
    {
        get
        {
            if (EditorGUIUtility.isProSkin)
                return kGridMajorColorDark;
            else
                return kGridMajorColorLight;
        }
    }

I want to use the flag defaultColors to decide if to use the default colors or the custom colors.

The problem is that if I'm doing :

private static Color gridMinorColor
    {
        get
        {
            if (defaultColors == true)
            {
                if (EditorGUIUtility.isProSkin)
                    return kGridMinorColorDark;
                else
                    return kGridMinorColorLight;
            }
        }
    }

I'm getting error on the get since it's not returning anything now.

Well simply not all cases of gridMinorColor return a valid value. In your case you can also use the conditional operator ? to make your code a bit more easy to read/write

private static Color gridMinorColor
{
    get
    {
        if (defaultColors)
        {
            return EditorGUIUtility.isProSkin ? kGridMinorColorDark : kGridMinorColorLight;
        }

        // equals the else part
        // but since you return within the if you can skip the else keyword
        return EditorGUIUtility.isProSkin ? CustomkGridMinorColorDark : CustomkGridMinorColorLight;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM