// WPFMA-SDK needs Unity 2022.3 (VRChat's version); on older Unity this file is skipped and WPFMAUnityVersionNotice explains why.
#if UNITY_2022_3_OR_NEWER
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace WPFMA
{
    internal static class WPFMAPlacementSection
    {
        private const string PrefKey = "WPFMA.Tools.PlacementExpanded";

        // Shared with the Scene view panel (WPFMAPlacementOverlay), so both use the same settings.
        internal static bool alignToSurface;
        internal static int scatterCount = 20;
        internal static float scatterRadius = 8f;
        internal static Vector2 scatterScale = new Vector2(0.85f, 1.2f);
        internal static bool scatterAlign;
        internal static int copiesX = 4;
        internal static int copiesZ = 1;
        internal static float spacingX = 2f;
        internal static float spacingZ = 2f;
        internal static float randomTurn = 360f;
        internal static Vector2 randomScale = new Vector2(0.9f, 1.1f);
        internal static string status;

        internal static void Draw(GUIStyle sectionStyle)
        {
            int count = Selected().Length;
            WPFMAToolSection.Draw(sectionStyle, "Placement tools", count == 0 ? "nothing selected" : count + " selected", PrefKey, Body);
        }

        internal static GameObject[] Selected()
        {
            return Selection.gameObjects.Where(g => !EditorUtility.IsPersistent(g)).ToArray();
        }

        private static void Body()
        {
            EditorGUILayout.LabelField(new GUIContent("Drop, scatter and lay out props.", "Drop things onto the floor, scatter props over the ground, and lay out rows and grids of copies."), WPFMAToolSection.Hint(WPFMAToolSection.MetaText));
            var selected = Selected();
            WPFMAToolDefaults.WorldPlacementOverlay = EditorGUILayout.ToggleLeft(new GUIContent("Panel in the Scene view",
                "Shows these buttons in the Scene view while something is selected, like the Particle System's panel."), WPFMAToolDefaults.WorldPlacementOverlay);

            EditorGUILayout.Space(4f);
            EditorGUILayout.LabelField("Drop to floor", EditorStyles.boldLabel);
            alignToSurface = EditorGUILayout.ToggleLeft("Tilt to match the surface", alignToSurface);
            using (new EditorGUI.DisabledScope(selected.Length == 0))
                if (GUILayout.Button("Drop " + selected.Length + " selected"))
                    EditorApplication.delayCall += () => status = WPFMAChangeLog.Run("Placement tools", "Drop " + selected.Length + " object(s) to the floor", null, true, () => Drop(selected, alignToSurface));

            EditorGUILayout.Space(6f);
            EditorGUILayout.LabelField("Scatter copies", EditorStyles.boldLabel);
            scatterCount = EditorGUILayout.IntSlider("How many", scatterCount, 1, 300);
            scatterRadius = EditorGUILayout.Slider("Area radius (m)", scatterRadius, 0.5f, 100f);
            EditorGUILayout.MinMaxSlider("Size range", ref scatterScale.x, ref scatterScale.y, 0.2f, 3f);
            EditorGUILayout.LabelField("Size " + scatterScale.x.ToString("0.00") + " - " + scatterScale.y.ToString("0.00"), WPFMAToolSection.Hint(WPFMAToolSection.FaintText));
            scatterAlign = EditorGUILayout.ToggleLeft("Tilt to match the ground", scatterAlign);
            using (new EditorGUI.DisabledScope(selected.Length == 0))
                if (GUILayout.Button(selected.Length == 0 ? "Select a prop to scatter" : "Scatter copies of '" + selected[0].name + "' around the Scene view"))
                {
                    var source = selected[0];
                    EditorApplication.delayCall += () => status = WPFMAChangeLog.Run("Placement tools", "Scatter copies of '" + source.name + "'", null, true, () => Scatter(source));
                }

            EditorGUILayout.Space(6f);
            EditorGUILayout.LabelField("Row / grid of copies", EditorStyles.boldLabel);
            float labelWidth = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = 60f;
            using (new EditorGUILayout.HorizontalScope())
            {
                copiesX = EditorGUILayout.IntField("Across", copiesX, GUILayout.MinWidth(90f));
                spacingX = EditorGUILayout.FloatField("every (m)", spacingX, GUILayout.MinWidth(90f));
            }
            using (new EditorGUILayout.HorizontalScope())
            {
                copiesZ = EditorGUILayout.IntField("Back", copiesZ, GUILayout.MinWidth(90f));
                spacingZ = EditorGUILayout.FloatField("every (m)", spacingZ, GUILayout.MinWidth(90f));
            }
            EditorGUIUtility.labelWidth = labelWidth;
            copiesX = Mathf.Clamp(copiesX, 1, 100);
            copiesZ = Mathf.Clamp(copiesZ, 1, 100);
            using (new EditorGUI.DisabledScope(selected.Length != 1 || copiesX * copiesZ < 2))
                if (GUILayout.Button(selected.Length == 1 ? "Make a " + copiesX + " x " + copiesZ + " grid of '" + selected[0].name + "'" : "Select one object"))
                {
                    var source = selected[0];
                    EditorApplication.delayCall += () => status = WPFMAChangeLog.Run("Placement tools", "Grid of '" + source.name + "'", null, true, () => Grid(source));
                }

            EditorGUILayout.Space(6f);
            EditorGUILayout.LabelField("Variety", EditorStyles.boldLabel);
            randomTurn = EditorGUILayout.Slider("Random turn (degrees)", randomTurn, 0f, 360f);
            EditorGUILayout.MinMaxSlider("Random size", ref randomScale.x, ref randomScale.y, 0.2f, 3f);
            using (new EditorGUI.DisabledScope(selected.Length == 0))
                if (GUILayout.Button("Randomise " + selected.Length + " selected"))
                    EditorApplication.delayCall += () => status = WPFMAChangeLog.Run("Placement tools", "Randomise " + selected.Length + " object(s)", null, true, () => Randomise(selected));

            if (!string.IsNullOrEmpty(status))
                EditorGUILayout.LabelField(status, WPFMAToolSection.Hint(WPFMAToolSection.OkGreen));
        }

        /// <summary>Runs a placement action on the next editor tick, recorded in the Change log.</summary>
        internal static void Do(string action, Func<string> change)
        {
            EditorApplication.delayCall += () =>
            {
                status = WPFMAChangeLog.Run("Placement tools", action, null, true, change);
                SceneView.RepaintAll();
            };
        }

        internal static string Turn(GameObject[] objects, float degrees)
        {
            Undo.RecordObjects(objects.Select(o => (UnityEngine.Object)o.transform).ToArray(), "Turn");
            foreach (var go in objects)
                go.transform.rotation = Quaternion.AngleAxis(degrees, Vector3.up) * go.transform.rotation;
            return "Turned " + objects.Length + " object(s) " + degrees.ToString("0") + " degrees.";
        }

        /// <summary>The Scene view panel: the same tools, small, with the settings above.</summary>
        internal static void DrawCompact()
        {
            var selected = Selected();
            float labelWidth = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = 62f;
            var mini = EditorStyles.miniButton;

            GUILayout.Label(selected.Length + " selected", EditorStyles.miniLabel);

            using (new EditorGUILayout.HorizontalScope())
            {
                if (GUILayout.Button(new GUIContent("Drop", "Drop the selected objects onto whatever is under them."), mini, GUILayout.Width(70f)))
                    Do("Drop " + selected.Length + " object(s) to the floor", () => Drop(selected, alignToSurface));
                alignToSurface = GUILayout.Toggle(alignToSurface, new GUIContent("Tilt", "Tilt to match the surface."), GUILayout.Width(50f));
            }

            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label("Turn", GUILayout.Width(44f));
                if (GUILayout.Button("-90", mini, GUILayout.Width(40f)))
                    Do("Turn " + selected.Length + " object(s)", () => Turn(selected, -90f));
                if (GUILayout.Button("+90", mini, GUILayout.Width(40f)))
                    Do("Turn " + selected.Length + " object(s)", () => Turn(selected, 90f));
                if (GUILayout.Button(new GUIContent("Random", "Random turn and size, from the ranges in Placement tools."), mini, GUILayout.Width(62f)))
                    Do("Randomise " + selected.Length + " object(s)", () => Randomise(selected));
            }

            GUILayout.Space(4f);
            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label(new GUIContent("Scatter", "How many copies, and how far around the Scene view's centre."), GUILayout.Width(44f));
                scatterCount = Mathf.Clamp(EditorGUILayout.IntField(scatterCount, GUILayout.Width(40f)), 1, 300);
                GUILayout.Label("within", GUILayout.Width(40f));
                scatterRadius = Mathf.Clamp(EditorGUILayout.FloatField(scatterRadius, GUILayout.Width(40f)), 0.5f, 100f);
                GUILayout.Label("m", GUILayout.Width(16f));
            }
            if (GUILayout.Button(new GUIContent("Scatter copies", "Copies of the first selected object, around where the Scene view looks."), mini))
            {
                var source = selected[0];
                Do("Scatter copies of '" + source.name + "'", () => Scatter(source));
            }

            using (new EditorGUILayout.HorizontalScope())
            {
                GUILayout.Label(new GUIContent("Grid", "Copies across x back, and the space between them."), GUILayout.Width(44f));
                copiesX = Mathf.Clamp(EditorGUILayout.IntField(copiesX, GUILayout.Width(30f)), 1, 100);
                GUILayout.Label("x", GUILayout.Width(10f));
                copiesZ = Mathf.Clamp(EditorGUILayout.IntField(copiesZ, GUILayout.Width(30f)), 1, 100);
                GUILayout.Label("every", GUILayout.Width(36f));
                // One spacing here; editing it sets both directions (the Placement tools panel can set them apart).
                float spacing = Mathf.Max(0.01f, EditorGUILayout.FloatField(spacingX, GUILayout.Width(34f)));
                GUILayout.Label("m", GUILayout.Width(16f));
                if (!Mathf.Approximately(spacing, spacingX))
                    spacingX = spacingZ = spacing;
            }
            using (new EditorGUI.DisabledScope(selected.Length != 1 || copiesX * copiesZ < 2))
            {
                if (GUILayout.Button(new GUIContent("Make grid", "A grid of copies of the selected object."), mini))
                {
                    var source = selected[0];
                    Do("Grid of '" + source.name + "'", () => Grid(source));
                }
            }

            GUILayout.Space(2f);
            if (GUILayout.Button(new GUIContent("More...", "Open Placement tools for every setting."), mini))
                WPFMAToolSection.Reveal(1, PrefKey);

            EditorGUIUtility.labelWidth = labelWidth;
        }

        private static bool FloorBelow(Vector3 from, GameObject ignore, out RaycastHit hit)
        {
            var hits = Physics.RaycastAll(from, Vector3.down, 500f)
                .Where(h => ignore == null || !h.collider.transform.IsChildOf(ignore.transform))
                .OrderBy(h => h.distance)
                .ToList();

            hit = hits.FirstOrDefault();
            return hits.Count > 0;
        }

        private static Bounds BoundsOf(GameObject go)
        {
            var renderers = go.GetComponentsInChildren<Renderer>();
            if (renderers.Length == 0)
                return new Bounds(go.transform.position, Vector3.zero);
            var bounds = renderers[0].bounds;
            foreach (var r in renderers.Skip(1))
                bounds.Encapsulate(r.bounds);
            return bounds;
        }

        internal static string Drop(GameObject[] objects, bool align)
        {
            int dropped = 0;
            foreach (var go in objects)
            {
                if (go == null)
                    continue;

                var bounds = BoundsOf(go);
                RaycastHit hit;
                if (!FloorBelow(new Vector3(bounds.center.x, bounds.min.y + 0.05f, bounds.center.z), go, out hit))
                    continue;

                Undo.RecordObject(go.transform, "Drop to floor");
                go.transform.position += Vector3.up * (hit.point.y - bounds.min.y);
                if (align)
                    go.transform.rotation = Quaternion.FromToRotation(go.transform.up, hit.normal) * go.transform.rotation;
                dropped++;
            }

            return "Dropped " + dropped + " object(s)" + (dropped < objects.Length ? "; the rest had nothing with a collider under them." : ".");
        }

        private static GameObject Copy(GameObject source)
        {
            var prefab = PrefabUtility.GetCorrespondingObjectFromSource(source);
            GameObject copy = prefab != null && PrefabUtility.IsAnyPrefabInstanceRoot(source)
                ? (GameObject)PrefabUtility.InstantiatePrefab(prefab, source.scene)
                : UnityEngine.Object.Instantiate(source);

            copy.name = source.name;
            copy.transform.rotation = source.transform.rotation;
            copy.transform.localScale = source.transform.localScale;
            return copy;
        }

        internal static string Scatter(GameObject source)
        {
            var center = WPFMAWorldTypes.PlacementPoint();
            var group = new GameObject("Scatter - " + source.name);
            Undo.RegisterCreatedObjectUndo(group, "Scatter");
            group.transform.position = center;

            var random = new System.Random();
            int placed = 0;

            for (int i = 0; i < scatterCount * 3 && placed < scatterCount; i++)
            {
                var offset = UnityEngine.Random.insideUnitCircle * scatterRadius;
                var top = new Vector3(center.x + offset.x, center.y + 60f, center.z + offset.y);

                RaycastHit hit;
                if (!FloorBelow(top, group, out hit))
                    continue;

                var copy = Copy(source);
                Undo.RegisterCreatedObjectUndo(copy, "Scatter");
                copy.transform.SetParent(group.transform, true);
                copy.transform.position = hit.point;
                copy.transform.rotation = Quaternion.Euler(0f, (float)random.NextDouble() * 360f, 0f) * source.transform.rotation;
                if (scatterAlign)
                    copy.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal) * copy.transform.rotation;
                float s = Mathf.Lerp(scatterScale.x, scatterScale.y, (float)random.NextDouble());
                copy.transform.localScale = source.transform.localScale * s;

                var bounds = BoundsOf(copy);
                copy.transform.position += Vector3.up * (hit.point.y - bounds.min.y);
                placed++;
            }

            Selection.activeGameObject = group;
            return placed == 0
                ? "Nothing to scatter onto - the area needs colliders on the ground."
                : "Scattered " + placed + " copies under '" + group.name + "'.";
        }

        internal static string Grid(GameObject source)
        {
            var right = source.transform.right;
            var forward = source.transform.forward;
            var group = new GameObject("Grid - " + source.name);
            Undo.RegisterCreatedObjectUndo(group, "Grid");
            group.transform.position = source.transform.position;

            int made = 0;
            for (int x = 0; x < copiesX; x++)
            {
                for (int z = 0; z < copiesZ; z++)
                {
                    if (x == 0 && z == 0)
                        continue;

                    var copy = Copy(source);
                    Undo.RegisterCreatedObjectUndo(copy, "Grid");
                    copy.transform.SetParent(group.transform, true);
                    copy.transform.position = source.transform.position + right * (x * spacingX) + forward * (z * spacingZ);
                    made++;
                }
            }

            Selection.activeGameObject = group;
            return "Made " + made + " copies under '" + group.name + "' (the original stays where it is).";
        }

        internal static string Randomise(GameObject[] objects)
        {
            var random = new System.Random();
            Undo.RecordObjects(objects.Select(o => (UnityEngine.Object)o.transform).ToArray(), "Randomise");
            foreach (var go in objects)
            {
                go.transform.rotation = Quaternion.AngleAxis(((float)random.NextDouble() - 0.5f) * randomTurn, Vector3.up) * go.transform.rotation;
                go.transform.localScale *= Mathf.Lerp(randomScale.x, randomScale.y, (float)random.NextDouble());
            }
            return "Randomised " + objects.Length + " object(s).";
        }
    }

    internal static class WPFMAColliderSection
    {
        private const string PrefKey = "WPFMA.Tools.ColliderExpanded";

        private static float smallSize = 0.4f;
        private static string status;
        private static List<GameObject> missing;

        internal static void Draw(GUIStyle sectionStyle)
        {
            WPFMAToolSection.Draw(sectionStyle, "Collider helper", missing == null ? "not scanned yet" : missing.Count + " without colliders", PrefKey, Body);
        }

        private static void Body()
        {
            EditorGUILayout.LabelField(new GUIContent("Floors to stand on, no snagging.", "Makes sure players have something to stand on, and that small decorations do not snag them."), WPFMAToolSection.Hint(WPFMAToolSection.MetaText));
            EditorGUILayout.Space(4f);

            EditorGUILayout.LabelField("Floors and walls", EditorStyles.boldLabel);
            if (GUILayout.Button("Find static objects with no collider"))
                EditorApplication.delayCall += () => missing = FindMissing();

            if (missing != null)
            {
                int large = missing.Count(g => IsLarge(g));
                EditorGUILayout.LabelField(missing.Count + " static object(s) have no collider, " + large + " of them big enough to be floors or walls.",
                    WPFMAToolSection.Hint(large > 0 ? WPFMAToolSection.WarnAmber : WPFMAToolSection.FaintText));

                using (new EditorGUILayout.HorizontalScope())
                {
                    using (new EditorGUI.DisabledScope(missing.Count == 0))
                    {
                        if (GUILayout.Button("Select them"))
                            Selection.objects = missing.Where(g => g != null).Cast<UnityEngine.Object>().ToArray();
                        if (GUILayout.Button("Add mesh colliders to the big ones"))
                        {
                            var targets = missing.Where(g => g != null && IsLarge(g)).ToArray();
                            EditorApplication.delayCall += () => status = WPFMAChangeLog.Run("Collider helper", "Add colliders to big static objects", null, true, () => AddColliders(targets));
                        }
                    }
                }
            }

            var selected = Selection.gameObjects.Where(g => !EditorUtility.IsPersistent(g)).ToArray();
            using (new EditorGUI.DisabledScope(selected.Length == 0))
                if (GUILayout.Button("Add mesh colliders to the selected objects"))
                    EditorApplication.delayCall += () => status = WPFMAChangeLog.Run("Collider helper", "Add colliders to selected", null, true, () => AddColliders(selected.SelectMany(g => g.GetComponentsInChildren<MeshFilter>()).Select(f => f.gameObject).Distinct().ToArray()));

            EditorGUILayout.Space(6f);
            EditorGUILayout.LabelField("Walk-through decorations", EditorStyles.boldLabel);
            smallSize = EditorGUILayout.Slider("Smaller than (m)", smallSize, 0.1f, 2f);
            using (new EditorGUILayout.HorizontalScope())
            {
                if (GUILayout.Button("Select small props with colliders"))
                    EditorApplication.delayCall += SelectSmall;
                using (new EditorGUI.DisabledScope(selected.Length == 0 || LayerMask.NameToLayer("Walkthrough") < 0))
                    if (GUILayout.Button("Make selected walk-through"))
                        EditorApplication.delayCall += () => status = WPFMAChangeLog.Run("Collider helper", "Make " + selected.Length + " object(s) walk-through", null, true, () => WalkThrough(selected));
            }
            EditorGUILayout.LabelField(LayerMask.NameToLayer("Walkthrough") < 0
                    ? "The 'Walkthrough' layer is missing - set up VRChat's layers from the VRChat SDK Builder tab first."
                    : "Walk-through objects stay solid to pickups but players pass through them.",
                WPFMAToolSection.Hint(WPFMAToolSection.FaintText));

            if (!string.IsNullOrEmpty(status))
                EditorGUILayout.LabelField(status, WPFMAToolSection.Hint(WPFMAToolSection.OkGreen));
        }

        private static bool IsLarge(GameObject go)
        {
            var renderer = go != null ? go.GetComponent<Renderer>() : null;
            if (renderer == null)
                return false;
            var size = renderer.bounds.size;
            return Mathf.Max(size.x, size.z) >= 1f || size.y >= 2f;
        }

        private static List<GameObject> FindMissing()
        {
            return UnityEngine.Object.FindObjectsOfType<MeshRenderer>()
                .Select(r => r.gameObject)
                .Where(g => g.scene.IsValid() && !g.CompareTag("EditorOnly"))
                .Where(g => GameObjectUtility.GetStaticEditorFlags(g) != 0)
                .Where(g => g.GetComponent<Collider>() == null)
                .ToList();
        }

        private static string AddColliders(GameObject[] objects)
        {
            int added = 0;
            foreach (var go in objects)
            {
                if (go == null || go.GetComponent<Collider>() != null || go.GetComponent<MeshFilter>() == null)
                    continue;
                Undo.AddComponent<MeshCollider>(go);
                added++;
            }
            missing = null;
            return "Added " + added + " mesh collider(s).";
        }

        private static void SelectSmall()
        {
            var small = UnityEngine.Object.FindObjectsOfType<Collider>()
                .Where(c => !c.isTrigger && c.gameObject.scene.IsValid())
                .Where(c => c.bounds.size.x < smallSize && c.bounds.size.y < smallSize * 2f && c.bounds.size.z < smallSize)
                .Where(c => c.GetComponent<Rigidbody>() == null)
                .Select(c => (UnityEngine.Object)c.gameObject)
                .Distinct()
                .ToArray();

            Selection.objects = small;
            status = small.Length == 0 ? "No small props with colliders." : "Selected " + small.Length + " small prop(s). Check them, then make them walk-through.";
        }

        private static string WalkThrough(GameObject[] objects)
        {
            int layer = LayerMask.NameToLayer("Walkthrough");
            Undo.RecordObjects(objects.Cast<UnityEngine.Object>().ToArray(), "Walk-through");
            foreach (var go in objects)
                go.layer = layer;
            return "Moved " + objects.Length + " object(s) to the Walkthrough layer.";
        }
    }

    internal static class WPFMAPlayerGuideSection
    {
        private const string PrefKey = "WPFMA.Tools.PlayerGuideExpanded";

        private static string status;

        internal static void Draw(GUIStyle sectionStyle)
        {
            WPFMAToolSection.Draw(sectionStyle, "Player-size guide", "check doors and ceilings", PrefKey, Body);
        }

        private static void Body()
        {
            EditorGUILayout.LabelField(new GUIContent("Player-size figures for scale.", "Drops see-through people (1 m, 1.75 m and 2.5 m tall) where the Scene view is looking, so you can check " + "doorways, ceilings and stairs. They are EditorOnly, so they never reach the upload."), WPFMAToolSection.Hint(WPFMAToolSection.MetaText));
            EditorGUILayout.Space(4f);

            if (GUILayout.Button("Add player-size guides", GUILayout.Height(24f)))
                EditorApplication.delayCall += () => WPFMAChangeLog.Run("Player-size guide", "Add player-size guides", null, true, () => { Add(); return status; });

            EditorGUILayout.LabelField(new GUIContent("Press Play to walk around.", "Press Play to walk around for real - the world SDK's ClientSim runs automatically."), WPFMAToolSection.Hint(WPFMAToolSection.FaintText));

            if (!string.IsNullOrEmpty(status))
                EditorGUILayout.LabelField(status, WPFMAToolSection.Hint(WPFMAToolSection.OkGreen));
        }

        private static void Add()
        {
            var root = new GameObject("Player Size Guides") { tag = "EditorOnly" };
            Undo.RegisterCreatedObjectUndo(root, "Add player guides");
            root.transform.position = WPFMAWorldTypes.PlacementPoint();

            var material = new Material(Shader.Find("Sprites/Default")) { color = new Color(0.45f, 0.75f, 1f, 0.35f), name = "Player Guide" };
            float[] heights = { 1f, 1.75f, 2.5f };

            for (int i = 0; i < heights.Length; i++)
            {
                var capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
                capsule.name = heights[i].ToString("0.##") + " m";
                capsule.tag = "EditorOnly";
                UnityEngine.Object.DestroyImmediate(capsule.GetComponent<Collider>());
                capsule.transform.SetParent(root.transform, false);
                capsule.transform.localPosition = new Vector3((i - 1) * 0.9f, heights[i] * 0.5f, 0f);
                capsule.transform.localScale = new Vector3(0.45f, heights[i] * 0.5f, 0.45f);
                var renderer = capsule.GetComponent<MeshRenderer>();
                renderer.sharedMaterial = material;
                renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;

                var label = new GameObject("Label") { tag = "EditorOnly" };
                label.transform.SetParent(capsule.transform, false);
                label.transform.localPosition = new Vector3(0f, 1.15f, 0f);
                label.transform.localScale = new Vector3(1f / 0.45f, 1f / (heights[i] * 0.5f), 1f / 0.45f) * 0.05f;
                var text = label.AddComponent<TextMesh>();
                text.text = heights[i].ToString("0.##") + " m";
                text.anchor = TextAnchor.LowerCenter;
                text.fontSize = 48;
                text.color = Color.white;
            }

            Selection.activeGameObject = root;
            status = "Added the guides. Delete them any time - they are left out of the upload anyway.";
        }
    }
}
#endif
