Files
grass-cutters/Assets/Editor/ImportRules.cs
NiemandKG d1c3761e58 Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and
Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date.
Images, audio, models and binaries go through Git LFS (.gitattributes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:40 +06:00

121 lines
5.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEditor;
using UnityEngine;
namespace GrassCutter.EditorTools
{
/// <summary>Import settings for Blender FBX, UI art, textures and audio.</summary>
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;
}
/// <summary>
/// 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.
/// </summary>
void OnPostprocessModel(GameObject g)
{
if (!assetPath.StartsWith("Assets/Art/Models/")) return;
g.transform.localRotation = Quaternion.AngleAxis(180f, Vector3.up) * g.transform.localRotation;
}
/// <summary>Use our hand-made URP materials (Assets/Art/Materials/M_*.mat) instead of the FBX ones.</summary>
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>(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<Material>(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/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;
}
}
}