Esempio n. 1
0
        void SpawnObjects()
        {
            // To apply a proper scale, get as a reference the length of a diagonal in tile 0 (note the "false" argument which specifies the position is in local coordinates)
            float   size  = Vector3.Distance(hexa.GetTileVertexPosition(0, 0, false), hexa.GetTileVertexPosition(0, 3, false));
            Vector3 scale = new Vector3(size, size, size);

            // Make it 50% smaller so it does not occupy entire tile
            scale *= 0.5f;

            // Spawn 50 objects
            for (int k = 0; k < 50; k++)
            {
                GameObject obj = Instantiate <GameObject> (spawnObject);

                // Move object to center of tile (GetTileCenter also takes into account extrusion)
                int tileIndex = Random.Range(0, hexa.tiles.Length);
                obj.transform.position = hexa.GetTileCenter(tileIndex);

                // Parent it to hexasphere, so it rotates along it
                obj.transform.SetParent(hexa.transform);

                // Align with surface
                obj.transform.LookAt(hexa.transform.position);

                // Set scale
                obj.transform.localScale = scale;

                // Set a random color (notice the use of material and not sharedMaterial so every cube can have a different color)
                obj.GetComponent <Renderer> ().material.color = new Color(Random.value, Random.value, Random.value);
            }
        }
        void OnSceneGUI()
        {
            if (hexa == null || Application.isPlaying || !hexa.enableGridEditor)
            {
                return;
            }
            Event e       = Event.current;
            bool  gridHit = hexa.CheckRay(HandleUtility.GUIPointToWorldRay(e.mousePosition));

            if (tileHighlightedIndex != hexa.lastHighlightedTileIndex)
            {
                tileHighlightedIndex = hexa.lastHighlightedTileIndex;
                SceneView.RepaintAll();
            }

            int count = tileSelectedIndices.Count;

            for (int k = 0; k < count; k++)
            {
                int     idx = tileSelectedIndices [k];
                Vector3 pos = hexa.GetTileCenter(idx);
                Handles.color = colorSelection;
                Handles.DrawSolidDisc(pos, hexa.tiles [idx].center, HandleUtility.GetHandleSize(pos) * 0.075f);
            }

            if (tileHighlightedIndex < 0)
            {
                return;
            }

            bool redraw = false;

            if ((e.type == EventType.MouseDown && e.isMouse && e.button == 0) || e.shift)
            {
                if (gridHit && e.type == EventType.MouseDown)
                {
                    e.Use();
                }
                if (!e.shift && tileSelectedIndices.Contains(tileHighlightedIndex))
                {
                    tileSelectedIndices.Remove(tileHighlightedIndex);
                }
                else
                {
                    if (!e.control)
                    {
                        tileSelectedIndices.Clear();
                    }
                    tileSelectedIndices.Add(tileHighlightedIndex);

                    if (textureMode > 0)
                    {
                        hexa.SetTileTexture(tileHighlightedIndex, textureMode, Color.white);
                        redraw = true;
                    }
                    if (!e.shift)
                    {
                        tileColor = hexa.GetTileColor(tileHighlightedIndex);
                        if (tileColor.a == 0)
                        {
                            tileColor = Color.white;
                        }
                        tileTextureIndex = hexa.GetTileTextureIndex(tileHighlightedIndex);
                    }
                    tileTag    = hexa.GetTileTag(tileHighlightedIndex);
                    tileTagInt = hexa.GetTileTagInt(tileHighlightedIndex);
                    EditorUtility.SetDirty(target);
                }
            }

            if (e.shift)
            {
                if (e.keyCode == KeyCode.S)
                {
                    if (tileTextureIndex == 0)
                    {
                        hexa.SetTileColor(tileHighlightedIndex, tileColor);
                    }
                    else
                    {
                        hexa.SetTileTexture(tileHighlightedIndex, tileTextureIndex, tileColor, false);
                    }
                    redraw = true;
                    e.Use();
                }
                else if (e.keyCode == KeyCode.C)
                {
                    hexa.ClearTile(tileHighlightedIndex);
                    redraw = true;
                    e.Use();
                }
            }

            if (redraw)
            {
                SceneView.RepaintAll();
            }

            if (gridHit)
            {
                Selection.activeGameObject = hexa.transform.gameObject;
            }
        }