コード例 #1
0
ファイル: UIManager.cs プロジェクト: alexmiloeski/med-td
    /// <summary> Shows the tower menu (see <see cref="towerMenuPrefab"/>) at the same
    /// screen position as the parameter LymphNode <paramref name="lymphNode"/>. Called
    /// by a LymphNode object when it is clicked with a tower on it. </summary>
    internal GameObject ShowTowerMenu(Transform lymphNode, Tower tower)
    {
        GameObject towerMenu = Instantiate(towerMenuPrefab, new Vector3(0f, 0f, -1.2f), activeGameUIPanel.rotation);

        towerMenu.transform.SetParent(activeGameUIPanel, false);

        // if this tower is not upgradeable (i.e. the current tower...
        // ...level is the last one), don't show the "upgrade" button
        int currLevel = tower.GetCurrentLevel();
        //int maxLevel = tower.numberOfLevels;
        int  maxLevel    = tower.GetNumberOfLevels();
        bool upgradeable = currLevel < maxLevel;

        if (!upgradeable)
        {
            Transform buttonUpgradeTower = towerMenu.transform.Find(Constants.ButtonUpgradeTower);
            buttonUpgradeTower.gameObject.SetActive(false);
        }

        // if this is a melee tower, show the "set rally point" button
        if (tower as MeleeTower != null)
        {
            Transform buttonSetRallyPoint = towerMenu.transform.Find(Constants.ButtonSetRallyPoint);
            buttonSetRallyPoint.gameObject.SetActive(true);
        }


        /////////////////////////////////////

        // gather the cost data

        int sellValue   = -1;
        int upgradeCost = -1;

        sellValue   = tower.GetCurrentSellValue();
        upgradeCost = tower.GetNextLevelCost();

        ShopMenu shopMenu = towerMenu.GetComponent <ShopMenu>();

        shopMenu.SetValueSell(sellValue);
        shopMenu.SetCostUpgrade(upgradeCost);

        /////////////////////////////////////

        // UI elements and other scene objects use different coordinate systems;
        // in order to position the menu where the lymph node is (on the screen)...
        // ...we have to do some conversions between World and Viewport
        RectTransform towerMenuRT          = towerMenu.GetComponent <RectTransform>();
        RectTransform canvasRT             = activeGameUIPanel.parent.GetComponent <RectTransform>();                                           // this assumes that this panel is the child of the top-level UI panel
        Vector2       viewportPosition     = Camera.main.WorldToViewportPoint(lymphNode.position);
        Vector2       uiOffset             = new Vector2((float)canvasRT.sizeDelta.x / 2f, (float)canvasRT.sizeDelta.y / 2f);                   // screen offset for the canvas
        Vector2       proportionalPosition = new Vector2(viewportPosition.x * canvasRT.sizeDelta.x, viewportPosition.y * canvasRT.sizeDelta.y); // position on the canvas

        // set the position and remove the screen offset
        towerMenuRT.localPosition = proportionalPosition - uiOffset;

        return(towerMenu);
    }