从库存拖到2dtexture到游戏 [英] Drag 2dtexture from inventory to game
问题描述
还好吧,所以我想提出一个移动2D建设游戏。 。在那里你可以在游戏中你的库存拖动矩形或圆形
Allright, so I am making a mobile 2d building game. where you can drag a rectangle or circle from your inventory in the game.
我怎么想它被设置:
顶部或右侧与对象/纹理库存,当我拖动一个项目从我的库存进入游戏。它然后删除清单中的项目。这些物品清点后都将移动1插槽了。从而使第一插槽占用百达
how I would like it to be set up: on the top or right side is an inventory with objects/textures, when I drag one of the items from my inventory into the game. it then removes the item from the inventory. after that the items inventory will all move 1 slot up. so that the first slot is allways occupied.
我将如何使这项工作?有人可以请帮我想想还是给我一些例子吗?
How would I make this work? can someone please help me think or give me some examples?
在此先感谢!
推荐答案
首先,你需要画一个图像清单中的项目。
我已经与连接到它的图像的图形用户界面按钮这样做过。
这是在C#中的例子:
First you need to draw an image for the item in the inventory. I have done this before with an GUI Button with an image attached to it. Here is an example in 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
}
}
当玩家点击该项目必须将它推向世界。
你可以做到这一点与这个代码:
When the player clicks on the item you must push it to the world. You can do that with this code:
private Vector3 mousePositionInWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
private Object block = Instantiate(objectYouClicked, new Vector3(mousePositionInWorld.x, mousePositionInWorld.y, 0), Quaternion.identity);
的对象必须被绑定到播放机鼠标/触摸位置。
你可以绑定到该代码玩家的对象:
The object must be bound to the player mouse/touch position. You can make the object bound to the player with this code:
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);
}
因此,例如你有一个清单对象。像这样的:
So for example you have an inventory object. Like this:
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);
}
}
然后你有一个脚本的预制/对象连接包含此代码:
Then you have an prefab/object with an script attached that contains this code:
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);
}
当玩家点击并保持该物体的位置在mousebutton这样改变到mouseposition
This way when the player clicks and holds the mousebutton on the object the position is changed to the mouseposition.
这篇关于从库存拖到2dtexture到游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!