void LoadPalettes()
    {
        var selection = Selection.activeGameObject;

        if (selection != null)
        {
            var sheet = selection.GetComponentInParent <PartsSheet>();
            if (sheet != null)
            {
                EditorApplication.delayCall += () =>
                {
                    palette  = sheet.palette;
                    parentTo = sheet.transform;
                    if (selected != null)
                    {
                        selected = sheet?.palette?.prefabs[0];
                    }
                    Repaint();
                };
            }
            else
            {
                Deselect();
            }
        }

        palettes.Clear();
        foreach (var guid in AssetDatabase.FindAssets("t:" + typeof(PartsPalette).Name))
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);
            var pal  = AssetDatabase.LoadAssetAtPath <PartsPalette>(path);
            if (pal != null)
            {
                palettes.Add(pal);
            }
        }

        paletteNames = new string[palettes.Count];
        for (int i = 0; i < palettes.Count; ++i)
        {
            if (palettes[i].title != null && palettes[i].title != "")
            {
                paletteNames[i] = palettes[i].title;
            }
            else
            {
                paletteNames[i] = palettes[i].name;
            }
        }

        if (palette != null && !palettes.Contains(palette))
        {
            palette = null;
        }

        if (palette == null && palettes.Count > 0)
        {
            palette = palettes[0];
        }
    }
        private void AddPartsPalette()
        {
            var partsPalette = new PartsPalette();

            AddComponent(partsPalette);
            AddControl(partsPalette);
        }
    void OnGUI()
    {
        EditorGUILayout.Space();

        int paletteIndex = palettes.IndexOf(palette);

        paletteIndex = EditorGUILayout.Popup("Palette", paletteIndex, paletteNames);
        palette      = paletteIndex < 0 ? null : palettes[paletteIndex];

        if (palette == null)
        {
            return;
        }

        if (palette != prevPalette)
        {
            foreach (GameObject obj in FindObjectsOfType(typeof(GameObject)))
            {
                if (obj.activeInHierarchy)
                {
                    var sheet = obj.GetComponent <PartsSheet>();
                    if (sheet != null)
                    {
                        if (sheet.palette == palette)
                        {
                            parentTo = sheet.transform;
                            Selection.activeGameObject = parentTo == null ? null : parentTo.gameObject;
                        }
                    }
                }
            }
            stageType = palette.stageType;
        }
        prevPalette = palette;

        if (ev.isMouse)
        {
            mousePos = ev.mousePosition;
            Repaint();
        }

        optionsToggle = EditorGUILayout.Foldout(optionsToggle, "Options");
        if (optionsToggle)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.Space();
            EditorGUILayout.BeginVertical();

            var par = EditorGUILayout.ObjectField("Parent To", parentTo, typeof(Transform), true) as Transform;
            if (par != parentTo)
            {
                if (par == null || (PrefabUtility.GetCorrespondingObjectFromSource(par) == null && PrefabUtility.GetPrefabInstanceHandle(par) == null))
                {
                    parentTo = par;
                }
            }

            var serializedObject = new SerializedObject(this);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("stageType"));
            serializedObject.ApplyModifiedProperties();

            EditorGUILayout.EndVertical();
            EditorGUILayout.EndHorizontal();
        }

        EditorGUILayout.Space();

        var header = EditorGUILayout.GetControlRect();

        GUI.Label(header, "Prefabs", EditorStyles.boldLabel);
        header.y     += header.height - 1f;
        header.height = 1f;
        EditorGUI.DrawRect(header, EditorStyles.label.normal.textColor);

        GUILayout.Space(2f);

        GUI.enabled = selected != null;
        if (GUILayout.Button("設置を終了", EditorStyles.miniButton))
        {
            Deselect();
        }
        GUI.enabled = true;
        if (ev.type == EventType.KeyDown && ev.keyCode == KeyCode.Escape)
        {
            Deselect();
        }

        var buttonHeight = EditorGUIUtility.singleLineHeight * 2f;
        var heightStyle  = GUILayout.Height(buttonHeight);

        var lastRect    = GUILayoutUtility.GetLastRect();
        var scrollMouse = mousePos;

        scrollMouse.x -= lastRect.xMin - prefabScroll.x;
        scrollMouse.y -= lastRect.yMax - prefabScroll.y;

        prefabScroll = EditorGUILayout.BeginScrollView(prefabScroll);

        if (palette.prefabs != null)
        {
            foreach (var prefab in palette.prefabs)
            {
                if (prefab == null)
                {
                    continue;
                }

                var rect = EditorGUILayout.GetControlRect(heightStyle);

                var bgRect = rect;
                bgRect.x      -= 1f;
                bgRect.y      -= 1f;
                bgRect.width  += 2f;
                bgRect.height += 2f;
                if (prefab == selected)
                {
                    EditorGUI.DrawRect(bgRect, new Color32(0x42, 0x80, 0xe4, 0xff));
                }
                {
                    EditorGUIUtility.AddCursorRect(bgRect, MouseCursor.Link);

                    if (bgRect.Contains(scrollMouse))
                    {
                        EditorGUI.DrawRect(bgRect, new Color32(0x42, 0x80, 0xe4, 0x40));
                        if (ev.type == EventType.MouseDown)
                        {
                            EditorApplication.delayCall += () =>
                            {
                                if (selected != prefab)
                                {
                                    Tools.current = Tool.None;
                                    selected      = prefab;
                                    Selection.activeGameObject = parentTo == null ? null : parentTo.gameObject;
                                }
                                else
                                {
                                    selected = null;
                                }
                                SceneView.RepaintAll();
                            };
                        }
                    }
                }

                var iconRect = new Rect(rect.x, rect.y, rect.height, rect.height);

                var icon = AssetPreview.GetAssetPreview(prefab);
                if (icon != null)
                {
                    GUI.DrawTexture(iconRect, icon, ScaleMode.ScaleToFit, true, 1f, Color.white, Vector4.zero, Vector4.one * 4f);
                }
                else
                {
                    EditorGUI.DrawRect(iconRect, EditorStyles.label.normal.textColor * 0.25f);
                }

                var labelRect = rect;
                labelRect.x     += iconRect.width + 4f;
                labelRect.width -= iconRect.width + 4f;
                labelRect.height = EditorGUIUtility.singleLineHeight;
                labelRect.y     += (buttonHeight - labelRect.height) * 0.5f;
                var labelStyle = prefab == selected ? EditorStyles.whiteBoldLabel : EditorStyles.label;
                GUI.Label(labelRect, prefab.name, labelStyle);
            }
        }

        EditorGUILayout.EndScrollView();

        if (AssetPreview.IsLoadingAssetPreviews())
        {
            Repaint();
        }
    }