コード例 #1
0
    // This function takes a Unit's UnitScript, makes it selected, and deselects any other units that were selected.
    // If null is passed in, it will just deselect everything.
    // This function also populates the nameText UI element with the currently selected unit's name, and also ensures
    // that the namePanel UI element is only active is a unit is selected.
    public void SelectUnit(GameObject SelectGameobject)
    {
        // Get an array of all GameObjects that have the tag "Unit".
        GameObject[] units = GameObject.FindGameObjectsWithTag("Unit");
        // Loop through all units and make sure they are not selected.
        if (units.Length > 0)
        {
            for (int i = 0; i < units.Length; i++)
            {
                UnitScript unitScript = units[i].GetComponent <UnitScript>();
                unitScript.selected = false;
                unitScript.UpdateVisuals();
            }
        }

        GameObject baseObj    = GameObject.FindGameObjectWithTag("Base");
        BaseScript baseScript = baseObj.GetComponent <BaseScript>();

        baseScript.selected = false;
        baseScript.UpdateVisuals();

        GameObject[] BulletFactorys = GameObject.FindGameObjectsWithTag("BulletFactory");
        if (BulletFactorys.Length > 0)
        {
            for (int i = 0; i < BulletFactorys.Length; i++)
            {
                BulletFactoryScript BulletFactoryScript = BulletFactorys[i].GetComponent <BulletFactoryScript>();
                BulletFactoryScript.selected = false;
                BulletFactoryScript.UpdateVisuals();
            }
        }

        GameObject[] LaserFactorys = GameObject.FindGameObjectsWithTag("LaserFactory");
        if (LaserFactorys.Length > 0)
        {
            for (int i = 0; i < LaserFactorys.Length; i++)
            {
                LaserFactoryScript LaserFactoryScript = LaserFactorys[i].GetComponent <LaserFactoryScript>();
                LaserFactoryScript.selected = false;
                LaserFactoryScript.UpdateVisuals();
            }
        }

        GameObject[] Enginers = GameObject.FindGameObjectsWithTag("Enginer");
        if (Enginers.Length > 0)
        {
            for (int i = 0; i < Enginers.Length; i++)
            {
                EnginerScript EnginerScript = Enginers[i].GetComponent <EnginerScript>();
                EnginerScript.selected = false;
                EnginerScript.UpdateVisuals();
            }
        }

        GameObject[] Miners = GameObject.FindGameObjectsWithTag("Miner");
        if (Miners.Length > 0)
        {
            for (int i = 0; i < Miners.Length; i++)
            {
                MinerScript MinerScript = Miners[i].GetComponent <MinerScript>();
                MinerScript.selected = false;
                MinerScript.UpdateVisuals();
            }
        }

        GameObject[] GoldMines = GameObject.FindGameObjectsWithTag("GoldMine");
        if (GoldMines.Length > 0)
        {
            for (int i = 0; i < GoldMines.Length; i++)
            {
                GoldMineScript GoldMineScript = GoldMines[i].GetComponent <GoldMineScript>();
                GoldMineScript.selected = false;
            }
        }

        if (SelectGameobject.tag == "Unit")
        {
            SelectUnitObj = SelectGameobject;
            selectedUnit  = SelectGameobject.GetComponent <UnitScript>();

            if (SelectGameobject != null)
            {
                // If there is a selected, mark it as selected, update its visuals, and update the UI elements.
                selectedUnit.selected = true;

                UpdateUI(selectedUnit);

                selectedUnit.UpdateVisuals();
            }
            else
            {
                // If we get in here, it means that the toSelect parameter was null, and that means that we
                // should deactivate the namePanel.
                namePanel.SetActive(false);
            }
        }

        if (SelectGameobject.tag == "Base")
        {
            BaseScript BaseScript = SelectGameobject.GetComponent <BaseScript>();

            if (SelectGameobject != null)
            {
                // If there is a selected, mark it as selected, update its visuals, and update the UI elements.
                BaseScript.selected = true;

                UpdateBaseUI(BaseScript);

                BaseScript.UpdateVisuals();
            }
            else
            {
                // If we get in here, it means that the toSelect parameter was null, and that means that we
                // should deactivate the namePanel.
                namePanel.SetActive(false);
            }
        }

        if (SelectGameobject.tag == "BulletFactory")
        {
            BulletFactoryScript BulletFactoryScript = SelectGameobject.GetComponent <BulletFactoryScript>();

            if (SelectGameobject != null)
            {
                // If there is a selected, mark it as selected, update its visuals, and update the UI elements.
                BulletFactoryScript.selected = true;

                UpdateBulletFactoryUI(BulletFactoryScript);

                BulletFactoryScript.UpdateVisuals();
            }
            else
            {
                // If we get in here, it means that the toSelect parameter was null, and that means that we
                // should deactivate the namePanel.
                namePanel.SetActive(false);
            }
        }

        if (SelectGameobject.tag == "LaserFactory")
        {
            LaserFactoryScript LaserFactoryScript = SelectGameobject.GetComponent <LaserFactoryScript>();

            if (SelectGameobject != null)
            {
                // If there is a selected, mark it as selected, update its visuals, and update the UI elements.
                LaserFactoryScript.selected = true;

                UpdateLaserFactoryUI(LaserFactoryScript);

                LaserFactoryScript.UpdateVisuals();
            }
            else
            {
                // If we get in here, it means that the toSelect parameter was null, and that means that we
                // should deactivate the namePanel.
                namePanel.SetActive(false);
            }
        }

        if (SelectGameobject.tag == "Enginer")
        {
            EnginerScript EnginerScript = SelectGameobject.GetComponent <EnginerScript>();

            if (SelectGameobject != null)
            {
                // If there is a selected, mark it as selected, update its visuals, and update the UI elements.
                EnginerScript.selected = true;

                UpdateEnginerUI(EnginerScript);

                EnginerScript.UpdateVisuals();
            }
            else
            {
                // If we get in here, it means that the toSelect parameter was null, and that means that we
                // should deactivate the namePanel.
                namePanel.SetActive(false);
            }
        }

        if (SelectGameobject.tag == "Miner")
        {
            MinerScript MinerScript = SelectGameobject.GetComponent <MinerScript>();

            if (SelectGameobject != null)
            {
                // If there is a selected, mark it as selected, update its visuals, and update the UI elements.
                MinerScript.selected = true;

                UpdateMinerUI(MinerScript);

                MinerScript.UpdateVisuals();
            }
            else
            {
                // If we get in here, it means that the toSelect parameter was null, and that means that we
                // should deactivate the namePanel.
                namePanel.SetActive(false);
            }
        }

        if (SelectGameobject.tag == "GoldMine")
        {
            SelectGoldMine = SelectGameobject;
            GoldMineScript GoldMineScript = SelectGameobject.GetComponent <GoldMineScript>();

            if (SelectGameobject != null)
            {
                // If there is a selected, mark it as selected, update its visuals, and update the UI elements.
                GoldMineScript.selected = true;

                UpdateGoldMineUI(GoldMineScript);

                //GoldMineScript.UpdateVisuals();
            }
            else
            {
                // If we get in here, it means that the toSelect parameter was null, and that means that we
                // should deactivate the namePanel.
                namePanel.SetActive(false);
            }
        }
    }