- Home /
Problem making a ruler
Hello, I'm trying to create a ruler by C# scrip, the proble is that the labels of the numbers are not put in correspondence with the rulers, this is because the rulers are GameObjects that have world coordinates and the labels that are guiText attached to the gameObject having as coordinates the pixeloffset.
Below how is the result of the script and the script itself, someone can help me? Thanks in advance!
using UnityEngine;
using System.Collections;
public class GridController : MonoBehaviour {
public float stepSize = 0.05f;
public GameObject line, ruler;
void Start(){
rulerInit();
}
public void rulerInit(){
// delete previous ruler
if (GameObject.FindGameObjectsWithTag("Ruler") != null) {
GameObject[] Rulers = GameObject.FindGameObjectsWithTag("Ruler");
foreach (GameObject cancelRule in Rulers){
Destroy(cancelRule);
}
}
float
xRight = 0f,
yTop = 0f;
for (int i=0; i < 50; i++){
if (i > 0) {
xRight += 0.2f;
yTop += 0.2f;
}
Vector3 hPosRight = new Vector3 (xRight, -0.2f, 0);
Vector3 vPosTop = new Vector3 (-0.2f, yTop, 0);
//Debug.Log (hPosRight);
// label
if (ruler.GetComponent("GUIText") == null) {
ruler.AddComponent("GUIText");
}
ruler.tag = "Ruler";
ruler.guiText.richText = true;
ruler.guiText.fontSize = 14;
ruler.guiText.lineSpacing = 0;
ruler.guiText.color = Color.black;
ruler.guiText.transform.localScale = Vector3.zero;
//ruler.guiText.transform.position = rulerLabelPos;
ruler.guiText.anchor = TextAnchor.MiddleLeft;
ruler.guiText.alignment = TextAlignment.Left;
ruler.guiText.lineSpacing = 0f;
ruler.guiText.text = i.ToString();
Quaternion rotation = Quaternion.identity;
Vector3 hScale = new Vector3 (0.01f, 0.1f, 0.01f);
ruler.transform.localScale = hScale;
ruler.guiText.name = "ruler_horizontal_"+ i.ToString();
ruler.transform.position = hPosRight;
Vector3 point = new Vector3 (ruler.renderer.bounds.max.x, ruler.renderer.bounds.max.y, 3f);
Vector3 pointOnScreen = Camera.main.WorldToScreenPoint(point);
Vector2 pixelOffset = new Vector2(Mathf.RoundToInt(pointOnScreen.x), Mathf.RoundToInt(pointOnScreen.y)+30);
ruler.guiText.pixelOffset = pixelOffset;
Instantiate(ruler, hPosRight, rotation);
}
}
}
Answer by robertbu · Aug 31, 2013 at 05:02 PM
Why not use 3D Text instead of GUIText? I'm sure sure how you are doing all your numbers. Near as I can see the above code only does a single number. But to answer the specific question, GUIText lives in Viewport space. Set the Pixel Offset of the GUIText to (0,0). Then you position the text using Camera.WorldToViewportPoint(). Assuming 'point' is the position for the number (and you've setup the anchor the way it needs to be):
ruler.guiText.transform.position = Camera.main.WorldToViewportPoint(point);
Did you get the solution bro and if yes can you share the same as i am also trying to make a ruler.
Your answer
Follow this Question
Related Questions
picture in picture 3d. 0 Answers
GameObject GUI Script Drawing to a Specific Camera 1 Answer
Multi Camera HUD buttons not working. 1 Answer