SettlementUIBridge.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using UnityEngine;
  2. /// <summary>
  3. /// Bridge component that connects SettlementInteractionManager's GameObject control
  4. /// with SettlementInteractionUI's UI Toolkit system
  5. /// </summary>
  6. public class SettlementUIBridge : MonoBehaviour
  7. {
  8. [Header("UI Bridge")]
  9. [Tooltip("The SettlementInteractionUI component to control")]
  10. public SettlementInteractionUI settlementUI;
  11. [Tooltip("Should this GameObject's active state control the UI visibility?")]
  12. public bool controlUIVisibility = true;
  13. private bool lastActiveState;
  14. void Start()
  15. {
  16. // Find SettlementInteractionUI if not assigned
  17. if (settlementUI == null)
  18. {
  19. // First try to get from same GameObject
  20. settlementUI = GetComponent<SettlementInteractionUI>();
  21. }
  22. if (settlementUI == null)
  23. {
  24. // Then try to find in scene
  25. settlementUI = FindFirstObjectByType<SettlementInteractionUI>();
  26. }
  27. if (settlementUI != null)
  28. {
  29. Debug.Log("[UIBridge] Connected to SettlementInteractionUI");
  30. // Force initial state
  31. lastActiveState = gameObject.activeSelf;
  32. UpdateUIVisibility();
  33. }
  34. else
  35. {
  36. Debug.LogError("[UIBridge] SettlementInteractionUI not found! Make sure it's on the same GameObject.");
  37. }
  38. }
  39. void Update()
  40. {
  41. if (!controlUIVisibility) return;
  42. // Check if GameObject active state changed
  43. bool currentActiveState = gameObject.activeSelf;
  44. if (currentActiveState != lastActiveState)
  45. {
  46. lastActiveState = currentActiveState;
  47. UpdateUIVisibility();
  48. }
  49. }
  50. private void UpdateUIVisibility()
  51. {
  52. if (settlementUI == null) return;
  53. if (gameObject.activeSelf)
  54. {
  55. Debug.Log("[UIBridge] GameObject activated - UI should show");
  56. // Force UI to show with test data if no real settlement detected
  57. settlementUI.ForceShowUI();
  58. }
  59. else
  60. {
  61. Debug.Log("[UIBridge] GameObject deactivated - UI should hide");
  62. settlementUI.ForceHideUI();
  63. }
  64. }
  65. void OnEnable()
  66. {
  67. UpdateUIVisibility();
  68. }
  69. void OnDisable()
  70. {
  71. if (settlementUI != null)
  72. {
  73. settlementUI.ForceHideUI();
  74. }
  75. }
  76. }