Пример #1
0
        // 放置两个button按钮作为返回上一个场景,以及退出
        private void PlaceButton()
        {
            // 返回按钮
            GameObject backButtonObject = GameObject.Instantiate(_commonParams.GetPrefabFileButton(),
                                                                 _parentObject.transform.position, _commonParams.GetPrefabFileButton().transform.rotation);

            backButtonObject.transform.SetParent(_parentObject.transform);
            RectTransform backRect = backButtonObject.GetComponent <RectTransform>();

            backRect.position  = new Vector3(50, _screenSize[1] - 50, 0);
            backRect.sizeDelta = new Vector2(50, 30);
            Text backText = backButtonObject.GetComponentInChildren <Text>();

            backText.text = "Back";
            Button backButton = backButtonObject.GetComponent <Button>();

            backButton.onClick.AddListener(delegate
            {
                SceneManager.LoadScene("LoadScore");
            });

            // 退出按钮
            GameObject exitButtonObject = GameObject.Instantiate(_commonParams.GetPrefabFileButton(),
                                                                 _parentObject.transform.position, _commonParams.GetPrefabFileButton().transform.rotation);

            exitButtonObject.transform.SetParent(_parentObject.transform);
            RectTransform exitRect = exitButtonObject.GetComponent <RectTransform>();

            exitRect.position  = new Vector3(_screenSize[0] - 50, _screenSize[1] - 50, 0);
            exitRect.sizeDelta = new Vector2(50, 30);
            Text exitText = exitButtonObject.GetComponentInChildren <Text>();

            exitText.text = "Exit";
            Button exitButton = exitButtonObject.GetComponent <Button>();

            exitButton.onClick.AddListener(delegate
            {
                Application.Quit();
            });
        }
Пример #2
0
        private void LoadScore()
        {
            // 获取Canvas
            GameObject canvasObject = GameObject.Find("Canvas_Menu");

            // 遍历musicxml目录里的所有xml文件
            DirectoryInfo xmlFolder = new DirectoryInfo(_commonParams.GetXmlFolderPath());

            int     xmlFileCount   = 0;
            Vector3 buttonPosition = new Vector3(Screen.width / 2, Screen.height - 100, 0);

            foreach (FileInfo xmlFile in xmlFolder.GetFiles())
            {
                if (xmlFile.Extension == ".xml")
                {
                    xmlFileCount += 1;
                    if (xmlFileCount >= 10) //TODO 设置一页最长放置个数,后续完善成滚动加载
                    {
                        break;
                    }

                    string     buttonName   = "Button" + xmlFileCount;
                    GameObject buttonObject = GameObject.Instantiate(_commonParams.GetPrefabFileButton(),
                                                                     canvasObject.transform.position, _commonParams.GetPrefabFileButton().transform.rotation);
                    buttonObject.name = buttonName;
                    buttonObject.transform.SetParent(canvasObject.transform);
                    RectTransform rect = buttonObject.GetComponent <RectTransform>();
                    // 设置位置为以画布左下角为坐标原点
                    rect.position = new Vector3(buttonPosition.x,
                                                buttonPosition.y - 50 * xmlFileCount,
                                                buttonPosition.z);
                    Text btnText = buttonObject.GetComponentInChildren <Text>();
                    btnText.text = xmlFile.Name.Replace(xmlFile.Extension, ""); // 设置button显示文字为去掉扩展名的文件名

                    Button button = buttonObject.GetComponent <Button>();
                    button.onClick.AddListener(delegate
                    {
                        _commonParams.SetScoreName(xmlFile.FullName); // 设置要加载的xml文件名
                        SceneManager.LoadScene("DrawScore");
                    });
                }
            }
        }