void OnEnable()
 {
     instance = this;
     this.autoRepaintOnSceneChange = true;
     this.minSize         = new Vector2(236, 250);
     m_RowBackgroundStyle = new GUIStyle();
     m_RowBackgroundStyle.normal.background = EditorGUIUtility.whiteTexture;
     s_CurrentPalette = null;
     RefreshAvailablePalettes();
 }
Beispiel #2
0
 void RefreshAvailablePalettes()
 {
     MaterialPalette cur = CurrentPalette;
     m_AvailablePalettes = FileUtility.FindAndLoadAssets<MaterialPalette>();
     m_AvailablePalettes_Str = m_AvailablePalettes.Select(x => x.name).ToArray();
     ArrayUtility.Add<string>(ref m_AvailablePalettes_Str, string.Empty);
     ArrayUtility.Add<string>(ref m_AvailablePalettes_Str, "New Material Palette...");
     m_CurrentPaletteIndex = System.Array.IndexOf(m_AvailablePalettes, cur);
     s_MaterialPalettePath.SetValue(AssetDatabase.GetAssetPath(cur), true);
 }
Beispiel #3
0
        static void CreateMaterialPalette()
        {
            string path = FileUtility.GetSelectedDirectory() + "/Material Palette.asset";

            // Only generate unique path if it already exists - otherwise GenerateAssetUniquePath can return empty string
            // in event of path existing in a directory that is not yet created.
            if (FileUtility.Exists(path))
            {
                path = AssetDatabase.GenerateUniqueAssetPath(path);
            }

            MaterialPalette newPalette = FileUtility.LoadRequired <MaterialPalette>(path);

            newPalette.array = Selection.GetFiltered(typeof(Material), SelectionMode.DeepAssets).Cast <Material>().ToArray();
            UnityEditor.EditorUtility.SetDirty(newPalette);
            EditorGUIUtility.PingObject(newPalette);
        }
 void SetMaterialPalette(MaterialPalette palette)
 {
     s_CurrentPalette = palette;
     RefreshAvailablePalettes();
 }
        void OnGUI()
        {
            DoContextMenu();

            GUILayout.Label("Quick Material", EditorStyles.boldLabel);
            Rect r    = GUILayoutUtility.GetLastRect();
            int  left = Screen.width - 68;

            GUILayout.BeginHorizontal(GUILayout.MaxWidth(Screen.width - 74));
            GUILayout.BeginVertical();

            m_QueuedMaterial = (Material)EditorGUILayout.ObjectField(m_QueuedMaterial, typeof(Material), true);

            GUILayout.Space(2);

            if (GUILayout.Button("Apply (Ctrl+Shift+Click)"))
            {
                ApplyMaterial(MeshSelection.topInternal, m_QueuedMaterial);
            }

            GUI.enabled = editor != null && MeshSelection.selectedFaceCount > 0;
            if (GUILayout.Button("Match Selection"))
            {
                m_QueuedMaterial = EditorMaterialUtility.GetActiveSelection();
            }
            GUI.enabled = true;

            GUILayout.EndVertical();

            GUI.Box(new Rect(left, r.y + r.height + 2, 64, 64), "");

            var previewTexture = EditorMaterialUtility.GetPreviewTexture(m_QueuedMaterial);

            if (previewTexture != null)
            {
                GUI.Label(new Rect(left + 2, r.y + r.height + 4, 60, 60), previewTexture);
            }
            else
            {
                GUI.Box(new Rect(left + 2, r.y + r.height + 4, 60, 60), "");
                GUI.Label(new Rect(left + 2, r.height + 28, 120, 32), "None\n(Texture)");
            }

            GUILayout.EndHorizontal();

            GUILayout.Space(4);

            GUI.backgroundColor = PreferenceKeys.proBuilderDarkGray;
            UI.EditorGUIUtility.DrawSeparator(2);
            GUI.backgroundColor = Color.white;

            GUILayout.Label("Material Palette", EditorStyles.boldLabel);

            EditorGUI.BeginChangeCheck();

            m_CurrentPaletteIndex = EditorGUILayout.Popup("", m_CurrentPaletteIndex, m_AvailablePalettes_Str);

            if (EditorGUI.EndChangeCheck())
            {
                MaterialPalette newPalette = null;

                // Add a new material palette
                if (m_CurrentPaletteIndex >= m_AvailablePalettes.Length)
                {
                    string path = AssetDatabase.GenerateUniqueAssetPath("Assets/Material Palette.asset");
                    newPalette = FileUtility.LoadRequired <MaterialPalette>(path);
                    EditorGUIUtility.PingObject(newPalette);
                }
                else
                {
                    newPalette = m_AvailablePalettes[m_CurrentPaletteIndex];
                }

                SetMaterialPalette(newPalette);
            }

            EditorGUI.BeginChangeCheck();
            s_CurrentPalette = (MaterialPalette)EditorGUILayout.ObjectField(s_CurrentPalette, typeof(MaterialPalette), false);
            if (EditorGUI.EndChangeCheck())
            {
                SetMaterialPalette(s_CurrentPalette);
            }

            GUILayout.Space(4);

            Material[] materials = CurrentPalette;

            m_ViewScroll = GUILayout.BeginScrollView(m_ViewScroll);

            for (int i = 0; i < materials.Length; i++)
            {
                if (i == 10)
                {
                    GUILayout.Space(2);
                    GUI.backgroundColor = PreferenceKeys.proBuilderLightGray;
                    UI.EditorGUIUtility.DrawSeparator(1);
                    GUI.backgroundColor = Color.white;
                    GUILayout.Space(2);
                }

                GUILayout.BeginHorizontal();
                if (i < 10)
                {
                    if (GUILayout.Button("Alt + " + (i == 9 ? 0 : (i + 1)).ToString(), EditorStyles.miniButton, GUILayout.MaxWidth(58)))
                    {
                        ApplyMaterial(MeshSelection.topInternal, materials[i]);
                    }
                }
                else
                {
                    if (GUILayout.Button("Apply", EditorStyles.miniButtonLeft, GUILayout.MaxWidth(44)))
                    {
                        ApplyMaterial(MeshSelection.topInternal, materials[i]);
                    }

                    GUI.backgroundColor = Color.red;
                    if (GUILayout.Button("", EditorStyles.miniButtonRight, GUILayout.MaxWidth(14)))
                    {
                        Material[] temp = new Material[materials.Length - 1];
                        System.Array.Copy(materials, 0, temp, 0, materials.Length - 1);
                        materials = temp;
                        SaveUserMaterials(materials);
                        return;
                    }
                    GUI.backgroundColor = Color.white;
                }

                EditorGUI.BeginChangeCheck();
                materials[i] = (Material)EditorGUILayout.ObjectField(materials[i], typeof(Material), false);
                if (EditorGUI.EndChangeCheck())
                {
                    SaveUserMaterials(materials);
                }

                GUILayout.EndHorizontal();
            }


            if (GUILayout.Button("Add"))
            {
                Material[] temp = new Material[materials.Length + 1];
                System.Array.Copy(materials, 0, temp, 0, materials.Length);
                materials = temp;
                SaveUserMaterials(materials);
            }

            GUILayout.EndScrollView();
        }