Example #1
0
    public static void DrawVegetationItemSelector(VegetationSystemPro vegetationSystemPro, VegetationPackagePro vegetationPackage, ref int selectedGridIndex, ref int vegIndex, ref int selectionCount, VegetationItemTypeSelection vegetationItemTypeSelection, int imageSize)
    {
        if (vegetationPackage == null)
        {
            return;
        }
        //AssetPreview.SetPreviewTextureCacheSize(100 + vegetationPackage.VegetationInfoList.Count);
        AssetPreview.SetPreviewTextureCacheSize(100 + vegetationSystemPro.GetMaxVegetationPackageItemCount());

        List <int> vegetationItemIndexList = new List <int>();

        for (int i = 0;
             i <= vegetationPackage.VegetationInfoList.Count - 1;
             i++)
        {
            VegetationItemInfoPro vegetationItemInfo = vegetationPackage.VegetationInfoList[i];
            switch (vegetationItemTypeSelection)
            {
            case VegetationItemTypeSelection.AllVegetationItems:
                vegetationItemIndexList.Add(i);
                break;

            case VegetationItemTypeSelection.LargeItems:

                if (vegetationItemInfo.VegetationType == VegetationType.Objects ||
                    vegetationItemInfo.VegetationType == VegetationType.LargeObjects ||
                    vegetationItemInfo.VegetationType == VegetationType.Tree)
                {
                    vegetationItemIndexList.Add(i);
                }
                break;

            case VegetationItemTypeSelection.Grass:
                if (vegetationItemInfo.VegetationType == VegetationType.Grass)
                {
                    vegetationItemIndexList.Add(i);
                }
                break;

            case VegetationItemTypeSelection.Plants:
                if (vegetationItemInfo.VegetationType == VegetationType.Plant)
                {
                    vegetationItemIndexList.Add(i);
                }
                break;

            case VegetationItemTypeSelection.Trees:
                if (vegetationItemInfo.VegetationType == VegetationType.Tree)
                {
                    vegetationItemIndexList.Add(i);
                }
                break;

            case VegetationItemTypeSelection.Objects:
                if (vegetationItemInfo.VegetationType == VegetationType.Objects)
                {
                    vegetationItemIndexList.Add(i);
                }
                break;

            case VegetationItemTypeSelection.LargeObjects:
                if (vegetationItemInfo.VegetationType == VegetationType.LargeObjects)
                {
                    vegetationItemIndexList.Add(i);
                }
                break;
            }
        }

        selectionCount = vegetationItemIndexList.Count;

        VegetationInfoComparer vIc = new VegetationInfoComparer
        {
            VegetationInfoList = vegetationPackage.VegetationInfoList
        };

        vegetationItemIndexList.Sort(vIc.Compare);

        GUIContent[] imageButtons = new GUIContent[vegetationItemIndexList.Count];

        for (int i = 0; i <= vegetationItemIndexList.Count - 1; i++)
        {
            if (vegetationPackage.VegetationInfoList[vegetationItemIndexList[i]].PrefabType == VegetationPrefabType.Mesh)
            {
                imageButtons[i] = new GUIContent
                {
                    image = AssetPreviewCache.GetAssetPreview(vegetationPackage.VegetationInfoList[vegetationItemIndexList[i]].VegetationPrefab)
                };
            }
            else
            {
                imageButtons[i] = new GUIContent
                {
                    image = AssetPreviewCache.GetAssetPreview(vegetationPackage.VegetationInfoList[vegetationItemIndexList[i]].VegetationTexture)
                };
            }
        }
        int imageWidth = imageSize;
        int columns    = Mathf.FloorToInt((EditorGUIUtility.currentViewWidth - 50) / imageWidth);
        int rows       = Mathf.CeilToInt((float)imageButtons.Length / columns);
        int gridHeight = (rows) * imageWidth;


        if (selectedGridIndex > imageButtons.Length - 1)
        {
            selectedGridIndex = 0;
        }
        if (imageButtons.Length > 0)
        {
            selectedGridIndex = GUILayout.SelectionGrid(selectedGridIndex, imageButtons, columns, GUILayout.MaxWidth(columns * imageWidth), GUILayout.MaxHeight(gridHeight));
        }

        vegIndex = vegetationItemIndexList.Count > selectedGridIndex ? vegetationItemIndexList[selectedGridIndex] : 0;
    }