UP-Viagg-io/Viagg-io/Assets/AutoRemapTextures.cs
2025-04-03 02:10:54 +02:00

48 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO; // Required for Directory.GetFiles()
public class AutoRemapTextures : MonoBehaviour
{
public string textureFolder = "Assets/Models/Materials"; // Change to your texture folder path
// Start is called before the first frame update
void Start()
{
Renderer renderer = GetComponent<Renderer>();
if (renderer == null)
{
Debug.LogError("No Renderer found on this object!");
return;
}
foreach (Material mat in renderer.sharedMaterials)
{
if (mat == null) continue;
// Try to find the texture with the same name as the material
string[] textureFiles = Directory.GetFiles(textureFolder, mat.name + ".*");
foreach (string texturePath in textureFiles)
{
if (texturePath.EndsWith(".meta")) continue; // Ignore meta files
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
if (texture != null)
{
string textureProperty = mat.HasProperty("_BaseMap") ? "_BaseMap" : "_MainTex";
mat.SetTexture(textureProperty, texture);
Debug.Log($"Assigned {texture.name} to {mat.name}");
}
else
{
Debug.LogWarning($"Texture not found for material: {mat.name}");
}
break; // Stop after first match
}
}
}
}