I have created a script with the help of this community to resize a gameobject when pressing Play in the Editor. The script is successful. My next step will be to upload the resized object to a database which I’m having problems figuring out. Anyways. The problem is, when I press Play again to be taken out of “Game” mode. The meshes retains the resize, but the transform appears to go BACK to before I hit play. Here are some screenshots, and then the code.
Before Play, very large object
After Hitting Play
Exiting Game Mode (scattered tiny pieces of couch everywhere)
My code recalculates bounds, normals, and everything. The Prefab itself is then appearing as scattered couch pieces. I’m worried if I try to upload this to the database it won’t be properly saved with the proper transforms.
Here is the code.
public class HiddenMonkTest : MonoBehaviour {
public float setRealLifeSize;
Vector3 center;
Vector3 size;
float largestDistance;
float scale;
MeshFilter[] objMeshFilter;
CombineInstance[] combineMeshFilter;
void Awake()
{
List<Vector3[]> allWorldCoords = new List<Vector3[]>();
List<Vector3> centers = new List<Vector3>();
MeshFilter[] objMeshFilter = this.GetComponentsInChildren<MeshFilter>();
foreach (Renderer render in transform.GetComponentsInChildren<Renderer>())
{
allWorldCoords.Add(BoundsToWorldCoordinates(render.bounds));
centers.Add(render.bounds.center);
}
center = Average(centers.ToArray()); //find center by getting average of all centers
Vector3[] totalBound = new Vector3[8]; //we make each vector a default of the center vector so we can compare with sqrMagnitude
for (int i = 0; i < totalBound.Length; i++)
{
totalBound[i] = center;
}
for (int i = 0; i < allWorldCoords.Count; i++)
{
for (int j = 0; j < totalBound.Length; j++)
{
if ((center - allWorldCoords[i][j]).sqrMagnitude > (center - totalBound[j]).sqrMagnitude)
{
totalBound[j] = allWorldCoords[i][j];
}
}
}
size = GetCubeSize(totalBound);
SetGreatesttDistance();
SetScale();
ResizeMeshCollection(objMeshFilter);
Debug.Log(size.x);
Debug.Log(size.y);
Debug.Log(size.z);
}
void ResizeMeshCollection(MeshFilter[] collection)
{
foreach (MeshFilter mf in collection)
{
mf.GetComponent<MeshFilter>();
Transform tf = mf.GetComponent<Transform>();
Mesh mesh = mf.sharedMesh;
Bounds bounds = mf.GetComponent<Renderer>().bounds;
Vector3[] verts = mesh.vertices;
for (int i = 0; i < verts.Length; i++)
{
verts[i].x = verts[i].x * scale;
verts[i].y = verts[i].y * scale;
verts[i].z = verts[i].z * scale;
}
tf.position = new Vector3((tf.position.x - center.x)* scale, (tf.position.y - center.y) * scale, (tf.position.z - center.z) * scale);
mesh.vertices = verts;
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
}
Vector3[] BoundsToWorldCoordinates(Bounds bounds)
{
return BoundsToWorldCoordinates(bounds.center, bounds.extents);
}
//
Vector3[] BoundsToWorldCoordinates(Vector3 position, Vector3 extents)
{
Vector3[] worldCoordinates = new Vector3[8];
worldCoordinates[0] = new Vector3(extents.x, extents.y, extents.z);
worldCoordinates[1] = new Vector3(extents.x, extents.y, -extents.z);
worldCoordinates[2] = new Vector3(extents.x, -extents.y, extents.z);
worldCoordinates[3] = new Vector3(extents.x, -extents.y, -extents.z);
worldCoordinates[4] = new Vector3(-extents.x, extents.y, extents.z);
worldCoordinates[5] = new Vector3(-extents.x, extents.y, -extents.z);
worldCoordinates[6] = new Vector3(-extents.x, -extents.y, extents.z);
worldCoordinates[7] = new Vector3(-extents.x, -extents.y, -extents.z);
for (int i = 0; i < worldCoordinates.Length; i++)
{
worldCoordinates[i] += position;
}
return worldCoordinates;
}
Vector3 GetCubeSize(Vector3[] vectors)
{
Vector3 center = Average(vectors);
Vector3 size = Vector3.zero;
float[] x = vectors.Select(a => a.x).ToArray();
float[] y = vectors.Select(a => a.y).ToArray();
float[] z = vectors.Select(a => a.z).ToArray();
float xMin = x.Min();
float xMax = x.Max();
float yMin = y.Min();
float yMax = y.Max();
float zMin = z.Min();
float zMax = z.Max();
size.x = xMax - xMin;
size.y = yMax - yMin;
size.z = zMax - zMin;
return size;
}
Vector3 Average(params Vector3[] vectors)
{
return AddAll(vectors) / vectors.Length;
}
Vector3 AddAll(params Vector3[] vectors)
{
Vector3 vectorsSum = Vector3.zero;
foreach (Vector3 vector in vectors)
{
vectorsSum += vector;
}
return vectorsSum;
}
void SetGreatesttDistance()
{
float localHighest;
localHighest = size.x;
if (localHighest < size.y) localHighest = size.y;
if (localHighest < size.z) localHighest = size.z;
largestDistance = localHighest;
Debug.Log(largestDistance);
}
void SetScale()
{
scale = setRealLifeSize / largestDistance;
}
}