I wrote a script that runs at start and utilizes the FindObjectsOfTypeAll(typeof(GameObject) function to iterate through all of the objects in the scene and adjust their position and scale based on the screen’s aspect ratio. When I press play it seems to work well enough, but when I press stop the game objects don’t always return to their original positions. Not only that, but I find that the transforms of the prefabs linked to those objects have also been changed. I can’t reload the scene and get back their original positions, the change is permanent.
So… what’s up with that? This seems like a bug to me. Here’s the relevant bit of code:
Vector3 deltaWorld = urMarker - blMarker;
Vector3 deltaScreen = urScreen - blScreen;
float xRatio = Mathf.Abs (deltaScreen.x / deltaWorld.x);
float zRatio = Mathf.Abs (deltaScreen.z / deltaWorld.z);
GameObject[ ] allObjects = (GameObject[ ])Resources.FindObjectsOfTypeAll(typeof(GameObject));
foreach (GameObject anObject in allObjects)
{
Transform anObjectTransform = anObject.transform;
float deltaMarkerX = anObjectTransform.position.x - blMarker.x;
float deltaMarkerZ = anObjectTransform.position.z - blMarker.z;
Vector3 newPosition = Vector3.zero;
newPosition.y = anObjectTransform.position.y;
newPosition.x = (deltaMarkerX * xRatio) + blScreen.x;
newPosition.z = (deltaMarkerZ * zRatio) + blScreen.z;
anObjectTransform.position = newPosition;
}