void ShowUpgradeTreePrototypeScaleUI(Terrain terrain)
 {
     if (terrain.terrainData != null && terrain.terrainData.NeedUpgradeScaledTreePrototypes())
     {
         var msgContent = EditorGUIUtility.TempContent(
             "Some of your prototypes have scaling values on the prefab. Since Unity 5.2 these scalings will be applied to terrain tree instances. Do you want to upgrade to this behaviour?",
             EditorGUIUtility.GetHelpIcon(MessageType.Warning));
         GUILayout.BeginVertical(EditorStyles.helpBox);
         GUILayout.Label(msgContent, EditorStyles.wordWrappedLabel);
         GUILayout.Space(3);
         if (GUILayout.Button("Upgrade", GUILayout.ExpandWidth(false)))
         {
             terrain.terrainData.UpgradeScaledTreePrototype();
             TerrainMenus.RefreshPrototypes();
         }
         GUILayout.Space(3);
         GUILayout.EndVertical();
     }
 }
        public override void OnInspectorGUI(Terrain terrain, IOnInspectorGUI editContext)
        {
            LoadTreeIcons(terrain);

            // Tree picker
            GUI.changed = false;

            ShowUpgradeTreePrototypeScaleUI(terrain);

            GUILayout.Label(Styles.trees, EditorStyles.boldLabel);
            selectedTree = TerrainInspector.AspectSelectionGridImageAndText(selectedTree, m_TreeContents, 64, Styles.noTreesDefined, out var doubleClick);

            if (selectedTree >= m_TreeContents.Length)
            {
                selectedTree = PaintTreesTool.kInvalidTree;
            }

            if (doubleClick)
            {
                TerrainTreeContextMenus.EditTree(new MenuCommand(terrain, selectedTree));
                GUIUtility.ExitGUI();
            }

            GUILayout.BeginHorizontal();
            using (new EditorGUI.DisabledScope(selectedTree == PaintTreesTool.kInvalidTree))
            {
                if (GUILayout.Button(Styles.massPlaceTrees))
                {
                    TerrainMenus.MassPlaceTrees();
                }
            }
            GUILayout.FlexibleSpace();
            TerrainInspector.MenuButton(Styles.editTrees, "CONTEXT/TerrainEngineTrees", terrain, selectedTree);
            TerrainInspector.ShowRefreshPrototypes();
            GUILayout.EndHorizontal();

            GUILayout.Label(TerrainInspector.styles.settings, EditorStyles.boldLabel);
            // Placement distance
            brushSize = TerrainInspectorUtility.PowerSlider(TerrainInspector.styles.brushSize, brushSize, 1, Mathf.Min(terrain.terrainData.size.x, terrain.terrainData.size.z), 4.0f);
            float oldDens = (3.3f - spacing) / 3f;
            float newDens = TerrainInspectorUtility.ScaledSliderWithRounding(Styles.treeDensity, oldDens, 0.1f, 1.0f, 100.0f, 1.0f);

            // Only set spacing when value actually changes. Otherwise
            // it will lose precision because we're constantly doing math
            // back and forth with it.
            if (newDens != oldDens)
            {
                spacing = (1.1f - newDens) * 3f;
            }

            GUILayout.Space(5);

            GUILayout.BeginHorizontal();
            GUILayout.Label(Styles.treeHeight, GUILayout.Width(EditorGUIUtility.labelWidth - 6));
            GUILayout.Label(Styles.treeHeightRandomLabel, GUILayout.ExpandWidth(false));
            allowHeightVar = GUILayout.Toggle(allowHeightVar, Styles.treeHeightRandomToggle, GUILayout.ExpandWidth(false));
            if (allowHeightVar)
            {
                EditorGUI.BeginChangeCheck();
                float min = treeHeight * (1.0f - treeHeightVariation);
                float max = treeHeight * (1.0f + treeHeightVariation);
                EditorGUILayout.MinMaxSlider(ref min, ref max, 0.01f, 2.0f);
                if (EditorGUI.EndChangeCheck())
                {
                    treeHeight          = (min + max) * 0.5f;
                    treeHeightVariation = (max - min) / (min + max);
                }
            }
            else
            {
                treeHeight          = EditorGUILayout.Slider(treeHeight, 0.01f, 2.0f);
                treeHeightVariation = 0.0f;
            }
            GUILayout.EndHorizontal();

            GUILayout.Space(5);

            lockWidthToHeight = EditorGUILayout.Toggle(Styles.lockWidthToHeight, lockWidthToHeight);

            GUILayout.Space(5);

            using (new EditorGUI.DisabledScope(lockWidthToHeight))
            {
                GUILayout.BeginHorizontal();
                GUILayout.Label(Styles.treeWidth, GUILayout.Width(EditorGUIUtility.labelWidth - 6));
                GUILayout.Label(Styles.treeWidthRandomLabel, GUILayout.ExpandWidth(false));
                allowWidthVar = GUILayout.Toggle(allowWidthVar, Styles.treeWidthRandomToggle, GUILayout.ExpandWidth(false));
                if (allowWidthVar)
                {
                    EditorGUI.BeginChangeCheck();
                    float min = treeWidth * (1.0f - treeWidthVariation);
                    float max = treeWidth * (1.0f + treeWidthVariation);
                    EditorGUILayout.MinMaxSlider(ref min, ref max, 0.01f, 2.0f);
                    if (EditorGUI.EndChangeCheck())
                    {
                        treeWidth          = (min + max) * 0.5f;
                        treeWidthVariation = (max - min) / (min + max);
                    }
                }
                else
                {
                    treeWidth          = EditorGUILayout.Slider(treeWidth, 0.01f, 2.0f);
                    treeWidthVariation = 0.0f;
                }
                GUILayout.EndHorizontal();
            }

            if (selectedTree == PaintTreesTool.kInvalidTree)
            {
                return;
            }

            GUILayout.Space(5);

            GameObject prefab = terrain.terrainData.treePrototypes[selectedTree].m_Prefab;
            string     treePrototypeWarning;

            terrain.terrainData.treePrototypes[selectedTree].Validate(out treePrototypeWarning);
            bool isLodTreePrototype = TerrainEditorUtility.IsLODTreePrototype(prefab);

            using (new EditorGUI.DisabledScope(!isLodTreePrototype))
            {
                randomRotation = EditorGUILayout.Toggle(Styles.treeRotation, randomRotation);
            }

            if (!isLodTreePrototype)
            {
                EditorGUILayout.HelpBox(Styles.treeRotationDisabled.text, MessageType.Info);
            }

            if (!string.IsNullOrEmpty(treePrototypeWarning))
            {
                EditorGUILayout.HelpBox(treePrototypeWarning, MessageType.Warning);
            }

            // TODO: we should check if the shaders assigned to this 'tree' support _TreeInstanceColor or not..  complicated check though
            treeColorAdjustment = EditorGUILayout.Slider(Styles.treeColorVar, treeColorAdjustment, 0, 1);

            if (prefab != null)
            {
                StaticEditorFlags staticEditorFlags = GameObjectUtility.GetStaticEditorFlags(prefab);
                bool contributeGI = (staticEditorFlags & StaticEditorFlags.ContributeGI) != 0;
                using (new EditorGUI.DisabledScope(true))   // Always disabled, because we don't want to edit the prefab.
                    contributeGI = EditorGUILayout.Toggle(Styles.treeContributeGI, contributeGI);
            }
        }