SettlementUIBridge.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Force initial state
  30. lastActiveState = gameObject.activeSelf;
  31. UpdateUIVisibility();
  32. }
  33. else
  34. {
  35. Debug.LogError("[UIBridge] SettlementInteractionUI not found! Make sure it's on the same GameObject.");
  36. }
  37. }
  38. void Update()
  39. {
  40. if (!controlUIVisibility) return;
  41. // Check if GameObject active state changed
  42. bool currentActiveState = gameObject.activeSelf;
  43. if (currentActiveState != lastActiveState)
  44. {
  45. lastActiveState = currentActiveState;
  46. UpdateUIVisibility();
  47. }
  48. }
  49. private void UpdateUIVisibility()
  50. {
  51. if (settlementUI == null) return;
  52. if (gameObject.activeSelf)
  53. {
  54. // Force UI to show with test data if no real settlement detected
  55. settlementUI.ForceShowUI();
  56. }
  57. else
  58. {
  59. settlementUI.ForceHideUI();
  60. }
  61. }
  62. void OnEnable()
  63. {
  64. UpdateUIVisibility();
  65. }
  66. void OnDisable()
  67. {
  68. if (settlementUI != null)
  69. {
  70. settlementUI.ForceHideUI();
  71. }
  72. }
  73. }