将 2dtexture 从库存拖到游戏中 [英] Drag 2dtexture from inventory to game

查看:15
本文介绍了将 2dtexture 从库存拖到游戏中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在制作一个移动 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?

提前致谢!

推荐答案

首先,您需要为清单中的项目绘制图像.我以前使用带有图像的 GUI 按钮完成了此操作.这是 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);
}

这样,当玩家在对象上单击并按住鼠标按钮时,位置会更改为鼠标位置.

This way when the player clicks and holds the mousebutton on the object the position is changed to the mouseposition.

这篇关于将 2dtexture 从库存拖到游戏中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆