- Home /
Delete Uv2 of a mesh?
Hi all,
I have been creating secondary Uv during runtime. Now after creating it i would want to destroy the secondary Uv.
How do you delete the secondary UV in runtime ? I Hope, I’m clear enough to make my question undestandable. Thanks in advance.
Answer by AlucardJay · Apr 10, 2018 at 09:23 AM
Disclaimer: I've never played with the uv2 channel, so these are the rantings of a madman.
I see 2 options :
1/ Assign the uv2 as null
2/ Create a copy of the mesh, but without assigning any uv2
example script :
In a new project, new scene, create an empty gameObject. Attach the following script, and add a material with a texture to see what's happening.
In play mode : press G to generate mesh with UVs; press R to null the UVs; press U to make a copy without UVs.
using UnityEngine;
using System.Collections;
[RequireComponent( typeof(MeshFilter), typeof(MeshRenderer) )]
public class MeshRemoveUV : MonoBehaviour
{
// ------------------------------------------------------- Persistent Functions
void Start()
{
GetComponent< MeshFilter >().mesh = GenerateMesh();
}
void Update()
{
if ( Input.GetKeyDown( KeyCode.G ) )
{
GetComponent< MeshFilter >().mesh = GenerateMesh();
return;
}
if ( Input.GetKeyDown( KeyCode.R ) )
{
GetComponent< MeshFilter >().mesh = NullifyUV( GetComponent< MeshFilter >().mesh );
return;
}
if ( Input.GetKeyDown( KeyCode.U ) )
{
GetComponent< MeshFilter >().mesh = CopyMeshWithoutUV( GetComponent< MeshFilter >().mesh );
}
}
// ------------------------------------------------------- Other Functions
Mesh NullifyUV( Mesh mesh )
{
mesh.name = "NullUVs";
mesh.uv = null;
return mesh;
}
Mesh CopyMeshWithoutUV( Mesh meshWithUVs )
{
Mesh mesh = new Mesh();
Vector3[] verts = new Vector3[ meshWithUVs.vertices.Length ];
System.Array.Copy( meshWithUVs.vertices, verts, meshWithUVs.vertices.Length );
int[] tris = new int[ meshWithUVs.triangles.Length ];
System.Array.Copy( meshWithUVs.triangles, tris, meshWithUVs.triangles.Length );
Vector3[] norms = new Vector3[ meshWithUVs.normals.Length ];
System.Array.Copy( meshWithUVs.normals, norms, meshWithUVs.normals.Length );
mesh.name = "WithoutUVs";
mesh.vertices = verts;
mesh.triangles = tris;
mesh.normals = norms;
mesh.RecalculateBounds();
return mesh;
}
Mesh GenerateMesh()
{
Mesh mesh = new Mesh();
Vector3[] verts = new Vector3[ 4 ];
verts[ 0 ] = new Vector3( 0, 1, 0 );
verts[ 1 ] = new Vector3( 1, 1, 0 );
verts[ 2 ] = new Vector3( 0, 0, 0 );
verts[ 3 ] = new Vector3( 1, 0, 0 );
Vector3[] norms = new Vector3[ 4 ];
norms[ 0 ] = -Vector3.forward;
norms[ 1 ] = -Vector3.forward;
norms[ 2 ] = -Vector3.forward;
norms[ 3 ] = -Vector3.forward;
int[] tris = new int[ 6 ];
tris[ 0 ] = 0;
tris[ 1 ] = 1;
tris[ 2 ] = 2;
tris[ 3 ] = 2;
tris[ 4 ] = 1;
tris[ 5 ] = 3;
Vector2[] uvs = new Vector2[ 4 ];
uvs[ 0 ] = new Vector2( 0, 1 );
uvs[ 1 ] = new Vector2( 1, 1 );
uvs[ 2 ] = new Vector2( 0, 0 );
uvs[ 3 ] = new Vector2( 1, 0 );
mesh.name = "HasUVs";
mesh.vertices = verts;
mesh.triangles = tris;
mesh.normals = norms;
mesh.uv = uvs;
mesh.RecalculateBounds();
return mesh;
}
}
Hey thank you for the reply, the option 2 mentioned above works well. But there is another issue now, once after generating the new mesh , and then i try to create a prefab of the gameobject. It shows the mesh is missing.
How do you create a prefab from the newly generated mesh ? I Hope, I’m clear enough to make my question undestandable. Thanks in advance.
Do you want to : 1/ use the mesh only in the same play session; or 2/ use the mesh after the game has been closed, and then restarted?
Option 2. I need to use the mesh after the game has been closed and then restarted.
Thanks in advance.