将2dtexture从库存拖到游戏中

好吧,所以我正在制作移动2D游戏。 您可以在游戏中从广告资源中拖出矩形或圆圈。

我希望如何设置:当我将库存中的一个项目拖入游戏时,在顶部或右侧是具有对象/纹理的库存。 然后它从库存中删除该项目。 之后,商品库存将全部向上移动1个。 这样第一个插槽就被占用了。

我怎么做这个工作? 请有人帮我思考或给我一些例子吗?

提前致谢!

首先,您需要为清单中的项目绘制图像。 之前我已经用GUI按钮附加了一个图像。 这是C#中的一个例子:

void OnGUI(){ if (GUI.Button(new Rect(0, 0 , 10, 10), itemTexture,)) { //The if here is to get when the image get pushed. //Push the item to the world } } 

当玩家点击该项目时,您必须将其推向世界。 您可以使用以下代码执行此操作:

 private Vector3 mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition); private Object block = Instantiate(objectYouClicked, new Vector3(mousePositionInWorld.x, mousePositionInWorld.y, 0), Quaternion.identity); 

该对象必须绑定到播放器鼠标/触摸位置。 您可以使用以下代码将对象绑定到播放器:

 private Vector3 screenPoint; void OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); } void OnMouseDrag() { Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); Vector3 currentPos = Camera.main.ScreenToWorldPoint(currentScreenPoint); rigidbody.MovePosition(currentPos); } 

例如,您有一个清单对象。 像这样:

 void OnGUI(){ if (GUI.Button(new Rect(0, 0 , 10, 10), itemTexture,)) { private Vector3 mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition); private Object block = Instantiate(objectYouClicked, new Vector3(mousePositionInWorld.x, mousePositionInWorld.y, 0), Quaternion.identity); } } 

然后你有一个附加脚本的预制件/对象,其中包含以下代码:

 private Vector3 screenPoint; void OnMouseDown() { screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position); } void OnMouseDrag() { Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); Vector3 currentPos = Camera.main.ScreenToWorldPoint(currentScreenPoint); rigidbody.MovePosition(currentPos); } 

这样,当玩家点击并按住对象上的鼠标按钮时,位置就会变为鼠标位置。