Unity2D:库存系统 - 出现在原始对象

时间:2017-07-09 23:15:17

标签: c# unity3d

我创建了一个可行的库存系统,但是我遇到了实例化预制件的问题。基本上,用户在开始时被给予两个项目(item1& item2);如果他们想要他们可以购买额外的物品(让日间项目2)放下,但是物品可以放下的区域有一个限制(4)。如果区域内的项目超过4,则除了实例化的预制件之外,所有项目都将被发送回其插槽,它们将被发送到空的gameObject。

我的问题:

假设我放置了第2项(原始和预制实例化)中的两项,然后放下项目1中的三项,所有项目将被送回,然后将项目2拖入该区域后,当我继续尝试将项目1(原始obj)拖动到它似乎拖动实例化预制件而不是原始obj的区域,使原始obj更改大小并更改图层中的顺序。

我的剧本:

private Vector3 screenPoint;
private Vector3 offset;
public Transform item1, item2;
public GameObject otherItems,otherItems2

void Awake() {
    item1 = GameObject.FindWithTag ("item1").transform;
    item2 = GameObject.FindWithTag ("item2").transform;
}

void OnMouseDrag()
{
    //if (canDrag == true) {
        Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        screenPoint.z = 10;
        Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint); // + offset

        transform.position = curPosition;
    //}
}
void Update()
{

    //1
    if (Input.GetMouseButtonUp (0)) {
        if (!GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
            item1.localPosition = Vector3.zero;
            rend1.sortingOrder = 1;
            //otherItems.SetActive (false);
        } else if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
            screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
            rend1.sortingOrder = -1;
            otherItems.SetActive (true);
            //canDrag = false;
        }
    }

    //2
    if (Input.GetMouseButtonUp (0)) {
        if (!GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
            item2.localPosition = Vector3.zero;
            rend2.sortingOrder = 1;
            //otherItems2.SetActive (false);
        } else if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
            screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
            rend2.sortingOrder = -1;
            otherItems2.SetActive (true);
            //slotHolder.SetActive (true);
            //canDrag = false;
        }
    }
    if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
        if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
            slotHolder.SetActive (true);
        }
    if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item2Placed) {
        if (GameObject.Find ("Aera Floor").GetComponent<SlotController> ().item1Placed) {
            slotHolder.SetActive (true);
        } 
    }

用于实例化预制件的脚本:

public void InstaniateItem() {
    GameObject item = (GameObject)Instantiate (ItemPrefab);
    item.transform.position = transform.position;
    item.transform.parent = transform;
    item.SetActive (true);
    if (slot.childCount < 0) {
        slotHolder.SetActive (false);
    }
}

额外信息:

  • 所有预制件都与原始产品具有相同的标签。

谢谢:)

1 个答案:

答案 0 :(得分:1)

通常,尝试缓存GameObject.Find()的结果,因为它会影响性能,在您的情况下,它会使代码真的变得混乱。有关详细信息,请参阅the documentation

您的问题可能来自双if (Input.GetMouseButtonUp(0))子句。每次发布单击时都会调用此方法,因此每次单击都会对两个项目进行检查。如果您已经遇到过两件事的问题,那么当您有三件或四件时会发生什么?也许你应该重新考虑你的方法。

以下是相当于您的代码,希望它可以帮助您清楚了解正在进行的操作。

void Awake() {
    item1 = GameObject.FindWithTag ("item1").transform;
    item2 = GameObject.FindWithTag ("item2").transform;
    SlotController slotCtrl = GameObject.Find ("Aera Floor").GetComponent<SlotController> ();
}

void Update()
{

if (Input.GetMouseButtonUp (0)) {
    if (!slotCtrl.item1Placed) {
        item1.localPosition = Vector3.zero;
        rend1.sortingOrder = 1;
        //otherItems.SetActive (false);
    } else  {
        screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
        rend1.sortingOrder = -1;
        otherItems.SetActive (true);
        //canDrag = false;
    }

    if (slotCtrl.item2Placed) {
        item2.localPosition = Vector3.zero;
        rend2.sortingOrder = 1;
        //otherItems2.SetActive (false);
    } else {
        screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
        rend2.sortingOrder = -1;
        otherItems2.SetActive (true);
        //slotHolder.SetActive (true);
        //canDrag = false;
    }
}

if (slotCtrl.item1Placed && slotCtrl.item2Placed) {
        slotHolder.SetActive (true);
    }
}