I’m trying to set some default vertex colors when i import a model. This part is working just fine, though i’m having some problems making these changes persist.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System;
using System.Collections.Generic;
public class AvatarPostProcessor : AssetPostprocessor
{
void OnPostprocessModel(GameObject obj)
{
if (shouldPostProcess(obj))
{
foreach (var mf in obj.GetComponentsInChildren<MeshFilter>())
applyDefaultVertexColor(mf.sharedMesh);
foreach (var skin in obj.GetComponentsInChildren<SkinnedMeshRenderer>())
applyDefaultVertexColor(skin.sharedMesh);
EditorUtility.SetDirty(obj);
AssetDatabase.SaveAssets();
}
}
private bool shouldPostProcess(GameObject obj)
{
return obj.name == "avatar" ||
this.assetPath.ContainsFast("Character/3p/equipment", false);
}
private static void applyDefaultVertexColor(Mesh sharedMesh)
{
Color[] colors = new Color[sharedMesh.vertexCount];
for (int i = 0; i < colors.Length; i++)
colors[i] = new Color32(0, 0, 0, 0);
sharedMesh.colors = colors;
}
}
This applies the vertex color to the imported mesh correctly, when i restart unity it is still there. However the original file does not change so i cannot commit this to SVN. Is there any way to store these changes in the FBX file?
Thanks!
Well it seems like changes like this are included in assetbundles, which is good. Other than that i think the only way is to actually go in and change the FBX.
Sorry to necro but have you found a solution for this?
I am having similar issues where I try to set the colors of the shared mesh, but the changes are not persisted.
The second post above indicates that the changes do “stick” in the sense that they are reapplied each import, assuming this import script is present and in fully working order at the time of import. Are you finding that is not the case?
Unity will NEVER manipulate your third party assets, only Unity assets (prefabs, materials, scenes, etc.)
Everything about a third-party format is on you to edit. That’s the boundary of the Unity world.
1 Like
I am finding that if I use the following code in edit-mode
Color[] colors = new Color[sharedMesh.vertexCount];
for (int i = 0; i < colors.Length; i++)
colors[i] = new Color32(1, 0, 0, 0);
sharedMesh.colors = colors;
EditorUtility.SetDirty(sharedMesh);
AssetDatabase.SaveAssets();
that the vertex colors do get modified (I see this because I use a shader that outputs the vertex colors) but then when I close and re-open unity, the vertex colors have been reset to white and I do not get why. I understood that OP had the same issues by saying that the ‘changes do not persist’ but maybe I am having a different issue.
However, I have found some editor tools that do what I need to do and will take a look at their source code to find out what I’m missing.
I think that won’t quite work, but you could be able to make it work with these changes:
- instead of dirtying and trying to save the shared mesh, instantiate a copy of the mesh and write that to a file on disk.
This would become a .asset file and you would instead hook that up wherever you wanted the colored verts.
Here’s an editor-mode codelet for writing meshes to disk in Unity:
https://github.com/kurtdekker/makegeo/blob/master/makegeo/Assets/Editor/MeshMakerAndSaver.cs
1 Like
The original code of this thread is using an AssetPostprocessor. It runs everytime Unity does re-import the asset in order to post process it. So automatic changes can be applied that way. However custom changes that the user can make / trigger on demand are of course lost as you just modified the imported version of the mesh that may get overwritten whenever the model is re-imported.
If you have custom editor tools that should modify a mesh inside the editor, you can make a copy of the mesh and store it yourself as a .asset
file. This mesh asset would not be modifed by Unity as it is not related to the original model. But that’s also the issue with this approach. The mesh would be disconnected from the source file it came from. So editing / updating the source file would of course not affect your duplicated mesh asset.
Yes, there are tools out there which allow certain kind of modifications of imported assets. However they always have to do one of the following two:
- Create a duplicate of the mesh so it becomes an independent asset that can be manipulated in Unity
- Record and store the actual actions / modifications the user does to the asset so they can be reapplied in a post processor if the asset is reimported.
The second approach has a view potential issues. If the model was actually edited outside of Unity, the reimported mesh could look completely different. So it may be hard up to impossible to apply “the same modifications” to the mew mesh. So such systems always have limitations.
4 Likes