首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一5库存系统不起作用

统一5库存系统不起作用
EN

Stack Overflow用户
提问于 2017-08-07 00:56:50
回答 1查看 104关注 0票数 0

世界各地的程序员你好。我已经为我的游戏建立了一个库存系统。唯一的问题是,当我点击项目,然后拖到它和空槽,它不会移动,我没有看到我有错误,我已经尝试调试,但没有成功,任何帮助?以下是代码:

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Inventory : MonoBehaviour {

private RectTransform inventoryRect;

private float inventoryWidth;
private float inventoryHeight;

public int slots;
public int rows;

public float slotPaddingLeft;
public float slotPaddingTop;

public float slotSize;

public GameObject slotPrefab;

private static Slot from;
private static Slot to;

private List<GameObject> allslots;

public GameObject iconPrefab;

private static GameObject hoverObject;

private static int emptySlots;

public Canvas canvas;

private float hoverYOffset;

private bool isPressed;

public EventSystem eventSystem;

public static int EmptySlots{
    get{ return emptySlots;}
    set{ emptySlots = value;}
}

// Use this for initialization
void Start () {
    CreateLayout ();
    canvas.enabled = false;
    isPressed = false;
}

// Update is called once per frame
void Update () {
    if (Input.GetKeyDown (KeyCode.I)) {
        if (Input.GetKeyDown (KeyCode.I)) {
            canvas.enabled = false;
        }
        canvas.enabled = true;
    }

    if (Input.GetMouseButtonUp (0)) {
        if (!eventSystem.IsPointerOverGameObject (-1) && from != null) {
            from.GetComponent<Image> ().color = Color.white;
            from.ClearSlot ();
            Destroy (GameObject.Find ("Hover"));
            to = null;
            from = null;
            hoverObject = null;
        }
    }

    if (hoverObject != null) {
        Vector2 position;
        RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas.transform as RectTransform, Input.mousePosition, canvas.worldCamera, out position);
        position.Set (position.x, position.y - hoverYOffset);
        hoverObject.transform.position = canvas.transform.TransformPoint (position);
    }
}

private void CreateLayout(){
    allslots = new List<GameObject> ();

    hoverYOffset = slotSize * 0.01f;

    emptySlots = slots;

    inventoryWidth = (slots / rows) * (slotSize + slotPaddingLeft) + slotPaddingLeft;
    inventoryHeight = rows * (slotSize + slotPaddingTop) + slotPaddingTop;

    inventoryRect = GetComponent<RectTransform> ();

    inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, inventoryWidth);
    inventoryRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, inventoryHeight);

    int colums = slots / rows;

    for (int y = 0; y < rows; y++) {
        for (int x = 0; x < colums; x++) {
            GameObject newSlot = (GameObject)Instantiate (slotPrefab);

            RectTransform slotRect = newSlot.GetComponent<RectTransform> ();

            newSlot.name = "Slot";
            newSlot.transform.SetParent (this.transform.parent);

            slotRect.localPosition = inventoryRect.localPosition + new Vector3 (slotPaddingLeft * (x + 1) + (slotSize * x), -slotPaddingTop * (y + 1) - (slotSize * y));
            slotRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, slotSize);
            slotRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, slotSize);

            allslots.Add (newSlot);
        }
    }
}

public bool AddItem(Item item){
    if (item.maxSize == 1) {
        PlaceEmpty (item);
        return true;
    }
    else {
        foreach (GameObject slot in allslots) {
            Slot temporary = slot.GetComponent<Slot> ();
            if (!temporary.IsEmpty) {
                if (temporary.CurrentItem.type == item.type && temporary.IsAvailable) {
                    temporary.AddItem (item);
                    return true;
                }
            }
        }
        if (emptySlots > 0) {
            PlaceEmpty (item);
        }
    }
    return false;
}

private bool PlaceEmpty(Item item){
    if (emptySlots > 0) {
        foreach (GameObject slot in allslots) {
            Slot temporary = slot.GetComponent<Slot> ();
            if (temporary.IsEmpty) {
                temporary.AddItem (item);
                emptySlots--;
                return true;
            }
        }
    }
    return false;
}

public void MoveItem(GameObject clicked){

    if (from == null) {
        if (!clicked.GetComponent<Slot> ().IsEmpty) {
            from = clicked.GetComponent<Slot> ();
            from.GetComponent<Image> ().color = Color.gray;

            hoverObject = (GameObject)Instantiate (iconPrefab);
            hoverObject.GetComponent<Image> ().sprite = clicked.GetComponent<Image> ().sprite;
            hoverObject.name = "Hover";

            RectTransform hoverTransform = hoverObject.GetComponent<RectTransform> ();
            RectTransform clickedTransform = clicked.GetComponent<RectTransform> ();

            hoverTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, clickedTransform.sizeDelta.x);
            hoverTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, clickedTransform.sizeDelta.y);

            hoverObject.transform.SetParent (GameObject.Find ("Canvas").transform, true);
            hoverObject.transform.localScale = from.gameObject.transform.localScale;
        }
    } 
    else if (to = null) {
        to = clicked.GetComponent<Slot> ();
        Destroy (GameObject.Find ("Hover"));
    }
    if (to != null && from != null) {
        Stack<Item> tmpTo = new Stack<Item> (to.Items);
        to.AddItems (from.Items);
        if (tmpTo.Count == 0) {
            from.ClearSlot ();
        } 
        else {
            from.AddItems (tmpTo);
        }

        from.GetComponent<Image> ().color = Color.white;
        to = null;
        from = null;
        hoverObject = null;
    }
}

}

造成这个问题的方法是MoveItem(),遗憾的是,它不是空引用或空指针,我对它的猜测已经持续了几天。任何关于如何解决这一问题的建议都将是有益的,而且非常受欢迎。提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-08-07 23:57:34

我还没有仔细研究过您的代码,但我马上就看到了这个问题:

代码语言:javascript
复制
else if (to = null) {
    to = clicked.GetComponent<Slot> ();
    Destroy (GameObject.Find ("Hover"));
}

这将导致将结束位置设置为null。若要解决此问题,请更改为双等值,如下所示:

代码语言:javascript
复制
else if (to == null) {
    to = clicked.GetComponent<Slot> ();
    Destroy (GameObject.Find ("Hover"));
}

如果这不能解决您的问题,请告诉我,我将更仔细地查看您的代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45538067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档