示例#1
0
        private void SetupPrefabsButtons()
        {
            // Find the prefabParent object and set the cellSize for the tile selection buttons
            _prefabParent = Utilities.FindGameObjectOrError("Prefabs");
            if (_prefabParent.GetComponent <GridLayoutGroup>() == null)
            {
                Debug.LogError("Make sure prefabParent has a GridLayoutGroup component");
            }
            // Counter to determine which tile button is pressed
            int tileCounter = 0;

            //Create a button for each tile in tiles
            foreach (Transform tile in _levelEditor.GetTiles())
            {
                int        index  = tileCounter;
                GameObject button = Instantiate(ButtonPrefab, Vector3.zero, Quaternion.identity);
                button.name = tile.name;
                button.GetComponent <Image>().sprite = tile.gameObject.GetComponent <SpriteRenderer>().sprite;
                button.transform.SetParent(_prefabParent.transform, false);
                button.transform.localScale = new Vector3(ButtonImageScale, ButtonImageScale, ButtonImageScale);
                // Add a click handler to the button
                button.GetComponent <Button>().onClick.AddListener(() => { _levelEditor.ButtonClick(index); });
                myButtons.Add(button.GetComponent <Button>());
                tileCounter++;
            }
        }