using UnityEngine; using UnityEngine.UIElements; public class SceneSetup : MonoBehaviour { [Header("Scene Setup")] public bool setupSceneOnStart = true; public Vector3 townHallPosition = Vector3.zero; public int numberOfTrees = 5; public int numberOfStones = 3; public int numberOfFarms = 2; public float resourceSpawnRadius = 15f; [Header("Materials (Optional)")] public Material villagerMaterial; public Material townHallMaterial; public Material treeMaterial; public Material stoneMaterial; public Material farmMaterial; void Start() { if (setupSceneOnStart) { SetupScene(); } } [ContextMenu("Setup Scene")] public void SetupScene() { CreateGround(); CreateTownHall(); CreateResourceNodes(); SetupCamera(); CreateGameManager(); } void CreateGround() { GameObject ground = GameObject.Find("Ground"); if (ground == null) { // Create ground plane ground = GameObject.CreatePrimitive(PrimitiveType.Plane); ground.name = "Ground"; ground.transform.position = Vector3.zero; ground.transform.localScale = new Vector3(5f, 1f, 5f); // 50x50 units // Set material if available Renderer groundRenderer = ground.GetComponent(); if (groundRenderer != null) { groundRenderer.material.color = new Color(0.3f, 0.5f, 0.3f); // Green ground } Debug.Log("Created ground plane"); } } void CreateTownHall() { GameObject townHallGO = GameObject.FindWithTag("TownHall"); if (townHallGO == null) { // Create TownHall townHallGO = GameObject.CreatePrimitive(PrimitiveType.Cube); townHallGO.name = "TownHall"; townHallGO.tag = "TownHall"; townHallGO.transform.position = townHallPosition; townHallGO.transform.localScale = new Vector3(3f, 2f, 3f); // Add TownHall component TownHall townHall = townHallGO.AddComponent(); // Apply material if available if (townHallMaterial != null) { townHallGO.GetComponent().material = townHallMaterial; } else { townHallGO.GetComponent().material.color = Color.blue; } // Create selection indicator GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder); indicator.transform.SetParent(townHallGO.transform); indicator.transform.localPosition = Vector3.down * 1.2f; indicator.transform.localScale = Vector3.one * 1.5f; indicator.GetComponent().material.color = Color.yellow; indicator.SetActive(false); townHall.selectionIndicator = indicator; Destroy(indicator.GetComponent()); Debug.Log("Created TownHall"); } } void CreateResourceNodes() { // Create Trees for (int i = 0; i < numberOfTrees; i++) { Vector3 position = GetRandomPosition(); CreateTree(position, i); } // Create Stones for (int i = 0; i < numberOfStones; i++) { Vector3 position = GetRandomPosition(); CreateStone(position, i); } // Create Farms for (int i = 0; i < numberOfFarms; i++) { Vector3 position = GetRandomPosition(); CreateFarm(position, i); } } void CreateTree(Vector3 position, int index) { GameObject tree = GameObject.CreatePrimitive(PrimitiveType.Cylinder); tree.name = $"Tree_{index + 1}"; tree.tag = "Tree"; tree.transform.position = position; tree.transform.localScale = new Vector3(1f, 2f, 1f); // Add ResourceNode component ResourceNode node = tree.AddComponent(); node.nodeType = ResourceNodeType.Tree; node.resourceType = ResourceType.Wood; node.maxResources = 50; node.currentResources = 50; node.regenerationRate = 2f; // Apply material if (treeMaterial != null) { tree.GetComponent().material = treeMaterial; } else { tree.GetComponent().material.color = Color.green; } // Create selection indicator CreateSelectionIndicator(tree, node); Debug.Log($"Created Tree at {position}"); } void CreateStone(Vector3 position, int index) { GameObject stone = GameObject.CreatePrimitive(PrimitiveType.Sphere); stone.name = $"Stone_{index + 1}"; stone.tag = "Stone"; stone.transform.position = position; stone.transform.localScale = Vector3.one * 1.5f; // Add ResourceNode component ResourceNode node = stone.AddComponent(); node.nodeType = ResourceNodeType.Stone; node.resourceType = ResourceType.Stone; node.maxResources = 75; node.currentResources = 75; node.regenerationRate = 0.5f; // Apply material if (stoneMaterial != null) { stone.GetComponent().material = stoneMaterial; } else { stone.GetComponent().material.color = Color.gray; } // Create selection indicator CreateSelectionIndicator(stone, node); Debug.Log($"Created Stone at {position}"); } void CreateFarm(Vector3 position, int index) { GameObject farm = GameObject.CreatePrimitive(PrimitiveType.Cube); farm.name = $"Farm_{index + 1}"; farm.tag = "Farm"; farm.transform.position = position; farm.transform.localScale = new Vector3(2f, 0.2f, 2f); // Add ResourceNode component ResourceNode node = farm.AddComponent(); node.nodeType = ResourceNodeType.Farm; node.resourceType = ResourceType.Food; node.maxResources = 100; node.currentResources = 100; node.regenerationRate = 3f; // Apply material if (farmMaterial != null) { farm.GetComponent().material = farmMaterial; } else { farm.GetComponent().material.color = Color.yellow; } // Create selection indicator CreateSelectionIndicator(farm, node); Debug.Log($"Created Farm at {position}"); } void CreateSelectionIndicator(GameObject parent, ResourceNode node) { GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder); indicator.transform.SetParent(parent.transform); indicator.transform.localPosition = Vector3.down * 0.6f; indicator.transform.localScale = Vector3.one * 1.2f; indicator.GetComponent().material.color = Color.cyan; indicator.SetActive(false); node.selectionIndicator = indicator; Destroy(indicator.GetComponent()); } Vector3 GetRandomPosition() { Vector2 randomCircle = Random.insideUnitCircle * resourceSpawnRadius; Vector3 position = townHallPosition + new Vector3(randomCircle.x, 0, randomCircle.y); // Make sure we don't spawn too close to town hall if (Vector3.Distance(position, townHallPosition) < 3f) { return GetRandomPosition(); // Try again } return position; } void SetupCamera() { Camera mainCamera = Camera.main; if (mainCamera == null) { mainCamera = FindObjectOfType(); } if (mainCamera != null) { // Position camera above the scene mainCamera.transform.position = new Vector3(0, 15, -10); mainCamera.transform.rotation = Quaternion.Euler(45, 0, 0); // Add camera controller if it doesn't exist CameraController cameraController = mainCamera.GetComponent(); if (cameraController == null) { cameraController = mainCamera.gameObject.AddComponent(); } // Set up camera for orthographic view (better for RTS games) mainCamera.orthographic = true; mainCamera.orthographicSize = 15f; Debug.Log("Camera setup complete"); } } void CreateGameManager() { // Check if GameManager already exists GameManager existingGameManager = FindObjectOfType(); if (existingGameManager == null) { GameObject gameManagerGO = new GameObject("GameManager"); // Add GameManager component GameManager gameManager = gameManagerGO.AddComponent(); Debug.Log("Created GameManager"); } // Create separate UI GameObject if it doesn't exist GameObject uiGameObject = GameObject.Find("UI"); if (uiGameObject == null) { uiGameObject = new GameObject("UI"); uiGameObject.transform.position = Vector3.zero; // Add UIDocument component UIDocument uiDoc = uiGameObject.AddComponent(); // Try to load the UXML file from Resources folder VisualTreeAsset gameUIAsset = Resources.Load("GameUI"); if (gameUIAsset != null) { uiDoc.visualTreeAsset = gameUIAsset; Debug.Log($"Assigned UXML asset '{gameUIAsset.name}' to UIDocument"); } else { Debug.LogWarning("Could not find GameUI.uxml in Resources! Please assign it manually in the inspector."); } // Try to load panel settings PanelSettings panelSettings = Resources.Load("PanelSettings"); if (panelSettings != null) { uiDoc.panelSettings = panelSettings; Debug.Log("Assigned PanelSettings to UIDocument"); } // Add GameUI component uiGameObject.AddComponent(); Debug.Log("Created UI GameObject with GameUI component"); } // Create Building System GameObject GameObject buildingSystemGO = GameObject.Find("BuildingSystem"); if (buildingSystemGO == null) { buildingSystemGO = new GameObject("BuildingSystem"); buildingSystemGO.AddComponent(); buildingSystemGO.AddComponent(); Debug.Log("Created BuildingSystem and HousingManager"); } } [ContextMenu("Clear Scene")] public void ClearScene() { // Clear existing objects (useful for testing) GameObject[] trees = GameObject.FindGameObjectsWithTag("Tree"); GameObject[] stones = GameObject.FindGameObjectsWithTag("Stone"); GameObject[] farms = GameObject.FindGameObjectsWithTag("Farm"); GameObject[] villagers = GameObject.FindGameObjectsWithTag("Villager"); GameObject townHall = GameObject.FindWithTag("TownHall"); foreach (var obj in trees) DestroyImmediate(obj); foreach (var obj in stones) DestroyImmediate(obj); foreach (var obj in farms) DestroyImmediate(obj); foreach (var obj in villagers) DestroyImmediate(obj); if (townHall != null) DestroyImmediate(townHall); Debug.Log("Scene cleared"); } }