Hi Guys, For a full day now I've been trying to scale one object, to a percentage size of another object (2 sprites). I've created two objects with a SetInitialSize() function. The first sets it's own size to the viewport, keeping it's aspect ratio (working correctly). The second one, takes that object's size AFTER it's been scaled to the viewport, and is made to a percentage size of that sprite. The issue being that I'm not able to find the size of the sprite after it's been scaled. FIRST ONE CALLED Code (CSharp): public bool SetInitialSize() { SpriteRenderer sr = GetComponent<SpriteRenderer>(); if(sr) { Camera cam; if(cam = transform.parent.GetComponent<Camera>()) { float worldScreenHeight = (float)(cam.orthographicSize * 2.0); float worldScreenWidth = worldScreenHeight / Screen.height * Screen.width; float newLocalScale = (worldScreenWidth / sr.sprite.bounds.size.x); Debug.Log("BEFORE" + GetComponent<SpriteRenderer>().sprite.bounds.size); transform.localScale = new Vector3(newLocalScale, newLocalScale, 1); SetScaleClamp(transform.localScale); Debug.Log("AFTER" + GetComponent<SpriteRenderer>().sprite.bounds.size); } else { Debug.Log("MapHandler::SetInitialSize() - Camera not found as parent to Map Object!"); return false; } } else { Debug.Log("MapHandler::SetInitialSize() - SpriteRenderer Component not found!"); return false; } return true; } SECOND ONE CALLED Code (CSharp): public bool SetInitialSize() { SpriteRenderer sr = GetComponent<SpriteRenderer>(); if (sr) { Camera cam; if (cam = GameObject.Find("MapViewCamera").GetComponent<Camera>()) { // Grid height in pixels from Map Bounds mapBounds = GameObject.Find("Map").GetComponent<SpriteRenderer>().sprite.bounds; Bounds imgBounds = GetComponent<SpriteRenderer>().sprite.bounds; float sizeY = imgBounds.size.y; float gridSize = mapBounds.size.y / 6; Vector3 newScale = new Vector3(gridSize / sizeY, gridSize / sizeY, gridSize / sizeY); transform.localScale = newScale; // Set correct z position according to parent (should be the map) Vector3 pos = transform.position; pos.z = transform.parent.transform.position.z - 1; transform.position = pos; } else { Debug.Log("ProtractorHandler::SetInitialSize() - Camera not found as parent to Map Object!"); return false; } } else { Debug.Log("ProtractorHandler::SetInitialSize() - SpriteRenderer Component not found!"); return false; } return true; } AND HERE IS WHERE THEY'RE CALLED AND CONTROLLED Code (CSharp): void OnEnable() { if (bInitiliased) { if (!SetInitialSizeObjects()) Debug.Log("MapObjectHandler::OnEnable - SetInitialSizeObjects Failed!"); } } bool SetInitialSizeObjects() { if(map) { MapHandler mapHandler; if (mapHandler = map.GetComponent<MapHandler>()) { if (mapHandler.SetInitialSize()) { if(protractor) { ProtractorHandler protHandler; if (protHandler = protractor.GetComponent<ProtractorHandler>()) { if(protHandler.SetInitialSize()) { return true; } The main issue is that the bounds of the object don't change before or after setting localScale, so there's no way to find it's object size. The Console is returning: BEFORE(44.4, 29.5, 0.2) AFTER(44.4, 29.5, 0.2) I've tried changing the object to different scales before the game starts, to make sure there IS a change in size. Looking at the game visually. It is actually changing it's size. Can anyone make any suggestions? Thanks!
To double check that there isn't any code changing it's scale before these scripts are doing anything I've added: Code (CSharp): void Start () { Debug.Log("STARTING" + GetComponent<SpriteRenderer>().sprite.bounds.size); bInitiliased = true; } void OnDisable() { Debug.Log("INITIAL" + GetComponent<SpriteRenderer>().sprite.bounds.size); } (On start up the object is disabled until it's used) which also returns: INITIAL(44.4, 29.5, 0.2), STARTING(44.4, 29.5, 0.2) There is no change to bounds. Though visually, there is obviously a change. What is happening with Sprite Bounds?
After doing more research I've found that the object is scaling correctly, however it's scaling to the sprite's original size, ignoring localScale. Is there a way to find it's pixel size after localScale has been applied? Or am I to manually calculate the pixel size by reducing the the localScale as a percentage from the total image size?
Sprite.bounds does not change as a reaction to changing the scale of your object. It is the bounds of the sprite in conjunction with Sprite.pixelsPerUnit. Meaning, it only changes if either the sprite itself changes (obviously), or if Sprite.pixelsPerUnit changes. In other words, a Sprite is not aware of the game objects it is used on.
This now makes sense. Does that mean it's impossible to scale one sprite in proportion to another one's scale, without really going into some maths on their theoretic pixel size based on their original bounds and their current localScale? It seems like I'd need to make some formula for that... ?
Unless the sprites are exactly the same size, it's going to involve a little math. It shouldn't be very hard, though, since you can always relate sprites to one another in terms of relative sizes. For example, a sprite that is 100x100 is exactly 0.5 times the size of another sprite that is 200x200. (Assuming the same pixelsPerUnit value, of course. If those are not the same, you need to convert for that as well.)
Yeah you were right, I only needed a simple converter for my script to make it work. I added this from another static class, for anyone struggling with the same issue . Code (CSharp): public static Vector3 CurrentObjectSize(Vector3 objBoundsSize, Vector3 objLocalScale) { Vector3 newObj; newObj.x = objBoundsSize.x * objLocalScale.x; newObj.y = objBoundsSize.y * objLocalScale.y; newObj.z = objBoundsSize.z * objLocalScale.z; return newObj; } Code (CSharp): public bool SetInitialSize() { SpriteRenderer sr = GetComponent<SpriteRenderer>(); if (sr) { Camera cam; if (cam = GameObject.Find("MapViewCamera").GetComponent<Camera>()) { SpriteRenderer m_sr; if(m_sr = GameObject.Find("Map").GetComponent<SpriteRenderer>()) { Bounds mapBounds = m_sr.sprite.bounds; mapBounds.size = Helper.CurrentObjectSize(m_sr.sprite.bounds.size, m_sr.transform.localScale); float sizeY = sr.sprite.bounds.size.y; float gridSize = (mapBounds.size.y / 6) / (protMeasurePercentSize / 100); Vector3 newScale = new Vector3(gridSize / sizeY, gridSize / sizeY, gridSize / sizeY); transform.localScale = newScale; // Set correct z position according to parent (should be the map) Vector3 pos = transform.position; pos.z = cam.transform.position.z + 1; transform.position = pos; } else { Debug.Log("ProtractorHandler::SetInitialSize - Unable to find SpriteRenderer component in Map GameObject!"); return false; } } else { Debug.Log("ProtractorHandler::SetInitialSize() - Camera not found as parent to Map Object!"); return false; } } else { Debug.Log("ProtractorHandler::SetInitialSize() - SpriteRenderer Component not found!"); return false; } return true; } }