Exemple #1
0
    private void Update()
    {
        //If the left mouse button is clicked.
        if (!EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
        {
            //Get the mouse position on the screen and send a raycast into the game world from that position.
            Vector2        worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            RaycastHit2D[] hit        = Physics2D.RaycastAll(worldPoint, Vector2.zero);

            //If something was hit, the RaycastHit2D.collider will not be null.
            if (hit.Length > 0)
            {
                for (int j = hit.Length - 1; j >= 0; j--)
                {
                    RaycastHit2D i = hit[j];
                    if (i.transform.gameObject.layer == LayerMask.NameToLayer("City"))
                    {
                        City city = UnityUtility.GetSafeComponent <City>(i.transform.parent.parent.gameObject);
                        cityUI.SetCity(city);
                        SelectedMarket = city;
                    }
                    if (i.transform.gameObject.layer == LayerMask.NameToLayer("Unit"))
                    {
                        Unit unit = UnityUtility.GetSafeComponent <Unit>(i.transform.gameObject);
                        unitUI.SetUnit(unit);
                    }
                }
            }
        }
    }