- Home /
AssetPostprocessor: How to persist changes to a model
I'm adding a submesh to a model in AssetPostprocessor.OnPostprocessAllAssets, and then building a prefab from it with PrefabUtility.CreatePrefab.
It works fine until I restart the editor, and then the changes to the model are shown to have been ephemeral; neither it nor the prefab retain the new submesh.
What's the trick to making lasting changes to a model, or at least saving a new mesh?
static def OnPostprocessAllAssets( imported as (string),
deleted as (string),
moved as (string),
movedFromAssetPath as (string) ):
for assetName in imported:
if assetName.EndsWith('Ship.obj'):
modelName = assetName.Replace('.obj', '').Replace('Assets/models/', '')
mesh = AssetDatabase.LoadAssetAtPath( assetName, Mesh )
BuildWireFrame( mesh )
go = GameObject( modelName )
mf = go.AddComponent[of MeshFilter]()
mf.sharedMesh = mesh
renderer = go.AddComponent[of MeshRenderer]()
renderer.materials = (
AssetDatabase.LoadAssetAtPath( 'Assets/materials/RimLight.mat', Material ),
AssetDatabase.LoadAssetAtPath( 'Assets/materials/WireFrame.mat', Material )
)
PrefabUtility.CreatePrefab( "Assets/prefabs/" + modelName + ".prefab", go )
EditorApplication.SaveAssets()
EditorApplication.SaveScene()
GameObject.DestroyImmediate( go )
Answer by Lucian_Games · Feb 25, 2014 at 05:00 PM
I'd still like to know if a model can be permanently modified, but cloning the mesh and saving it to the AssetDatabase works.
original as Mesh = AssetDatabase.LoadAssetAtPath( assetName, Mesh )
// clone
mesh = Mesh()
mesh.vertices = original.vertices
mesh.triangles = original.triangles
mesh.normals = original.normals
mesh.uv = original.uv
// modify clone
BuildWire( mesh )
modelName = assetName.Replace('.obj', '').Replace('Assets/models/', '')
meshAssetPath = "Assets/meshes/${modelName}Mesh.asset"
// save
AssetDatabase.CreateAsset( mesh, meshAssetPath )
// reimport
mesh = AssetDatabase.LoadAssetAtPath( meshAssetPath, Mesh )
// use
go = GameObject( modelName )
mf = go.AddComponent[of MeshFilter]()
mf.sharedMesh = mesh
renderer = go.AddComponent[of MeshRenderer]()
renderer.materials = (
AssetDatabase.LoadAssetAtPath( 'Assets/materials/Rim.mat', Material ),
AssetDatabase.LoadAssetAtPath( 'Assets/materials/Wire.mat', Material )
)
PrefabUtility.CreatePrefab( "Assets/prefabs/" + modelName + ".prefab", go )
AssetDatabase.Refresh()
EditorApplication.SaveAssets()
EditorApplication.SaveScene( EditorApplication.currentScene )
GameObject.DestroyImmediate( go )
Your answer
Follow this Question
Related Questions
Any way to invisibly tag an object? 1 Answer
Running a script when Unity starts. 3 Answers
How can i get the center of the entire editor window 0 Answers
get folder from selection array 4 Answers
Editor Scripting - Override Scaling 1 Answer