以下是我的角色移动且2个或更多对象进入光线投射视野的代码,GUI元素与其名称重叠。我已经尝试了很多GUI选项但没有成功,请告诉我什么是正确的方法。
using UnityEngine;
using System.Collections;
public class rayss : MonoBehaviour
{
FOV2DEyes eyes;
FOV2DVisionCone visionCone;
float speed = -5;
RaycastHit hit;
RaycastHit[] objects ;
void Start()
{
eyes = GetComponentInChildren<FOV2DEyes>();
visionCone = GetComponentInChildren<FOV2DVisionCone>();
}
void FixedUpdate()
{
if (transform.position.x < -10 || transform.position.x > 10)
{
speed *= -1;
}
transform.position = new Vector3(transform.position.x + speed * Time.fixedDeltaTime, transform.position.y, transform.position.z);
}
void Update()
{
objects = new RaycastHit[eyes.hits.Count];
int i = 0;
foreach (RaycastHit hit in eyes.hits)
{
if (hit.transform)
{
objects[i] =hit ;
i++;
}
}
}
void OnGUI()
{
foreach (RaycastHit hit in objects)
{
GUI.Label( new Rect(40, 25, 100, 40), hit.collider.gameObject.name);
}
}
}
答案 0 :(得分:0)
简单的OnGUI,您可以将代码更改为:
void OnGUI()
{
float yHeight = 25;
foreach (RaycastHit hit in objects)
{
GUI.Label( new Rect(40, yHeight, 100, 40), hit.collider.gameObject.name);
yHeight += 40 + 5// Label Height + some padding
}
}
如果对象列表过多,则需要添加滚动视图,如this。