Пример #1
0
    private void DrawPalette()
    {
        var palette = m_Root.Q <VisualElement>(name: "palette");

        palette.Clear();
        var paletteSO = new SerializedObject(pixelAsset.Palette);
        var picker    = new ObjectField();

        picker.objectType = typeof(Palette);
        picker.BindProperty(paletteSO);
        // palette.Add(new IMGUIContainer(PaletteDrawOnGUI));
        palette.style.backgroundColor = Color.black;
        var colorsProp = paletteSO.FindProperty("Colors");

        for (int i = 0; i < colorsProp.arraySize; i++)
        {
            if (i == paletteIndex)
            {
                var entry = new ColorField();
                entry.pickingMode = PickingMode.Position;
                entry.BindProperty(colorsProp.GetArrayElementAtIndex(i));
                entry.showEyeDropper               = false;
                entry.showAlpha                    = false;
                entry.style.width                  = entry.style.height = 18;
                entry.style.marginLeft             = entry.style.marginRight = 0;
                entry.style.marginTop              = entry.style.marginBottom = 0;
                entry.style.borderColor            = Color.white;
                entry.style.borderLeftWidth        = entry.style.borderRightWidth = 1;
                entry.style.borderTopWidth         = entry.style.borderBottomWidth = 1;
                entry.style.borderBottomLeftRadius = entry.style.borderBottomRightRadius = 2;
                entry.style.borderTopLeftRadius    = entry.style.borderTopRightRadius = 2;
                entry.RegisterValueChangedCallback(OnPaletteEdit);
                palette.Add(entry);
            }
            else
            {
                var index = i;
                var entry = new Button(() =>
                {
                    paletteIndex = index;
                    var colors   = palette.Query <VisualElement>();
                    colors.ForEach(c => palette.Remove(c));
                    DrawPalette();
                });
                entry.style.backgroundImage = EditorGUIUtility.whiteTexture;
                entry.style.unityBackgroundImageTintColor = (Color)pixelAsset.Palette.Colors[i];
                entry.style.width      = entry.style.height = 16;
                entry.style.marginLeft = entry.style.marginRight = 1;
                entry.style.marginTop  = entry.style.marginBottom = 1;
                palette.Add(entry);
            }
        }
        DrawFrames();
        // palette.style.height = (pixelAsset.Palette.Colors.Length / 4) * 18;
    }