コード例 #1
0
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// DrawMeshDecalMaterialList
        /// Draw actual selectable material (looking inside it's folder) list and handle dev's selections
        /// </summary>
        ///////////////////////////////////////////////////////////////////////////////////////////////////////
        public static Material DrawMeshDecalMaterialList(Material actualMaterial, float areaWidth)
        {
            // save actual gui color
            Color actualGuiColor = GUI.color;
            Color bgColor        = GUI.backgroundColor;

            // initialise selected material to actual material
            Material selectedMaterial = actualMaterial;

            EditorGUILayout.Separator();

            // get actual material list from decal's material folder
            bool redoTheList = false;

            for (int i = 0; i < meshDecalMaterialsList.Count; i++)
            {
                if (AssetDatabase.GetAssetPath(meshDecalMaterialsList [i]).Length < 2)
                {
                    redoTheList = true;
                }
            }

            if (redoTheList)
            {
                Debug.Log("Material deleted, redo mesh decal list");
            }

            if ((meshDecalMaterialsList.Count <= 0) || redoTheList)
            {
                meshDecalMaterialsList = EditorBasicFunctions.GetMeshDecalMaterialList();
            }

            // calculate paths
            List <string> localTotalMaterialPathList = new List <string> ();

            for (int i = 0; i < meshDecalMaterialsList.Count; i++)
            {
                string totalPath  = AssetDatabase.GetAssetPath(meshDecalMaterialsList [i]);
                string actualPath = Path.GetDirectoryName(totalPath);

                int index = 0;

                for (int j = 0; j < actualPath.Length; j++)
                {
                    if (actualPath [j] == '/')
                    {
                        index = j;
                    }
                }

                string finalPath = actualPath.Substring(index + 1, actualPath.Length - index - 1);

                localTotalMaterialPathList.Add(finalPath);
            }

            List <string> localMaterialPathList = localTotalMaterialPathList.Distinct().ToList();

            if (!showSystemFolders)
            {
                localMaterialPathList.Remove("System");
            }

            //print ("------------------------------------------");
            //for (int i = 0; i < localMaterialPathList.Count; i++)
            //{
            //print (localMaterialPathList [i]);
            //}

            EditorGUILayout.Separator();
            GenericMeshDecal.actualFolderIndex = EditorGUILayout.Popup(GenericMeshDecal.actualFolderIndex, localMaterialPathList.ToArray(), new GUILayoutOption[] { GUILayout.Width(0.81f * areaWidth) });
            EditorGUILayout.Separator();

            string selectedMaterialPath = BasicDefines.NOT_DEFINED;

            if (localMaterialPathList.Count > 0)
            {
                selectedMaterialPath = localMaterialPathList [GenericMeshDecal.actualFolderIndex];
            }

            // test some things
            int      numberOfMaterialsPerLine = 4;
            bool     begin = false;
            int      cont  = 0;
            bool     actualSelectedElementIsInSelectedPathList = false;
            Material firstElementInActualPath = null;


            // draw materials in editor window
            for (int i = 0; i < Mathf.Ceil((float)meshDecalMaterialsList.Count); i++)
            {
                if (localTotalMaterialPathList [i] == selectedMaterialPath)
                {
                    if (firstElementInActualPath == null)
                    {
                        firstElementInActualPath = meshDecalMaterialsList [i];
                    }

                    if ((selectedMaterial == meshDecalMaterialsList [i]))
                    {
                        actualSelectedElementIsInSelectedPathList = true;
                    }

                    if (cont % numberOfMaterialsPerLine == 0)
                    {
                        GUILayout.BeginHorizontal();
                        begin = true;
                    }

                    if (actualMaterial == meshDecalMaterialsList [i])
                    {
                        GUI.color           = new Color(1, 1, 1, 1);
                        GUI.backgroundColor = new Color(0.6f, 0.0f, 0.6f, 1f);
                    }
                    else
                    {
                        GUI.color           = new Color(1, 1, 1, 1f);
                        GUI.backgroundColor = new Color(1, 1, 1, 0.3f);
                    }

                    float buttonsScale = 0.94f * areaWidth / (numberOfMaterialsPerLine + 0.3f);

                    // test if one material is selected
                    bool selected = GUILayout.Button(new GUIContent(meshDecalMaterialsList [i].mainTexture, meshDecalMaterialsList [i].name), new GUILayoutOption[] {
                        GUILayout.Width(buttonsScale),
                        GUILayout.Height(buttonsScale)
                    });

                    // if selected then is the actual selected material
                    if (selected)
                    {
                        selectedMaterial = meshDecalMaterialsList [i];
                    }

                    if (cont % numberOfMaterialsPerLine == numberOfMaterialsPerLine - 1)
                    {
                        GUILayout.EndHorizontal();
                        begin = false;
                    }

                    cont++;
                }
            }

            if (!actualSelectedElementIsInSelectedPathList && DecalManagerWindow.autoChangeToFirstElementInList)
            {
                //print ("Change to firstElementInActualPath: " + firstElementInActualPath.name);
                selectedMaterial = firstElementInActualPath;
            }

            if (begin)
            {
                EditorGUILayout.EndHorizontal();
            }

            // restore Gui Color
            GUI.color           = actualGuiColor;
            GUI.backgroundColor = bgColor;


            // return actual selected material
            return(selectedMaterial);
        }