using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class s_ModelCustomization : MonoBehaviour { public GameObject targetGameObject; // Player GO to customize public MeshFilter playerMeshFilter; public s_CarCollectionSO carCollection; // Reference to the car collection scriptable object public TextMeshProUGUI carNameText; // Reference to the text component to display the car name public List colorPanels; // List of color panels to change the car's color public s_SkinCollectionSO skinCollection; // Reference for the skins private string selectedPlayerName; private int currentIndex = 0; // Index to keep track of the current mesh being displayed private void Start() { // Ensure the target player car reference is not null if (targetGameObject == null) { // Get target using tag targetGameObject = GameObject.FindGameObjectWithTag("Car Body"); return; } // Ensure the playerMeshRenderer reference is not null if (playerMeshFilter == null) { Debug.Log("Player mesh renderer is missing for; " + this.name); // Get renderer component from the targetGameObject playerMeshFilter = targetGameObject.GetComponent(); return; } // Ensure the carCollection reference is not null if (carCollection == null) { Debug.LogError("Car collection is missing for; " + this.name); carCollection = GetComponentInParent().carCollection; return; } // Initialize the color panels dictionary InitializeColorPanels(); } private void InitializeColorPanels() { // Initialize the color panels list colorPanels = new List(); // Find all color panels under the parent object Transform parentTransform = transform.parent; foreach (Transform child in parentTransform) { // Check if the child's name matches a car identifier if (carCollection.cars.Exists(car => car.identifier == child.gameObject.name)) { // Add the color panel to the list colorPanels.Add(child.gameObject); } } } // Method to update the color panels based on the current car identifier private void UpdateColorPanels() { // Disable all color panels foreach (GameObject panel in colorPanels) { panel.SetActive(false); } // Enable the color panel associated with the current car identifier string currentIdentifier = carCollection.cars[currentIndex].identifier; int panelIndex = colorPanels.FindIndex(panel => panel.name == currentIdentifier); if (panelIndex != -1) { colorPanels[panelIndex].SetActive(true); } } public void ChangeCarMeshForward() { // Increment the currentIndex to switch to the next mesh currentIndex = (currentIndex + 1) % carCollection.cars.Count; UpdateCarIdentifierText(); ApplyMesh(); UpdateColorPanels(); } // Method to change the car's mesh backward public void ChangeCarMeshBackward() { // Decrement the currentIndex currentIndex--; // Wrap around if currentIndex becomes negative if (currentIndex < 0) { currentIndex = carCollection.cars.Count - 1; Debug.Log("currentIndex wrapped around to " + currentIndex); } Debug.Log("Current index: " + currentIndex); UpdateCarIdentifierText(); ApplyMesh(); UpdateColorPanels(); } // Method to update the car identifier text private void UpdateCarIdentifierText() { if (carNameText != null && carCollection != null && carCollection.cars.Count > 0) { string currentIdentifier = carCollection.cars[currentIndex].identifier; carNameText.SetText("Current Car: " + currentIdentifier); } } // Apply the mesh to the target GameObject private void ApplyMesh() { // Get the mesh from the car collection based on the current index string identifier = carCollection.cars[currentIndex].identifier; Mesh newMesh = carCollection.GetCarModel(identifier); // Assuming the target object has a MeshFilter component MeshFilter meshFilter = targetGameObject.GetComponent(); if (meshFilter != null) { // Change the mesh meshFilter.mesh = newMesh; ApplyFirstSavedMaterial(); } else { Debug.LogError("Target object doesn't have a MeshFilter component."); } } private void ApplyFirstSavedMaterial() { // Update skin collection to match current panel skinCollection = colorPanels[currentIndex].GetComponent().skinCollection; // Apply material from the skin collection index 0 Material firstSkinMaterial = skinCollection.skinsList[0].material; targetGameObject.GetComponent().material = firstSkinMaterial; } }