我正在研究的背景知识:基本上,我有一个立方体模型,该模型将用于生成某些地形结构。所有这些游戏对象都是通过以下方式在运行时创建的:
float yPos = 0f;
float scale = 0.5f;
var robot = GameObject.Instantiate(grassTile) as GameObject;
robot.transform.position = new Vector3(0, yPos, 0);
robot.transform.localScale = new Vector3(scale, scale, scale);
robot.transform.rotation = Quaternion.Euler(-90, 0, 0);
width = robot.GetComponent<Renderer>().bounds.size.z;
for(int i=0;i<5;i++){
yPos += width;
robot = GameObject.Instantiate(grassTile) as GameObject;
robot.transform.position = new Vector3(0, yPos, 0);
robot.transform.localScale = new Vector3(scale, scale, scale);
robot.transform.rotation = Quaternion.Euler(-90, 0, 0);
//attach the script
robot.AddComponent<CubeBehavior>();
}
CubeBehavior.cs里面是什么:
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0) {
Debug.Log(transform.position);
}
}
发生了什么事,无论我触摸哪个多维数据集,它都将始终返回最后一个多维数据集位置。请给我有关这个问题的启示。预先感谢
答案 0 :(得分:2)
您没有代码可以检测到哪个立方体被触摸。
Input.touches
仅告诉您当前在屏幕上检测到多少触摸。
要检测触摸,您需要做更多的工作。一种可能的解决方案是实现触摸事件处理程序。受the doc启发的示例:
public class CubeBehaviour : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
//Detect current clicks on the GameObject (the one with the script attached)
public void OnPointerDown(PointerEventData pointerEventData)
{
//Output the name of the GameObject that is being clicked
Debug.Log(name + " click down at position " + transform.position);
}
//Detect if clicks are no longer registering
public void OnPointerUp(PointerEventData pointerEventData)
{
Debug.Log(name + "No longer being clicked");
}
}
这将需要在场景中的某个地方放置EventSystem
。潜在地,您还需要多维数据集上的触发对撞机,不确定