unity 背包选中_unity3d中用GUI展示物品背包,并在背包中显示一个3d物体 !!!并可以在背包中随意移动物品...

如题,求思路,求一个简单的demo ,也可以看看下面的代码为什么不能实现物品移动。求指导。在此万分感谢

private int btnclickCount=0; //打开背包按钮的次数

private GUIContent[] Contents; //背包的格子数

private int currentIndex=0; //当前选中

private int goodsIndex=2; //物品所在背包的索引

public Texture2D goodsimage; //当做物体了

private bool isgoodsSelect=false; //是否选中了物品

void OnGUI()

{

if(GUI.Button(new Rect(0,0,100,50),"打开背包"))

{

btnclickCount++;

}

if(btnclickCount%2==1)//按钮的次数是奇数 就打开背包

{

if(isgoodsSelect)

{

goodsIndex=currentIndex;

isgoodsSelect=false;

}

Contents=new GUIContent[9];

for(int i=0;i{

if(i==goodsIndex)

{

Contents[i]=new GUIContent(goodsimage);

}

else</

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现: 1. 给背包物品 A 添加一个 Button 组件,并在点击事件添加销毁 A 的代码。 2. 在背包添加物品时,为每个物品添加一个唯一的 ID。 3. 在 UI 上的物品 B 上添加一个 RectTransform 组件,记录它的初始位置。 4. 在点击背包物品 A 的事件,获取 A 的 ID,并根据 ID 找到对应的 UI 物品 B。 5. 使用 Tween 或其他方式将物品 B 移动到指定位置,完成位移效果。 下面是示例代码(仅供参考): ```csharp // 背包物品类 public class Item { public int id; public string name; // 其他属性 } // 背包管理类 public class BagManager : MonoBehaviour { public GameObject itemPrefab; // 物品预制体 public RectTransform bagPanel; // 背包面板 public RectTransform itemB; // UI 物品 B private List<Item> items = new List<Item>(); // 背包物品列表 // 添加物品背包 public void AddItem(Item item) { // 在背包生成一个新的物品 A GameObject go = Instantiate(itemPrefab, bagPanel); Button btn = go.GetComponent<Button>(); btn.onClick.AddListener(() => { // 点击事件:销毁物品 A,并移动物品 B Destroy(go); MoveItemB(item.id); }); // 设置物品 A 的属性 // ... items.Add(item); } // 移动物品 B 到指定位置 private void MoveItemB(int itemId) { Item item = items.Find(i => i.id == itemId); if (item == null) { return; } Vector3 targetPos = // 计算目标位置 // 使用 Tween 或其他方式移动物品 B // ... } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值