- Home /
Remap UVs and persist the changes
Hi,
I have a set of around 50 prefabs each one with its own LODs. I'm trying to remap the UVs of all the meshes to my atlas texture using a C# script in Unity because I don't want to make it one by one from Blender. I've done the code that makes de trick but I'm not able to persist the changes so when I close the Unity editor, next time I open it all the changes are lost.
I've tryed to execute this code at runtime on an instance of the prefab and Apply then the changes to update the prefab. Everything seems ok until I shut down the editor and open it again .
I've tryed to execute this code in Editor mode (not runtime) directly on the prefab (with an editor script and a button). Same as before: the remap seems ok but after shuting down Unity and opening it again all the changes are vanished.
Someone can give me an idea of what's going on?
Many thanks guys
PS: my remap code
MeshFilter[] meshes = objecte.transform.GetComponentsInChildren<MeshFilter>();
foreach (var m in meshes)
{
var mesh = m.sharedMesh;
List<Vector2> uvs = new List<Vector2>();
List<Vector2> newUvs = new List<Vector2>();
mesh.GetUVs(0, uvs);
for (int i = 0; i < uvs.Count; i++)
{
var v2 = new Vector2((uvs[i].x * factorEscalatX) + offsetX, (uvs[i].y * factorEscalatY) + offsetY);
newUvs.Add(v2);
}
mesh.SetUVs(0, newUvs);
}
//AssetDatabase.SaveAssets(); useless :(
Check out this asset - it's free on the asset store. Basically, what you're doing now is changing Unity's copy of the object, but not writing to the file. There are ways to do it manually, but if you're using .OBJ files, that asset is super useful - if you're not, there are paid assets for most other file types, or you can try your hand at writing your own or adapting the one I linked to work for different file types.
Hi, thanks for answering. I'm using FBX files but it doesn't matter, I will check this asset (It seems a bit more complex for what I need but I will parse and study it anyway). The thing that drives me crazy is that I read everywhere that working on the meshfilter.shared$$anonymous$$esh should update automatically the prefabs. It's not working for me don't know why...
Your answer
Follow this Question
Related Questions
how do i stop instantiate from changing a prefab 0 Answers
Dynamically Loading audio into a prefab and Instantiate in scene. 0 Answers
Unity 5 : Best way to initialize a UI panel coming from a prefab with many text fields ? 0 Answers
Prefabs won't change? 1 Answer
Multiple Prefab in Object Pool or One Prefab With Add Component 0 Answers