UNITY文档翻译之GetColorBlindSafePalette方法(UNITY非官方中文文档)

**

UNITY文档翻译之GetColorBlindSafePalette方法

**

public static int GetColorBlindSafePalette(Color[] palette,float minimumLuminance,float maximumLuminance);

Parameters参数

参数名称 参数含义
palette调色板 数组形式的调色板(笑)
minimumLuminance最小亮度 最小亮度允许为0到1。建议黑暗背景至少设为0.2
maximumLuminance最大亮度 最大亮度允许为0到1。建议较亮背景至多设为0.8

Returns返回值
返回明确的int类型的调色板颜色的数字(代码)
Description描述
获取应区分于normal vision(正常视力),deuteranopia(双眼),protanopia,tritanopia的颜色的调色板的参数(实在不知道咋翻译2333)

在你使用这个方法之前要记得事先把你需要的颜色的个数分配给size参数返回值表示了你调色板的明确的颜色的个数(上面说过了)。如果你设定的明确的颜色的个数少于你设定的size参数,那么调色板将会按照颜色的顺序一直重复

将下面脚本作为ColorSwatchExample.cs添加到Assets/Editor窗口并且导航到菜单选项 Window->Color Swatch Example.

using UnityEditor;
using UnityEngine;
using UnityEngine.Accessibility;

public class ColorSwatchExample : EditorWindow
{
    // 你想要设定的调色板的大小
    private const int k_SwatchTextureSize = 16;
    //调色板颜色数量的最大值
    private const int k_MaxPaletteSize = 10;

    [MenuItem("Window/Color Swatch Example")]
    private static void CreateWindow()
    {
        var window = GetWindow<ColorSwatchExample>();
        window.position = new Rect(0f, 0f, 400f, 80f);
    }

    // 用于调色板背景的纹理(比如斜杠横杠)
    private Texture2D[] m_SwatchBackgrounds = new Texture2D[k_MaxPaletteSize];

    // 设定的调色板颜色的数量
    [SerializeField]
    private int m_PaletteSize = 8;
    // 所需亮度值的范围
    [SerializeField]
    private Vector2 m_DesiredLuminance = new Vector2(0.2f, 0.9f);
    //获取的颜色
    [SerializeField]
    private Color[] m_Palette;
    // 调色板重复之前的唯一颜色数(雾)
    [SerializeField]
    private int m_NumUniqueColors;

    // 当窗口第一次打开时创建调色板背景纹理
    protected virtual void OnEnable()
    {
        titleContent = new GUIContent("Color Swatches");

        // create background swatches with different patterns for repeated colors
        m_SwatchBackgrounds[0] = CreateSwatchBackground(k_SwatchTextureSize, 0, 0);
        m_SwatchBackgrounds[1] = CreateSwatchBackground(k_SwatchTextureSize, 1, 4);
        m_SwatchBackgrounds[2] = CreateSwatchBackground(k_SwatchTextureSize, 1, 3);
        m_SwatchBackgrounds[3] = CreateSwatchBackground(k_SwatchTextureSize, 6, 1);
        m_SwatchBackgrounds[4] = CreateSwatchBackground(k_SwatchTextureSize, 4, 3);
        m_SwatchBackgrounds[5] = CreateSwatchBackground(k_SwatchTextureSize, 6, 6);
        m_SwatchBackgrounds[6] = CreateSwatchBackground(k_SwatchTextureSize, 4, 2);
        m_SwatchBackgrounds[7] = CreateSwatchBackground(k_SwatchTextureSize, 6, 4);
        m_SwatchBackgrounds[8] = CreateSwatchBackground(k_SwatchTextureSize, 2, 5);
        m_SwatchBackgrounds[9] = CreateSwatchBackground(k_SwatchTextureSize, 1, 2);

        UpdatePalette();
    }

    // 当窗口关闭的时候清除所有的调色板背景纹理
    protected virtual void OnDisable()
    {
        for (int i = 0, count = m_SwatchBackgrounds.Length; i < count; ++i)
            DestroyImmediate(m_SwatchBackgrounds[i]);
    }

    protected virtual void OnGUI()
    {
        // 输入需要的颜色数和亮度值
        EditorGUI.BeginChangeCheck();

        m_PaletteSize = EditorGUILayout.IntSlider("Palette Size", m_PaletteSize, 0, k_MaxPaletteSize);

        float min = m_DesiredLuminance.x;
        float max = m_DesiredLuminance.y;
        EditorGUILayout.MinMaxSlider("Luminance Range", ref min, ref max, 0f, 1f);
        m_DesiredLuminance = new Vector2(min, max);

        if (EditorGUI.EndChangeCheck())
        {
            UpdatePalette();
        }

        //如果参数超出范围则警告
        if (m_NumUniqueColors == 0)
        {
            string warningMessage = "Unable to generate any unique colors with the specified luminance requirements.";
            EditorGUILayout.HelpBox(warningMessage, MessageType.Warning);
        }
        // 否则在一排输出
        else
        {
            using (new GUILayout.HorizontalScope())
            {
                GUILayout.FlexibleSpace();
                Color oldColor = GUI.color;

                int swatchBackgroundIndex = 0;
                for (int i = 0; i < m_PaletteSize; ++i)
                {
                    //达到重复的颜色时更改色斑背景纹理
                    if (i > 0 && i % m_NumUniqueColors == 0)
                        ++swatchBackgroundIndex;

                    Rect rect = GUILayoutUtility.GetRect(k_SwatchTextureSize * 2, k_SwatchTextureSize * 2);
                    rect.width = k_SwatchTextureSize * 2;

                    GUI.color = m_Palette[i];
                    GUI.DrawTexture(rect, m_SwatchBackgrounds[swatchBackgroundIndex], ScaleMode.ScaleToFit, true);
                }

                GUI.color = oldColor;
                GUILayout.FlexibleSpace();
            }
        }
    }

    // 创建白色纹理,并丢弃一些像素来形成图案
    private Texture2D CreateSwatchBackground(int size, int discardPixelCount, int discardPixelStep)
    {
        var swatchBackground = new Texture2D(size, size);
        swatchBackground.hideFlags = HideFlags.HideAndDontSave;
        swatchBackground.filterMode = FilterMode.Point;

        var pixels = swatchBackground.GetPixels32();
        int counter = 0;
        bool discard = false;
        for (int i = 0, count = pixels.Length; i < count; ++i)
        {
            pixels[i] = new Color32(255, 255, 255, (byte)(discard ? 0 : 255));
            ++counter;
            if (discard && counter == discardPixelCount)
            {
                discard = false;
                counter = 0;
            }
            else if (!discard && counter == discardPixelStep)
            {
                discard = true;
                counter = 0;
            }
        }
        swatchBackground.SetPixels32(pixels);

        swatchBackground.Apply();
        return swatchBackground;
    }

    //请求新的调色板
    private void UpdatePalette()
    {
        m_Palette = new Color[m_PaletteSize];
        m_NumUniqueColors =
            VisionUtility.GetColorBlindSafePalette(m_Palette, m_DesiredLuminance.x, m_DesiredLuminance.y);
    }
}

博主每天都会翻译一篇2019.3的Unity的文档
发布了4 篇原创文章 · 获赞 5 · 访问量 203

猜你喜欢

转载自blog.csdn.net/qq_15534667/article/details/104950901