MiniGames/Assets/Editor/FinalFix.cs
2025-07-16 22:36:33 +05:00

45 lines
1.9 KiB
C#

using UnityEditor;
using UnityEngine;
public class FinalFix : EditorWindow
{
[MenuItem("Tools/Apply Materials and Create Prefab")]
static void FixPrefab()
{
string basePath = "Assets/GrabbingMachine/";
string fbxPath = basePath + "SM_Grabbing_Machine.fbx";
string prefabPath = basePath + "SM_Grabbing_Machine.prefab";
GameObject model = AssetDatabase.LoadAssetAtPath<GameObject>(fbxPath);
if (!model) { Debug.LogError("FBX not found"); return; }
GameObject instance = PrefabUtility.InstantiatePrefab(model) as GameObject;
foreach (Renderer renderer in instance.GetComponentsInChildren<Renderer>())
{
string baseName = "grabbing_machine_" + renderer.name.ToLower().Replace("mesh", "").Trim();
string matPath = basePath + "Materials/" + baseName + ".mat";
Material mat = AssetDatabase.LoadAssetAtPath<Material>(matPath);
if (mat == null) continue;
Texture2D albedo = AssetDatabase.LoadAssetAtPath<Texture2D>($"{basePath}Textures/T_grabbing_machine_grabbing_machine_{baseName}_BC.png");
Texture2D normal = AssetDatabase.LoadAssetAtPath<Texture2D>($"{basePath}Textures/T_grabbing_machine_grabbing_machine_{baseName}_N.png");
Texture2D mrao = AssetDatabase.LoadAssetAtPath<Texture2D>($"{basePath}Textures/T_grabbing_machine_grabbing_machine_{baseName}_MRAO.png");
mat.shader = Shader.Find("Standard");
if (albedo) mat.SetTexture("_MainTex", albedo);
if (normal) { mat.EnableKeyword("_NORMALMAP"); mat.SetTexture("_BumpMap", normal); }
if (mrao) { mat.EnableKeyword("_METALLICGLOSSMAP"); mat.SetTexture("_MetallicGlossMap", mrao); }
renderer.sharedMaterial = mat;
}
PrefabUtility.SaveAsPrefabAsset(instance, prefabPath);
DestroyImmediate(instance);
Debug.Log("✅ Prefab created with all materials assigned.");
}
}