TitleScreenTroubleshooter.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * TITLE SCREEN ERROR TROUBLESHOOTING GUIDE
  3. * ========================================
  4. *
  5. * If you're getting ArgumentOutOfRangeException errors when running the Title Screen,
  6. * this is likely due to UI Toolkit stylesheet issues.
  7. *
  8. * SOLUTION STEPS:
  9. * ==============
  10. *
  11. * 1. REMOVE USS REFERENCE FROM UXML (FIXED)
  12. * - The TitleScreen.uxml file now has the problematic Style reference removed
  13. * - You'll need to manually assign the USS file in Unity instead
  14. *
  15. * 2. ASSIGN STYLESHEET MANUALLY IN UNITY:
  16. * a) Open your TitleScreen scene
  17. * b) Select the GameObject with UIDocument component
  18. * c) In the UIDocument component, find the "Style Sheets" section
  19. * d) Click the "+" button to add a style sheet
  20. * e) Assign the TitleScreen.uss file from Assets/UI Toolkit/
  21. *
  22. * 3. ALTERNATIVE SIMPLE SETUP (If USS still causes issues):
  23. * - You can remove the USS file entirely and use inline styles
  24. * - The buttons will use Unity's default styling
  25. * - See the SimpleSetup method below for code-based styling
  26. *
  27. * 4. VERIFY SETUP:
  28. * - Make sure the UXML file is assigned to the UIDocument
  29. * - Make sure the TitleScreenManager script is on the same GameObject
  30. * - Check Console for any "not found" warnings about buttons
  31. *
  32. * COMMON ISSUES:
  33. * =============
  34. *
  35. * - USS files with unsupported CSS properties cause this error
  36. * - Hardcoded GUID references in UXML can break when files are moved
  37. * - Button names in UXML must match exactly what the script expects
  38. *
  39. * TEST WITHOUT STYLING:
  40. * ====================
  41. *
  42. * If you want to test functionality without any styling issues:
  43. * 1. Remove the USS file assignment completely
  44. * 2. The UI will use Unity's default button styling
  45. * 3. Once functionality works, you can re-add styling gradually
  46. *
  47. */
  48. using UnityEngine;
  49. using UnityEngine.UIElements;
  50. public class TitleScreenTroubleshooter : MonoBehaviour
  51. {
  52. [Header("Troubleshooting Options")]
  53. public bool useSimpleSetupWithoutUSS = false;
  54. [ContextMenu("Apply Simple Setup (No USS)")]
  55. public void ApplySimpleSetup()
  56. {
  57. var uiDocument = GetComponent<UIDocument>();
  58. if (uiDocument == null)
  59. {
  60. Debug.LogError("No UIDocument found on this GameObject");
  61. return;
  62. }
  63. var root = uiDocument.rootVisualElement;
  64. // Apply basic inline styling to avoid USS issues
  65. var titleScreen = root.Q<VisualElement>("TitleScreen");
  66. if (titleScreen != null)
  67. {
  68. titleScreen.style.width = Length.Percent(100);
  69. titleScreen.style.height = Length.Percent(100);
  70. titleScreen.style.backgroundColor = new Color(0.08f, 0.08f, 0.12f, 1f); // Dark blue background
  71. }
  72. var mainContent = root.Q<VisualElement>("MainContent");
  73. if (mainContent != null)
  74. {
  75. mainContent.style.width = Length.Percent(100);
  76. mainContent.style.height = Length.Percent(100);
  77. mainContent.style.alignItems = Align.Center;
  78. mainContent.style.justifyContent = Justify.Center;
  79. mainContent.style.flexDirection = FlexDirection.Column;
  80. }
  81. var gameTitle = root.Q<Label>("GameTitle");
  82. if (gameTitle != null)
  83. {
  84. gameTitle.style.fontSize = 48;
  85. gameTitle.style.color = new Color(1f, 0.84f, 0.39f, 1f); // Gold color
  86. gameTitle.style.unityFontStyleAndWeight = FontStyle.Bold;
  87. gameTitle.style.marginBottom = 20;
  88. }
  89. var gameSubtitle = root.Q<Label>("GameSubtitle");
  90. if (gameSubtitle != null)
  91. {
  92. gameSubtitle.style.fontSize = 18;
  93. gameSubtitle.style.color = new Color(0.8f, 0.8f, 0.8f, 1f); // Light gray
  94. gameSubtitle.style.marginBottom = 40;
  95. }
  96. // Style buttons
  97. var newGameButton = root.Q<Button>("NewGameButton");
  98. var loadGameButton = root.Q<Button>("LoadGameButton");
  99. var quitButton = root.Q<Button>("QuitButton");
  100. StyleButton(newGameButton, new Color(0.24f, 0.47f, 0.24f, 1f)); // Green
  101. StyleButton(loadGameButton, new Color(0.24f, 0.24f, 0.47f, 1f)); // Blue
  102. StyleButton(quitButton, new Color(0.47f, 0.24f, 0.24f, 1f)); // Red
  103. Debug.Log("Simple setup applied - no USS styling used");
  104. }
  105. private void StyleButton(Button button, Color backgroundColor)
  106. {
  107. if (button == null) return;
  108. button.style.width = 200;
  109. button.style.height = 50;
  110. button.style.marginTop = 8;
  111. button.style.marginBottom = 8;
  112. button.style.backgroundColor = backgroundColor;
  113. button.style.borderTopLeftRadius = 8;
  114. button.style.borderTopRightRadius = 8;
  115. button.style.borderBottomLeftRadius = 8;
  116. button.style.borderBottomRightRadius = 8;
  117. button.style.fontSize = 16;
  118. button.style.color = Color.white;
  119. button.style.unityFontStyleAndWeight = FontStyle.Bold;
  120. }
  121. [ContextMenu("Test Button Finding")]
  122. public void TestButtonFinding()
  123. {
  124. var uiDocument = GetComponent<UIDocument>();
  125. if (uiDocument == null)
  126. {
  127. Debug.LogError("No UIDocument found");
  128. return;
  129. }
  130. var root = uiDocument.rootVisualElement;
  131. var newGameButton = root.Q<Button>("NewGameButton");
  132. var loadGameButton = root.Q<Button>("LoadGameButton");
  133. var quitButton = root.Q<Button>("QuitButton");
  134. Debug.Log($"NewGameButton found: {newGameButton != null}");
  135. Debug.Log($"LoadGameButton found: {loadGameButton != null}");
  136. Debug.Log($"QuitButton found: {quitButton != null}");
  137. if (newGameButton == null || loadGameButton == null || quitButton == null)
  138. {
  139. Debug.LogWarning("Some buttons not found - check UXML button names match script expectations");
  140. }
  141. else
  142. {
  143. Debug.Log("All buttons found successfully!");
  144. }
  145. }
  146. }