2017-08-07 115 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")); 
} 

如果這沒有解決您的問題,讓我知道,我會看看你的代碼更難。