So i am try make one map and blip system with GUI, so if the blip position is one center of map, the blip(image) position is center of image. I try make but failed. the script: Code (CSharp): using UnityEngine; using System.Collections; [ExecuteInEditMode] public class MapBehaviour : MonoBehaviour { public Texture mapTexture; // map texture [SerializeField]private float mapLimitX,mapLimitY,mapScreenX,mapScreenY; // map real size x and y, map screen size x and y(iam use resolution 800:600) public bool isShowing = false; // render this? public BlipBehaviour[] blips; // blip array void OnGUI(){ if(isShowing){ RenderMap(); // render map function } } void RenderMap(){ // draw map in center screen, resize to all resolutions GUI.DrawTexture(new Rect(Screen.width/2-GetRelativePosition(mapScreenX/2,true),Screen.height/2-GetRelativePosition(mapScreenY/2,false),GetRelativePosition(mapScreenX,true),GetRelativePosition(mapScreenY,false)),mapTexture); // get all blips array for(int i=0;i<blips.Length;i++){ // calcule x blip position float _x = GetBlipScreenPosition(blips.transform.position.x,mapLimitX,Screen.width,blips.sizeX,mapScreenX); // calcule y blip position float _y = GetBlipScreenPosition(blips.transform.position.y,mapLimitY,Screen.height,blips.sizeY,mapScreenY); _x = GetRelativePosition(_x,true); // resize x blip position to all resolution _y = GetRelativePosition(_y,false); // resize y blip position to all resolution Rect rectangle = new Rect( // rect (_x), (_y), GetRelativePosition(blips.sizeX,true), // resize blip size x GetRelativePosition(blips.sizeY,false)); // resize blip size y GUI.DrawTexture(rectangle,blips.icon); // draw } } float GetBlipScreenPosition(float blipP, float mpRealSize,float screen, float blipSize, float mpScreen){ // util functions float r = (blipP/mpRealSize)*screen*(mpScreen/screen); return r; } float GetRelativePosition(float a,bool isWidth){ // util functions 2 float b = 0; float c = 0; if(isWidth){ b = Screen.width; c = 800f; } else{ b = Screen.height; c = 600f; } return(a/c)*b; } }
Ok, sorry for this Code (CSharp): using UnityEngine; using System.Collections; public class BlipBehaviour : MonoBehaviour { public float sizeX,sizeY; public Texture icon; public string iconName; void Start(){ icon = Resources.Load<Texture>(iconName); } }
A lot of your problems come from accessing the variable blips (which is an array of BlipBehaviours) as though it were a single blip, i.e., dereferencing public member variables, accessing its .transform property, etc. Instead I made a variable called blip that references the single blip we are working on in the for loop. That must be what you are intending to do, I think. Code (csharp): using UnityEngine; using System.Collections; [ExecuteInEditMode] public class MapBehaviour : MonoBehaviour { public Texture mapTexture; // map texture [SerializeField]private float mapLimitX,mapLimitY,mapScreenX,mapScreenY; // map real size x and y, map screen size x and y(iam use resolution 800:600) public bool isShowing = false; // render this? public BlipBehaviour[] blips; // blip array void OnGUI(){ if(isShowing){ RenderMap(); // render map function } } void RenderMap(){ // draw map in center screen, resize to all resolutions GUI.DrawTexture(new Rect(Screen.width/2-GetRelativePosition(mapScreenX/2,true),Screen.height/2-GetRelativePosition(mapScreenY/2,false),GetRelativePosition(mapScreenX,true),GetRelativePosition(mapScreenY,false)),mapTexture); // get all blips array for(int i=0;i<blips.Length;i++){ // get a reference to ONE blip out of the blips array BlipBehaviour blip = blips[i]; // calcule x blip position float _x = GetBlipScreenPosition(blip.transform.position.x,mapLimitX,Screen.width,blip.sizeX,mapScreenX); // calcule y blip position float _y = GetBlipScreenPosition(blip.transform.position.y,mapLimitY,Screen.height,blip.sizeY,mapScreenY); _x = GetRelativePosition(_x,true); // resize x blip position to all resolution _y = GetRelativePosition(_y,false); // resize y blip position to all resolution Rect rectangle = new Rect( // rect (_x), (_y), GetRelativePosition(blip.sizeX,true), // resize blip size x GetRelativePosition(blip.sizeY,false)); // resize blip size y GUI.DrawTexture(rectangle,blip.icon); // draw } } float GetBlipScreenPosition(float blipP, float mpRealSize,float screen, float blipSize, float mpScreen){ // util functions float r = (blipP/mpRealSize)*screen*(mpScreen/screen); return r; } float GetRelativePosition(float a,bool isWidth){ // util functions 2 float b = 0; float c = 0; if(isWidth){ b = Screen.width; c = 800f; } else{ b = Screen.height; c = 600f; } return(a/c)*b; } }
So thanks, but my problem is with math, let's say Map Limit x and y is 60, Map Screen x is 400, Map Limit y is 300, and the blip position is (30,30), so the blip position in screen should be (400,300) center of screen. But this not happen, the blip screen position is (200,300), i try multiply by 2, but no work with all possible value from blip position(only work if blip position in map is center). Look in the Red Square(the blip) Wrong: Right:
You're doing a linear scaling mapping of a 2-dimensional vector, so without looking at your code, here are the steps you must do to get each axis from one coordinate system to the other: - first subtract out the notional "zero" (could be the center, could be lower left, upper left, etc.) of the world where the game object that you are showing the position of is, in your world - multiply by the size of the full deflection of the target coordinate system (in this case your minimap width and height in OnGUI coordinate space) - divide by the size of the full deflection of your world (where we started out above) in world space - possibly invert the Y value's sign (OnGUI Y coordinates get larger going down, whereas world Y coordinates get larger going up) - finally add in the zero of the minimap, whatever you choose that to be such that it correlates with the in-game zeropoint. These are the steps and they must be done in this particular order for each X and Y axis. Check your code over, and once you are doing the steps above, your coordinate scaling should work fine. Obviously, you should put in test code to put one map element at the zero, make sure it shows up at the zero. Then move it over 50% and 50% up and make sure it works. Step by step you will solve it with the guide above.