Exemplo n.º 1
0
        private void DrawGrass()
        {
            EditorGUILayout.LabelField("Items", EditorStyles.boldLabel);

            grassScrollPos = EditorGUILayout.BeginScrollView(grassScrollPos, EditorStyles.textArea, GUILayout.Height(thumbSize + 10f));
            using (new EditorGUILayout.HorizontalScope())
            {
                for (int i = 0; i < script.grassPrefabs.Count; i++)
                {
                    Texture2D thumb = AssetPreview.GetAssetPreview(script.grassPrefabs[i].prefab);
                    if (script.grassPrefabs[i].type == SpawnerBase.GrassType.Billboard)
                    {
                        thumb = script.grassPrefabs[i].billboard;
                    }
                    if (thumb == null)
                    {
                        thumb = EditorGUIUtility.IconContent("d_BuildSettings.Broadcom").image as Texture2D;
                    }

                    if (GUILayout.Button(new GUIContent("", thumb), (selectedGrassID == i) ? VegetationSpawnerEditor.PreviewTexSelected : VegetationSpawnerEditor.PreviewTex, GUILayout.MinHeight(thumbSize), GUILayout.MaxWidth(thumbSize), GUILayout.MaxHeight(thumbSize)))
                    {
                        selectedGrassID = i;
                    }
                }
            }
            EditorGUILayout.EndScrollView();

            //Edge case: Clamp in case there's a switch to scene with less items
            selectedGrassID = Mathf.Min(selectedGrassID, script.grassPrefabs.Count - 1);

            VegetationSpawner.GrassPrefab grass = script.grassPrefabs.Count > 0 ? script.grassPrefabs[selectedGrassID] : null;

            using (new EditorGUILayout.HorizontalScope())
            {
                if (grass != null)
                {
                    EditorGUILayout.LabelField("Instances: " + grass.instanceCount.ToString("##,#"), EditorStyles.miniLabel);
                }
                GUILayout.FlexibleSpace();

                if (GUILayout.Button(new GUIContent("Add", EditorGUIUtility.IconContent(EditorGUIUtility.isProSkin ? "d_Toolbar Plus" : "Toolbar Plus").image, "Add new item")))
                {
                    VegetationSpawner.GrassPrefab newGrass = new SpawnerBase.GrassPrefab();
                    script.grassPrefabs.Add(newGrass);

                    newGrass.seed   = Random.Range(0, 9999);
                    selectedGrassID = script.grassPrefabs.Count - 1;
                    newGrass.index  = script.grassPrefabs.Count;

                    script.RefreshGrassPrototypes();
                }

                if (GUILayout.Button(new GUIContent("", EditorGUIUtility.IconContent("TreeEditor.Duplicate").image, "Duplicate item")))
                {
                    GrassPrefab newGrass = Duplicate(script.grassPrefabs[selectedGrassID]);

                    script.grassPrefabs.Add(newGrass);
                    selectedGrassID = script.grassPrefabs.Count - 1;
                    newGrass.index  = script.grassPrefabs.Count;

                    script.RefreshGrassPrototypes();

                    //Spawn it now so changes are visible
                    script.SpawnGrass(script.grassPrefabs[selectedGrassID]);
                }

                if (GUILayout.Button(new GUIContent("", EditorGUIUtility.IconContent("d_TreeEditor.Trash").image, "Remove")))
                {
                    script.grassPrefabs.RemoveAt(selectedGrassID);

                    selectedGrassID = script.grassPrefabs.Count - 1;

                    script.RefreshGrassPrototypes();
                }
            }

            if (grass != null)
            {
                Undo.RecordObject(script, "Modified grass item");

                serializedObject.Update();
                using (var grassChange = new EditorGUI.ChangeCheckScope())
                {
                    EditorGUILayout.LabelField("Appearance", EditorStyles.boldLabel);

                    grass.type = (SpawnerBase.GrassType)EditorGUILayout.Popup("Render type", (int)grass.type, new string[] { "Mesh", "Billboard" });

                    if (grass.type == SpawnerBase.GrassType.Mesh)
                    {
                        grass.prefab = EditorGUILayout.ObjectField("Prefab", grass.prefab, typeof(GameObject), true) as GameObject;
                    }
                    if (grass.type == SpawnerBase.GrassType.Billboard)
                    {
                        grass.billboard = EditorGUILayout.ObjectField("Billboard", grass.billboard, typeof(Texture2D), true) as Texture2D;
                    }

                    grass.mainColor  = EditorGUILayout.ColorField("Main", grass.mainColor);
                    grass.linkColors = EditorGUILayout.Toggle(new GUIContent("Link", "Set the main and secondary color with one value"), grass.linkColors);

                    if (grass.linkColors)
                    {
                        grass.secondairyColor = grass.mainColor;
                    }
                    else
                    {
                        grass.secondairyColor = EditorGUILayout.ColorField("Secondary", grass.secondairyColor);
                    }
                    VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Width", "Min/max width of the mesh"), ref grass.minMaxWidth, 0f, 2f);
                    VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Length", "Min/max length of the mesh"), ref grass.minMaxHeight, 0f, 2f);

                    if (grassChange.changed)
                    {
                        EditorUtility.SetDirty(target);
                        script.UpdateProperties(grass);
                    }

                    EditorGUILayout.Space();

                    EditorGUILayout.LabelField("Spawning rules", EditorStyles.boldLabel);
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        grass.seed = EditorGUILayout.IntField("Seed", grass.seed, GUILayout.MaxWidth(EditorGUIUtility.labelWidth + 50f));
                        if (GUILayout.Button("Randomize", GUILayout.MaxWidth(100f)))
                        {
                            grass.seed = Random.Range(0, 99999);
                        }
                    }

                    grass.probability = EditorGUILayout.Slider("Spawn chance %", grass.probability, 0f, 100f);

                    EditorGUILayout.Space();

                    grass.collisionCheck   = EditorGUILayout.Toggle(new GUIContent("Collision check", "Take into account the collision cache to avoid spawning inside colliders (see Settings tab)"), grass.collisionCheck);
                    grass.rejectUnderwater = EditorGUILayout.Toggle(new GUIContent("Remove underwater", "The water height level can be set in the settings tab"), grass.rejectUnderwater);
                    VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Height range", "Min/max height this item can spawn at"), ref grass.heightRange, 0f, 1000f);
                    VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Slope range", "Min/max slope (0-90 degrees) this item can spawn at"), ref grass.slopeRange, 0f, 90f);
                    VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Curvature range", "0=Concave (bowl), 0.5=flat, 1=convex (edge)"), ref grass.curvatureRange, 0f, 1f);

                    EditorGUILayout.Space();

                    LayerMaskSettings(grass.layerMasks);

                    if (grassChange.changed)
                    {
                        serializedObject.ApplyModifiedProperties();
                    }
                }
                EditorGUILayout.Space();

                if (GUILayout.Button(new GUIContent(" Respawn", EditorGUIUtility.IconContent("d_Refresh").image), GUILayout.Height(30f)))
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Restart();

                    script.SpawnGrass(script.grassPrefabs[selectedGrassID]);

                    sw.Stop();

                    Log.Add("Respawning grass: " + sw.Elapsed.Seconds + " seconds...");
                }
            }
        }
Exemplo n.º 2
0
        private void DrawTrees()
        {
            EditorGUILayout.LabelField("Species", EditorStyles.boldLabel);

            //Tree item view
            treeScrollPos = EditorGUILayout.BeginScrollView(treeScrollPos, EditorStyles.textArea, GUILayout.Height(thumbSize + 10f));
            using (new EditorGUILayout.HorizontalScope())
            {
                for (int i = 0; i < script.treeTypes.Count; i++)
                {
                    if (script.treeTypes[i] == null)
                    {
                        continue;
                    }

                    Texture2D thumb = EditorGUIUtility.IconContent("d_BuildSettings.Broadcom").image as Texture2D;

                    if (script.treeTypes[i].prefabs.Count > 0)
                    {
                        if (script.treeTypes[i].prefabs[0] != null)
                        {
                            if (script.treeTypes[i].prefabs[0].prefab)
                            {
                                thumb = AssetPreview.GetAssetPreview(script.treeTypes[i].prefabs[0].prefab);
                            }
                        }
                    }

                    if (GUILayout.Button(new GUIContent("", thumb), (selectedTreeID == i) ? VegetationSpawnerEditor.PreviewTexSelected : VegetationSpawnerEditor.PreviewTex, GUILayout.MinHeight(thumbSize), GUILayout.MaxWidth(thumbSize), GUILayout.MaxHeight(thumbSize)))
                    {
                        selectedTreeID = i;
                    }
                }
            }

            EditorGUILayout.EndScrollView();

            Undo.RecordObject(script, "Modified tree species");

            serializedObject.Update();
            using (var treeChange = new EditorGUI.ChangeCheckScope())
            {
                //Tree type view options
                using (new EditorGUILayout.HorizontalScope())
                {
                    GUILayout.FlexibleSpace();

                    if (GUILayout.Button(new GUIContent("Add", EditorGUIUtility.IconContent(EditorGUIUtility.isProSkin ? "d_Toolbar Plus" : "Toolbar Plus").image, "Add new item")))
                    {
                        VegetationSpawner.TreeType tree = TreeType.New();

                        script.treeTypes.Add(tree);
                        selectedTreeID = script.treeTypes.Count - 1;
                    }
                    if (script.treeTypes.Count > 0)
                    {
                        if (GUILayout.Button(new GUIContent("", EditorGUIUtility.IconContent("d_TreeEditor.Trash").image, "Remove")))
                        {
                            script.treeTypes.RemoveAt(selectedTreeID);
                            selectedTreeID = script.treeTypes.Count - 1;

                            if (selectedTreeID < 0)
                            {
                                selectedTreeID = 0;
                            }

                            script.RefreshTreePrefabs();
                        }
                    }
                }

                //Settings for selected


                if (script.treeTypes.Count > 0)
                {
                    VegetationSpawner.TreeType tree = script.treeTypes[selectedTreeID];

                    EditorGUILayout.LabelField("Prefabs", EditorStyles.boldLabel);
                    if (tree.prefabs.Count == 0)
                    {
                        EditorGUILayout.HelpBox("Add a tree prefab first", MessageType.Info);
                    }

                    for (int p = 0; p < tree.prefabs.Count; p++)
                    {
                        TreePrefab item = tree.prefabs[p];

                        using (new EditorGUILayout.HorizontalScope())
                        {
                            using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
                            {
                                EditorGUI.BeginChangeCheck();
                                item.prefab = EditorGUILayout.ObjectField("Prefab", item.prefab, typeof(GameObject), true) as GameObject;

                                if (item.prefab)
                                {
                                    if (EditorUtility.IsPersistent(item.prefab) == false)
                                    {
                                        EditorGUILayout.HelpBox("Prefab cannot be a scene instance", MessageType.Error);
                                    }
                                }
                                item.probability = EditorGUILayout.Slider("Spawn chance %", item.probability, 0f, 100f);
                                if (EditorGUI.EndChangeCheck())
                                {
                                    script.UpdateTreeItem(tree);
                                    if (autoRespawnTrees.boolValue)
                                    {
                                        script.SpawnTree(tree);
                                    }
                                    EditorUtility.SetDirty(target);
                                }
                            }

                            if (GUILayout.Button(new GUIContent("", EditorGUIUtility.IconContent("d_TreeEditor.Trash").image, "Remove")))
                            {
                                tree.prefabs.RemoveAt(p);

                                script.RefreshTreePrefabs();
                            }
                        }
                    }

                    using (new EditorGUILayout.HorizontalScope())
                    {
                        GUILayout.FlexibleSpace();

                        if (GUILayout.Button(new GUIContent("Add", EditorGUIUtility.IconContent(EditorGUIUtility.isProSkin ? "d_Toolbar Plus" : "Toolbar Plus").image, "Add new item")))
                        {
                            TreePrefab p = new TreePrefab();
                            p.probability = 100f;

                            tree.prefabs.Add(p);

                            script.RefreshTreePrefabs();
                        }
                    }

                    if (tree.prefabs.Count > 0)
                    {
                        if (tree.prefabs[0].prefab != null)
                        {
                            EditorGUILayout.LabelField("Spawn rules", EditorStyles.boldLabel);

                            EditorGUI.BeginChangeCheck();

                            using (new EditorGUILayout.HorizontalScope())
                            {
                                tree.seed = EditorGUILayout.IntField("Seed", tree.seed, GUILayout.MaxWidth(EditorGUIUtility.labelWidth + 50f));
                                if (GUILayout.Button("Randomize", GUILayout.MaxWidth(100f)))
                                {
                                    tree.seed = Random.Range(0, 99999);
                                }
                            }
                            tree.probability = EditorGUILayout.Slider("Global spawn chance %", tree.probability, 0f, 100f);
                            tree.distance    = EditorGUILayout.Slider("Distance between", tree.distance, 0.5f, 50f);
                            VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Scale", "Scale is randomly selected from this range"), ref tree.scaleRange, 0f, 2f);
                            tree.sinkAmount = EditorGUILayout.Slider(new GUIContent("Sink amount", "Lowers the Y position of the tree"), tree.sinkAmount, 0f, 1f);

                            EditorGUILayout.Space();
                            tree.collisionCheck = EditorGUILayout.Toggle("Collision check", tree.collisionCheck);

                            tree.rejectUnderwater = EditorGUILayout.Toggle(new GUIContent("Remove underwater", "The water height level can be set in the settings tab"), tree.rejectUnderwater);
                            VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Height range", "Min/max height this item can spawn at"), ref tree.heightRange, 0f, script.maxTerrainHeight);
                            VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Slope range", "Min/max slope (0-90 degrees) this item can spawn at"), ref tree.slopeRange, 0f, 90f);
                            VegetationSpawnerEditor.DrawRangeSlider(new GUIContent("Curvature range", "0=Concave (bowl), 0.5=flat, 1=convex (edge)"), ref tree.curvatureRange, 0f, 1f);

                            if (EditorGUI.EndChangeCheck())
                            {
                                if (!autoRespawnTrees.boolValue)
                                {
                                    return;
                                }

                                Stopwatch sw = new Stopwatch();
                                sw.Restart();
                                script.SpawnTree(tree);
                                sw.Stop();

                                Log.Add("Respawning tree: " + sw.Elapsed.Milliseconds + "ms...");

                                EditorUtility.SetDirty(target);
                            }

                            LayerMaskSettings(tree.layerMasks);

                            EditorGUILayout.LabelField("Instances: " + tree.instanceCount.ToString("##,#"), EditorStyles.miniLabel);

                            if (autoRespawnTrees.boolValue == false)
                            {
                                EditorGUILayout.HelpBox("Auto respawning is disabled for trees in the settings tab", MessageType.Warning);
                            }

                            if (GUILayout.Button(new GUIContent(" Respawn", EditorGUIUtility.IconContent("d_Refresh").image), GUILayout.Height(30f)))
                            {
                                Stopwatch sw = new Stopwatch();
                                sw.Restart();
                                script.SpawnTree(tree);
                                sw.Stop();

                                Log.Add("Respawning tree: " + sw.Elapsed.Milliseconds + "ms...");
                            }
                        }
                    }
                }

                if (treeChange.changed)
                {
                    EditorUtility.SetDirty(target);
                    serializedObject.ApplyModifiedProperties();
                }
            }
        }