试图让#34; smart"统一的十字准线(使用javascript)

时间:2015-03-24 18:55:19

标签: user-interface unity3d unityscript raycasting

这是我的剧本:

var ctexture : Texture2D;
function Update() {
var ray = Camera.main.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
    var pointVec=Camera.main.WorldToScreenPoint(hit.point);
    GUI.Label(Rect(pointVec.x,pointVec.y,145,93),ctexture);
    Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
Debug.Log(hit.point);}}

我可以看到Debug.DrawRay光线,它就是我想要它的方式。问题是我无法看到GUI.Label的结果。分配了ctexture,Debug.Log打印出正确的信息。

提前致谢。

1 个答案:

答案 0 :(得分:0)

将代码放在OnGUI()函数中:

var ctexture : Texture2D;

function OnGUI() {
    var ray = Camera.main.ScreenPointToRay (Vector3(Screen.width/2,Screen.height/2,0));
    var hit : RaycastHit;
    if (Physics.Raycast (ray, hit, 100)) {
        var pointVec=Camera.main.WorldToScreenPoint(hit.point);
        GUI.Label(Rect(pointVec.x,pointVec.y,145,93),ctexture);
        Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
        Debug.Log(hit.point);
    }
}