Unity 5库存系统无法正常工作

时间:2017-08-07 00:56:51

标签: unity3d unity5

Hello Programrsmers遍布全球。我已经为自己的游戏制作了一个库存系统。唯一的问题是,当我点击项目然后将其拖动到空槽并且它没有移动时我有点看不到我所遇到的错误并且我试图调试它但没有成功任何救命?这是代码:

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(),遗憾的是它不是空引用或nullpointer,我只是出于想法已经与它争吵了几天......任何关于如何解决这个问题的建议确实很有帮助,也很受欢迎。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我没有仔细查看您的代码,但我立即看到了这个问题:

else if (to = null) {
    to = clicked.GetComponent<Slot> ();
    Destroy (GameObject.Find ("Hover"));
}

这导致结束位置设置为null。要解决这个问题,请改为double等于:

else if (to == null) {
    to = clicked.GetComponent<Slot> ();
    Destroy (GameObject.Find ("Hover"));
}

如果这不能解决您的问题,请告诉我,我会更加努力地查看您的代码。