// Get the sacaling values based on the textures width and height scaling getscalingFromTexture(int mtW, int mtH) { scaling sc = new scaling(FrameObj.transform.localScale); float s = 1; //scale to scale the sprite object by sc.origSize = new Vector2(mtW, mtH); // If height AND width are less than the max values, then ONLY rescale // the frame if (mtH < maxHeight && mtW < maxWidth) { sc.frameScale.x = (mtW / maxWidth) * sc.frameScale.x; sc.frameScale.y = (mtH / maxHeight) * sc.frameScale.y; sc.onlyFrameIsScaled = true; return(sc); } // If width is greater than max width, Rescale image based of width fraction if (mtW > maxWidth) { s = maxWidth / mtW; // Perform scaling for the frame bounding inverse to the image sprite scaling sc.frameScale.x = (1 / s) * sc.frameScale.x; sc.frameScale.y = (mtH / maxHeight) * sc.frameScale.y; sc.widthCorrected = true; } // If, after the previous transform (or if no transform was made), // the height is still greater than max height, then rescale image // according to the height ratio btw image height and max height if (mtH * s > maxHeight) { s = maxHeight / (mtH); sc.heightCorrected = true; } sc.scale = s; return(sc); }
// This function is the setup function that opens the image and loads the sprite with it public scaling initiateSprite(Sprite sprite, int tw, int th) { //Adjust for variation in initial max width and heitght w.r.t 420 reference rt = GetComponent <RectTransform>(); rt.localScale = new Vector3(maxWidth / 420, maxHeight / 420, transform.localScale.z); int i = 0; GetComponent <Image>().sprite = sprite; // Attach the sprite to the image UI // Obtain the scaling needed from the texture scaling sc = getscalingFromTexture(tw, th); // Scale the image frame foreach (maintainFrameSize mfsScript in FindObjectsOfType <maintainFrameSize>()) { mfsScript.setSize(); } transform.localScale = sc.scale; foreach (maintainFrameSize mfsScript in FindObjectsOfType <maintainFrameSize>()) { mfsScript.restoreSize(); } return(sc); //return new scaling(rt.localScale); }
// This function is the setup function that opens the image and loads the sprite with it public scaling initiateSprite(Sprite sprite, int tw, int th) { //Adjust for variation in max width and heithg FrameObj.transform.localScale = new Vector3(maxWidth / 420, maxHeight / 420, FrameObj.transform.localScale.z); scaling sc = getscalingFromTexture(tw, th); SpriteObj.GetComponent <SpriteRenderer>().sprite = sprite; // Set the sprite object of the sprite renderer SpriteObj.transform.localScale = new Vector3(sc.scale, sc.scale, 1); // Rescale the sprite to fit max size FrameObj.transform.localScale = sc.frameScale; // Adjust the bounding frame of the image sprite RectTransform rt = SpriteObj.GetComponent <RectTransform>(); rt.sizeDelta = new Vector2(4.2f / (sc.frameScale.x * sc.scale), 4.2f / (sc.frameScale.y * sc.scale)); return(sc); }
// Get the sacaling values based on the textures width and height scaling getscalingFromTexture(int mtW, int mtH) { // The texture will be mapped to an area that is // x: maxWidth units // y: maxHeight units scaling sc = new scaling(rt.localScale); // Initialize a new scaling object using the current scaling float s = 1; //scale to scale the sprite object by sc.origSize = new Vector2(mtW, mtH); // If height AND width are less than the max values, then ONLY rescale the frame if (mtH < maxHeight && mtW < maxWidth) { sc.scale.x = (mtW / maxWidth) * sc.scale.x; sc.scale.y = (mtH / maxHeight) * sc.scale.y; sc.frameIsScaledDown = true; sc.scaleDownFactor = 1; // We're scaling down to the size of the frame, therefore the image itself wasn't scaled return(sc); } // If height OR width OR both are greater than max values else { // If width is proportionally greaterer than max width, Rescale height if (mtW / maxWidth > mtH / maxHeight) { sc.scaleDownFactor = maxWidth / mtW; //Scaled TO maxWidth from mtW (actual pixels width) sc.scale.y = (mtH / maxHeight) * sc.scaleDownFactor * sc.scale.y; sc.widthMaxxed = true; } else // Rescale width { sc.scaleDownFactor = maxHeight / mtH; //Scaled TO maxHeight from mtW (actual pixels height) sc.scale.x = (mtW / maxWidth) * sc.scaleDownFactor * sc.scale.x; sc.heightMaxxed = true; } } return(sc); }