Untitled
unknown
plain_text
a year ago
4.0 kB
6
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomTextureSwitcher : MonoBehaviour
{
[SerializeField] List<MeshRenderer> meshes; // List of MeshRenderers (5 meshes)
[SerializeField] List<Material> alternateMaterials; // List of alternate materials for each mesh
private List<Material> originalMaterials = new List<Material>(); // List to store original materials
private bool isEmissionToggling = false; // Flag for emission toggling
void Start()
{
// Ensure meshes and alternateMaterials are properly assigned
if (meshes.Count != alternateMaterials.Count || meshes.Count == 0)
{
Debug.LogError("Mesh and alternate material lists are not correctly assigned!");
return;
}
// Store the original materials from the meshes
for (int i = 0; i < meshes.Count; i++)
{
originalMaterials.Add(meshes[i].material); // Store the original material for each mesh
}
// Start the coroutine to switch materials randomly for each mesh
for (int i = 0; i < meshes.Count; i++)
{
// Start a coroutine for each mesh individually
StartCoroutine(SwitchMaterialRandomly(i));
}
}
IEnumerator SwitchMaterialRandomly(int index)
{
while (true) // Infinite loop to keep switching materials
{
// Get the current material of the mesh
Material currentMaterial = meshes[index].material;
// Switch between original and alternate material
//if (currentMaterial == originalMaterials[index])
//{
// meshes[index].material = alternateMaterials[index]; // Switch to alternate material
// StartCoroutine(ToggleEmission(meshes[index], true)); // Start emission toggling for alternate material
//}
//else
//{
// StopCoroutine(ToggleEmission(meshes[index], true)); // Stop emission toggling
// meshes[index].material = originalMaterials[index]; // Switch back to original material
// SetEmission(meshes[index].material, false); // Turn off emission for the original material
//}
StartCoroutine(ToggleEmission(meshes[index], true));
// Wait for a random time between 0.5 to 1 second before switching again
float randomTime = Random.Range(0.5f, 5f);
yield return new WaitForSeconds(randomTime);
}
}
// Coroutine to toggle emission on/off
IEnumerator ToggleEmission(MeshRenderer meshRenderer, bool toggleOn)
{
isEmissionToggling = toggleOn;
Material mat = meshRenderer.material;
while (isEmissionToggling)
{
// Toggle emission on/off
bool emissionEnabled = mat.IsKeywordEnabled("_EMISSION");
SetEmission(mat, !emissionEnabled, 0.5f); // Toggle emission state
// Wait for a short time before toggling again (like flashing)
yield return new WaitForSeconds(0.5f); // You can tweak this for faster or slower flashing
}
}
// Method to set emission on or off
void SetEmission(Material mat, bool enableEmission, float intensity = 1f)
{
if (enableEmission)
{
mat.EnableKeyword("_EMISSION"); // Enable emission
Color emissionColor = Color.white * Mathf.LinearToGammaSpace(intensity); // Set emission color with intensity
mat.SetColor("_EmissionColor", emissionColor); // Apply the emission color with intensity
}
else
{
mat.DisableKeyword("_EMISSION"); // Disable emission
}
}
}Editor is loading...
Leave a Comment