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

        paletteNames = new string[palettes.Count];
        for (int i = 0; i < palettes.Count; ++i)
        {
            paletteNames[i] = palettes[i].name;
        }

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

        if (palette == null && palettes.Count > 0)
        {
            palette = palettes[0];
        }
    }
示例#2
0
        /// <summary>
        /// 选择调色板
        /// </summary>
        private void GUI_ChoosePalette()
        {
            // 选择Palette
            allPalettes = PrefabPalette.GetAllPalettes();
            var paletteNames = new string[allPalettes.Length];

            for (int i = 0; i < paletteNames.Length; i++)
            {
                paletteNames[i] = allPalettes[i].name;
            }

            if (paletteNames.Length == 0)
            {
                paletteNames = new[] { "No Palette !" }
            }
            ;
            GUI.SetNextControlName("popup_01");
            EditorGUI.BeginChangeCheck();
            currentPaletteIndex = EditorGUILayout.Popup(currentPaletteIndex, paletteNames,
                                                        UnityGUIStyles.toolbarDropDown, GUILayout.Width(160));
            if (EditorGUI.EndChangeCheck())
            {
                // 重置当前选择的TileIndex为0
                currentTileIndex = 0;
            }

            // 新建Palette
            if (GUILayout.Button("Create Palette", UnityGUIStyles.toolbarButton, GUILayout.Width(100)))
            {
                PrefabPalette.CreatePalette();
            }
        }
    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(PrefabPalette).Name))
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);
            var pal  = AssetDatabase.LoadAssetAtPath <PrefabPalette>(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];
        }
    }
    void OnSelectionChange()
    {
        var selection = Selection.activeGameObject;

        if (selection != null)
        {
            var sheet = selection.GetComponentInParent <PrefabSheet>();
            if (sheet != null)
            {
                EditorApplication.delayCall += () => {
                    palette  = sheet.palette;
                    parentTo = sheet.transform;
                    selected = null;
                    Repaint();
                };
            }
            else
            {
                Deselect();
            }
        }

        LoadPalettes();
    }
    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 (ev.isMouse)
        {
            mousePos = ev.mousePosition;
            Repaint();
        }

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

            raycastMask = LayerMaskField("Raycast Mask", raycastMask);

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

            onlyUpwards = EditorGUILayout.Toggle("Only Up Normals", onlyUpwards);

            //Snapping
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Snap");
            snap        = EditorGUILayout.Toggle(snap, GUILayout.Width(15f));
            GUI.enabled = snap;
            snapValue   = EditorGUILayout.FloatField(snapValue);
            snapValue   = Mathf.Max(snapValue, 0f);
            GUI.enabled = true;
            EditorGUILayout.EndHorizontal();

            //Rotation mode
            rotationMode = (RotationType)EditorGUILayout.EnumPopup("Rotation Mode", rotationMode);
            if (rotationMode == RotationType.Random)
            {
                minRotation = EditorGUILayout.Vector3Field("Min Rotation", minRotation);
                maxRotation = EditorGUILayout.Vector3Field("Max Rotation", maxRotation);
            }
            else if (rotationMode == RotationType.Custom)
            {
                customRotation = EditorGUILayout.Vector3Field("Rotation", customRotation);
            }

            //Scale mode
            scaleMode = (ScaleType)EditorGUILayout.EnumPopup("Scale Mode", scaleMode);
            if (scaleMode == ScaleType.Random)
            {
                minScale = EditorGUILayout.Vector3Field("Min Scale", minScale);
                maxScale = EditorGUILayout.Vector3Field("Max Scale", maxScale);
            }
            else if (scaleMode == ScaleType.RandomXYZ)
            {
                minScaleU = EditorGUILayout.FloatField("Min Scale", minScaleU);
                maxScaleU = EditorGUILayout.FloatField("Max Scale", maxScaleU);
            }
            else if (scaleMode == ScaleType.Custom)
            {
                customScale = EditorGUILayout.Vector3Field("Scale", customScale);
            }

            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("Stop Placement (ESC)", 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);

        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));
            }
            else
            {
                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 += () =>
                        {
                            selected = prefab;
                            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.Space();
        EditorGUILayout.EndScrollView();

        if (AssetPreview.IsLoadingAssetPreviews())
        {
            Repaint();
        }
    }
    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 <PrefabSheet>();
                    if (sheet != null)
                    {
                        if (sheet.palette == palette)
                        {
                            parentTo = sheet.transform;
                        }
                    }
                }
            }
        }
        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;
                }
            }

            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("Stop Placement (ESC)", 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);

        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)
                            {
                                selected = prefab;
                            }
                            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();
        }
    }