So I’m trying to scale my background sprite(s) to fill the screen, there are 2 a regular background image, and a sprite that lays over it with alpha transparency to fake shadows (since lighting doesn’t apply to 2d sprites) the issue is i cant seem to find that magical float to multiply my sprite width/height by to get the desired size (maybe i just suck at math. My First attempt was:
percentW = (float)scrW/(float)imgW;
percentH = (float)scrH/(float)imgH;
which gives me 0.3125/0.3125
but in playing with inspector variables i found that for this particular resolution i need 0.8/0.8 so i looked online and found this code:
float height = 2.0f * Mathf.Tan(0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad);
float width=height * scrW/scrH;
which gives me 0.649 for height, closer but still not perfect. I can hard code a close approximation by multiplying by 1.3 to get .84 but i want .8 almost exactly, and its driving me nuts.
This is going to be an android title meaning tons and tons of varying resolutions so of course independence is of the utmost importance. So has anyone figured out how to do this yet?
This helped me MASSIVELY in my project, cheers :)
– CoadjutantHI Robert, When I use this code, monodevelop says Error: Assets/C#Scripts/MainMenu1.cs(45,35): error CS1061: Type
– Henobject' does not contain a definition for
sprite' and no extension methodsprite' of type
object' could be found (are you missing a using directive or an assembly reference?)@Hen - The code above compiled fine for me. I just tested it again to make sure. What changes did you make from what is here?
– ChangelingI hv copied it into a CS Script, and it puked these errors. I will work it for CS thank you for the code. I think this transforms all the sprites isn't it?
– HenThe above code scales a sprite to fill the screen. And yes the code will need a bit of changing for C#.
– Changeling