using UnityEditor;
using UnityEngine;
namespace GrassCutter.EditorTools
{
/// Import settings for Blender FBX, UI art, textures and audio.
public class ImportRules : AssetPostprocessor
{
void OnPreprocessModel()
{
bool pack = assetPath.StartsWith("Assets/Art/Pack/");
if (!pack && !assetPath.StartsWith("Assets/Art/Models/")) return;
var mi = (ModelImporter)assetImporter;
mi.bakeAxisConversion = true;
mi.useFileScale = true;
mi.globalScale = 1f;
mi.importCameras = false;
mi.importLights = false;
mi.importVisibility = false;
mi.importAnimation = false;
mi.animationType = assetPath.Contains("SM_Crew") || assetPath.Contains("SM_Dir_") ? ModelImporterAnimationType.Generic : ModelImporterAnimationType.None;
mi.addCollider = false;
// runtime MeshColliders (seabed rocks and wrecks) need CPU-readable meshes in the player
mi.isReadable = assetPath.Contains("SM_Spire") || assetPath.Contains("SM_Wreck") || (pack && !assetPath.Contains("SM_Veh_") && !assetPath.Contains("Hay_Bale"));
mi.meshCompression = ModelImporterMeshCompression.Off;
mi.importNormals = ModelImporterNormals.Import;
mi.importTangents = ModelImporterTangents.CalculateMikk;
mi.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
mi.materialLocation = ModelImporterMaterialLocation.InPrefab;
mi.materialSearch = ModelImporterMaterialSearch.Everywhere;
mi.materialName = ModelImporterMaterialName.BasedOnMaterialName;
}
///
/// The Blender exports land rotated 180° about Y (fronts face −Z). Turn every model around so the manifest's
/// convention holds in Unity: front = +Z, N flap = +Z, E = +X.
///
void OnPostprocessModel(GameObject g)
{
if (!assetPath.StartsWith("Assets/Art/Models/")) return;
g.transform.localRotation = Quaternion.AngleAxis(180f, Vector3.up) * g.transform.localRotation;
}
/// Use our hand-made URP materials (Assets/Art/Materials/M_*.mat) instead of the FBX ones.
Material OnAssignMaterialModel(Material material, Renderer renderer)
{
// the pack's atlas, and its glass as far as a name tells
if (assetPath.StartsWith("Assets/Art/Pack/"))
return AssetDatabase.LoadAssetAtPath(material.name.Contains("Glass") || renderer.name.Contains("Glass") ? "Assets/Art/Materials/M_PackGlass.mat" : "Assets/Art/Materials/M_Pack.mat");
if (!assetPath.StartsWith("Assets/Art/Models/")) return null;
var name = material.name;
int dot = name.IndexOf('.');
if (dot > 0) name = name.Substring(0, dot); // Blender duplicates: M_Foo.001
var path = "Assets/Art/Materials/" + name + ".mat";
return AssetDatabase.LoadAssetAtPath(path);
}
void OnPreprocessTexture()
{
var ti = (TextureImporter)assetImporter;
if (assetPath.StartsWith("Assets/Art/UI/"))
{
ti.textureType = TextureImporterType.Default;
ti.alphaIsTransparency = true;
ti.mipmapEnabled = false;
ti.sRGBTexture = true;
ti.wrapMode = TextureWrapMode.Clamp;
ti.maxTextureSize = 2048;
ti.textureCompression = TextureImporterCompression.CompressedHQ;
ti.npotScale = TextureImporterNPOTScale.None;
}
else if (assetPath.StartsWith("Assets/Art/Textures/"))
{
ti.textureType = TextureImporterType.Default;
ti.mipmapEnabled = true;
ti.anisoLevel = 4;
ti.wrapMode = TextureWrapMode.Repeat;
bool crew = assetPath.Contains("T_Crew_");
ti.maxTextureSize = assetPath.Contains("PolygonFarm") || crew ? 2048 : 1024;
ti.textureCompression = crew ? TextureImporterCompression.Compressed : TextureImporterCompression.CompressedHQ;
}
else if (assetPath.StartsWith("Assets/Resources/Talk/"))
{
// the rooms behind the video calls: mipmapped, so TalkHolo can take them a little out of focus
ti.textureType = TextureImporterType.Default;
ti.mipmapEnabled = true;
ti.sRGBTexture = true;
ti.wrapMode = TextureWrapMode.Clamp;
ti.maxTextureSize = 1024;
ti.textureCompression = TextureImporterCompression.CompressedHQ;
}
else if (assetPath.StartsWith("Assets/Art/Generated/"))
{
ti.textureType = TextureImporterType.Default;
ti.alphaIsTransparency = true;
ti.mipmapEnabled = true;
ti.wrapMode = TextureWrapMode.Clamp;
ti.textureCompression = TextureImporterCompression.Uncompressed;
}
}
void OnPreprocessAudio()
{
if (!assetPath.StartsWith("Assets/Audio/")) return;
var ai = (AudioImporter)assetImporter;
var s = ai.defaultSampleSettings;
bool music = assetPath.Contains("music_");
bool loop = assetPath.Contains("_loop");
if (music)
{
s.loadType = AudioClipLoadType.Streaming;
s.compressionFormat = AudioCompressionFormat.Vorbis;
s.quality = 0.75f;
}
else if (loop)
{
s.loadType = AudioClipLoadType.CompressedInMemory;
s.compressionFormat = AudioCompressionFormat.Vorbis;
s.quality = 0.8f;
}
else
{
s.loadType = AudioClipLoadType.DecompressOnLoad;
s.compressionFormat = AudioCompressionFormat.ADPCM;
}
ai.defaultSampleSettings = s;
ai.loadInBackground = music;
}
}
}