فهرست منبع

Improve create ui performance a bit.

Stephen Damm 6 سال پیش
والد
کامیت
e38c6378ed
2فایلهای تغییر یافته به همراه10 افزوده شده و 7 حذف شده
  1. 6 3
      SongBrowserPlugin/UI/SongBrowserUI.cs
  2. 4 4
      SongBrowserPlugin/UI/UIBuilder.cs

+ 6 - 3
SongBrowserPlugin/UI/SongBrowserUI.cs

@@ -152,6 +152,9 @@ namespace SongBrowserPlugin.UI
                     RefreshSongList();
                 };
 
+                Button sortButtonTemplate = Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "PlayButton"));
+                Button otherButtonTemplate = Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == "QuitButton"));
+
                 float fontSize = 2.75f;
                 float buttonWidth = 17.0f;
                 float buttonHeight = 5.0f;
@@ -171,7 +174,7 @@ namespace SongBrowserPlugin.UI
                 _sortButtonGroup = new List<SongSortButton>();
                 for (int i = 0; i < buttonNames.Length; i++)
                 {
-                    _sortButtonGroup.Add(UIBuilder.CreateSortButton(rect, "PlayButton", arrowIcon, buttonNames[i], fontSize, buttonX, buttonY, buttonWidth, buttonHeight, sortModes[i], onSortButtonClickEvent));
+                    _sortButtonGroup.Add(UIBuilder.CreateSortButton(rect, sortButtonTemplate, arrowIcon, buttonNames[i], fontSize, buttonX, buttonY, buttonWidth, buttonHeight, sortModes[i], onSortButtonClickEvent));
                     buttonX -= buttonWidth;
                 }
 
@@ -179,7 +182,7 @@ namespace SongBrowserPlugin.UI
                 _log.Debug("Creating add to favorites button...");
 
                 RectTransform transform = this._levelDetailViewController.transform as RectTransform;
-                _addFavoriteButton = UIBuilder.CreateUIButton(transform, "QuitButton");
+                _addFavoriteButton = UIBuilder.CreateUIButton(transform, otherButtonTemplate);
                 (_addFavoriteButton.transform as RectTransform).anchoredPosition = new Vector2(40f, 5.75f);
                 (_addFavoriteButton.transform as RectTransform).sizeDelta = new Vector2(10f, 10f);
                 UIBuilder.SetButtonText(ref _addFavoriteButton, _addFavoriteButtonText);
@@ -203,7 +206,7 @@ namespace SongBrowserPlugin.UI
                 _log.Debug("Creating delete button...");
 
                 transform = this._levelDetailViewController.transform as RectTransform;
-                _deleteButton = UIBuilder.CreateUIButton(transform, "QuitButton");
+                _deleteButton = UIBuilder.CreateUIButton(transform, otherButtonTemplate);
                 (_deleteButton.transform as RectTransform).anchoredPosition = new Vector2(46f, 0f);
                 (_deleteButton.transform as RectTransform).sizeDelta = new Vector2(15f, 5f);
                 UIBuilder.SetButtonText(ref _deleteButton, "Delete");

+ 4 - 4
SongBrowserPlugin/UI/UIBuilder.cs

@@ -56,9 +56,9 @@ namespace SongBrowserPlugin.UI
         /// <param name="buttonTemplate"></param>
         /// <param name="buttonInstance"></param>
         /// <returns></returns>
-        static public Button CreateUIButton(RectTransform parent, string buttonTemplate)
+        static public Button CreateUIButton(RectTransform parent, Button buttonTemplate)
         {
-            Button btn = UnityEngine.Object.Instantiate(Resources.FindObjectsOfTypeAll<Button>().First(x => (x.name == buttonTemplate)), parent, false);
+            Button btn = UnityEngine.Object.Instantiate(buttonTemplate, parent, false);
             UnityEngine.Object.DestroyImmediate(btn.GetComponent<GameEventOnUIButtonClick>());
             btn.onClick = new Button.ButtonClickedEvent();
             btn.name = "CustomUIButton";
@@ -78,10 +78,10 @@ namespace SongBrowserPlugin.UI
         /// <param name="w"></param>
         /// <param name="h"></param>
         /// <param name="action"></param>
-        public static SongSortButton CreateSortButton(RectTransform rect, string templateButtonName, Sprite iconSprite, string buttonText, float fontSize, float x, float y, float w, float h, SongSortMode sortMode, System.Action<SongSortMode> onClickEvent)
+        public static SongSortButton CreateSortButton(RectTransform rect, Button buttonTemplate, Sprite iconSprite, string buttonText, float fontSize, float x, float y, float w, float h, SongSortMode sortMode, System.Action<SongSortMode> onClickEvent)
         {
             SongSortButton sortButton = new SongSortButton();
-            Button newButton = UIBuilder.CreateUIButton(rect, templateButtonName);
+            Button newButton = UIBuilder.CreateUIButton(rect, buttonTemplate);
 
             newButton.interactable = true;
             (newButton.transform as RectTransform).anchoredPosition = new Vector2(x, y);