2017-08-07 54 views
0

你好程序员在世界各地。我为自己的比赛制定了自己的盘点制度。唯一的问题是,当我点击物品,然后将其拖动到并且空插槽它不移动,我有点看不到我所遇到的错误,我试图调试它,但没有成功的任何帮助?下面是代码:统一5库存系统不工作

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; 
    } 
} 

}

这是造成该问题的方法是移动选项()可悲的是它不是一个nullreference或空指针,我只是我的想法与它被strugling几天...有关如何解决这个问题的任何建议将是有益的,并且确实很受欢迎。提前致谢!

回答

0

我还没有采取长时间看你的代码,但马上我看到了这个问题:

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

这导致最终位置被设置为null。为了解决这个问题,更改为双等于像这样:

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

如果这没有解决您的问题,让我知道,我会看看你的代码更难。