TitleScreenSetupHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * TITLE SCREEN SETUP FIX
  3. * ======================
  4. *
  5. * ERROR: "UIDocument component not found on TitleScreenManager GameObject"
  6. *
  7. * SOLUTION:
  8. * ========
  9. *
  10. * The TitleScreenManager script requires a UIDocument component on the same GameObject.
  11. *
  12. * STEP-BY-STEP FIX:
  13. * ================
  14. *
  15. * Option A - Add UIDocument to existing GameObject:
  16. * ------------------------------------------------
  17. * 1. Select the GameObject that has the TitleScreenManager script
  18. * 2. In Inspector, click "Add Component"
  19. * 3. Search for "UIDocument" and add it
  20. * 4. In the UIDocument component:
  21. * - Set "Source Asset" to your TitleScreen.uxml file
  22. * - In "Style Sheets" section, click "+" and add TitleScreen.uss
  23. *
  24. * Option B - Move script to GameObject with UIDocument:
  25. * ---------------------------------------------------
  26. * 1. Find the GameObject that already has UIDocument component
  27. * 2. Add the TitleScreenManager script to that GameObject
  28. * 3. Remove TitleScreenManager from the old GameObject
  29. *
  30. * Option C - Create new GameObject with both components:
  31. * ----------------------------------------------------
  32. * 1. Create empty GameObject named "TitleScreenManager"
  33. * 2. Add UIDocument component
  34. * 3. Add TitleScreenManager script
  35. * 4. Configure UIDocument as described in Option A
  36. *
  37. * VERIFY SETUP:
  38. * ============
  39. *
  40. * After fixing, you should have ONE GameObject with:
  41. * ✓ TitleScreenManager script
  42. * ✓ UIDocument component (with UXML and USS assigned)
  43. *
  44. * The error should disappear and buttons should work.
  45. *
  46. */
  47. using UnityEngine;
  48. using UnityEngine.UIElements;
  49. public class TitleScreenSetupHelper : MonoBehaviour
  50. {
  51. [Header("Setup Instructions")]
  52. [TextArea(8, 12)]
  53. public string setupSteps = @"1. This GameObject needs BOTH components:
  54. - TitleScreenManager script ✓
  55. - UIDocument component (missing)
  56. 2. Add UIDocument component to this GameObject
  57. 3. In UIDocument inspector:
  58. - Source Asset: TitleScreen.uxml
  59. - Style Sheets: Add TitleScreen.uss
  60. 4. Remove this helper component when done";
  61. [Header("Setup Validation")]
  62. public bool hasUIDocument = false;
  63. public bool hasManagerScript = false;
  64. public bool uiDocumentConfigured = false;
  65. private void Start()
  66. {
  67. ValidateSetup();
  68. }
  69. [ContextMenu("Validate Setup")]
  70. public void ValidateSetup()
  71. {
  72. Debug.Log("=== TITLE SCREEN SETUP VALIDATION ===");
  73. // Check for UIDocument
  74. UIDocument uiDoc = GetComponent<UIDocument>();
  75. hasUIDocument = uiDoc != null;
  76. Debug.Log($"UIDocument component: {(hasUIDocument ? "✓ Found" : "❌ Missing")}");
  77. // Check for TitleScreenManager
  78. TitleScreenManager manager = GetComponent<TitleScreenManager>();
  79. hasManagerScript = manager != null;
  80. Debug.Log($"TitleScreenManager script: {(hasManagerScript ? "✓ Found" : "❌ Missing")}");
  81. // Check UIDocument configuration
  82. if (hasUIDocument)
  83. {
  84. var sourceAsset = uiDoc.visualTreeAsset;
  85. uiDocumentConfigured = sourceAsset != null;
  86. Debug.Log($"UXML file assigned: {(sourceAsset != null ? "✓ Yes" : "❌ No")}");
  87. if (sourceAsset != null)
  88. {
  89. Debug.Log($"UXML file: {sourceAsset.name}");
  90. }
  91. // Check for style sheets (they're embedded in the UXML or assigned differently)
  92. Debug.Log("USS files: Check manually in UIDocument inspector");
  93. }
  94. // Final status
  95. bool setupComplete = hasUIDocument && hasManagerScript && uiDocumentConfigured;
  96. Debug.Log($"=== SETUP STATUS: {(setupComplete ? "✅ COMPLETE" : "❌ INCOMPLETE")} ===");
  97. if (!setupComplete)
  98. {
  99. Debug.LogWarning("Setup incomplete! Follow the instructions in the setupSteps field.");
  100. }
  101. }
  102. [ContextMenu("Auto-Fix Setup (if possible)")]
  103. public void AutoFixSetup()
  104. {
  105. Debug.Log("Attempting auto-fix...");
  106. // Add UIDocument if missing
  107. if (!hasUIDocument)
  108. {
  109. UIDocument uiDoc = gameObject.AddComponent<UIDocument>();
  110. Debug.Log("✓ Added UIDocument component");
  111. // Try to find and assign UXML file
  112. var uxml = Resources.Load<VisualTreeAsset>("TitleScreen");
  113. if (uxml == null)
  114. {
  115. // Try different paths
  116. string[] searchPaths = {
  117. "Assets/UI Toolkit/TitleScreen.uxml",
  118. "Assets/UI/TitleScreen.uxml",
  119. "TitleScreen"
  120. };
  121. foreach (string path in searchPaths)
  122. {
  123. uxml = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(path);
  124. if (uxml != null)
  125. {
  126. Debug.Log($"✓ Found UXML at: {path}");
  127. break;
  128. }
  129. }
  130. }
  131. if (uxml != null)
  132. {
  133. uiDoc.visualTreeAsset = uxml;
  134. Debug.Log("✓ UXML file assigned automatically");
  135. }
  136. else
  137. {
  138. Debug.LogWarning("❌ Could not find TitleScreen.uxml - please assign manually");
  139. }
  140. }
  141. ValidateSetup();
  142. }
  143. // Remove this component once setup is complete
  144. [ContextMenu("Remove Setup Helper")]
  145. public void RemoveSetupHelper()
  146. {
  147. if (Application.isPlaying)
  148. {
  149. Destroy(this);
  150. }
  151. else
  152. {
  153. DestroyImmediate(this);
  154. }
  155. Debug.Log("Setup helper removed");
  156. }
  157. }