- Home /
Question by
Djikky · Jun 01, 2018 at 02:59 PM ·
c#meshmeshfiltersharedmeshreplace mesh
Replace MeshFilter mesh by a other mesh in Editor
Hi,
I've created a script to replace (in Editor) all the MeshFilter's mesh in my scene by the mesh "meshToAssign" but i'm having trouble to get my mesh list.
Here is my code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReplaceMesh : MonoBehaviour
{
public Mesh meshToReplace;
public Mesh meshToAssign;
public List<MeshFilter> meshFilterToWorkOn;
[ContextMenu("FindMeshRendererToWorkOn")]
public void FindMeshRendererToWorkOn()
{
meshFilterToWorkOn.Clear();
MeshFilter[] meshFilterAll = FindObjectsOfType<MeshFilter>();
int meshFilterAllLength = meshFilterAll.Length;
for (int i = 0; i < meshFilterAllLength; i++)
{
if (meshFilterAll[i].sharedMesh == meshToReplace)
meshFilterToWorkOn.Add(meshFilterAll[i]);
}
}
[ContextMenu("ReplaceAllMeshInList")]
public void ReplaceAllMeshInList()
{
int meshFilterToWorkOnCount = meshFilterToWorkOn.Count;
for (int i = 0; i < meshFilterToWorkOnCount; i++)
meshFilterToWorkOn[i].mesh = meshToAssign;
}
}
It seems like the variable "meshToReplace" isn't equal to "meshFilterAll[i].sharedMesh" but when i tryed with "meshFilterAll[i].mesh" a error has appear in the console when i execute the command.
Can you guys help me ?
Comment
what exactly do you mean when you say is not equal? and yes, you must not use the mesh accessor during edit mode.
Answer by Djikky · Jun 04, 2018 at 08:52 AM
It wasn't equal to sharedMesh cause meshToReplace was a assetMesh and not a sharedMesh. But it's working now ! I just had to get the sharedMesh of my MeshFilter.
public class ReplaceMesh : MonoBehaviour
{
public MeshFilter meshToReplace;
public Mesh meshToAssign;
public List<MeshFilter> meshFilterToWorkOn;
[ContextMenu("FindMeshRendererToWorkOn")]
public void FindMeshRendererToWorkOn()
{
Mesh sharedMesh = meshToReplace.sharedMesh;
meshFilterToWorkOn.Clear();
MeshFilter[] meshFilterAll = FindObjectsOfType<MeshFilter>();
int meshFilterAllLength = meshFilterAll.Length;
for (int i = 0; i < meshFilterAllLength; i++)
{
if (meshFilterAll[i].sharedMesh == sharedMesh)
meshFilterToWorkOn.Add(meshFilterAll[i]);
}
}
[ContextMenu("ReplaceAllMeshInList")]
public void ReplaceAllMeshInList()
{
int meshFilterToWorkOnCount = meshFilterToWorkOn.Count;
for (int i = 0; i < meshFilterToWorkOnCount; i++)
meshFilterToWorkOn[i].mesh = meshToAssign;
}
}
Thanks for u reply !