Как создать экземпляр ButtonObjectPool после пролистывания указанным объектом в Unity - PullRequest
0 голосов
/ 14 декабря 2018

Я строю продовольственный проект дополненной реальности.Я пытаюсь провести 3D-объект (еду) в список магазинов, чтобы сделать заказ еды.Затем он должен отобразить сборный объект.Проблема в том, что я не могу создать префаб для конкретного.

Swipe.cs

public Transform player; // Drag your player here
public ShopScrollList shoplist;

void Update ()
{
        foreach(Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                fp = touch.position;
                lp = touch.position;
            }
            if (touch.phase == TouchPhase.Moved )
            {
                lp = touch.position;
                swipeDistanceX = Mathf.Abs((lp.x-fp.x));
                swipeDistanceY = Mathf.Abs((lp.y-fp.y));
            }
            if(touch.phase == TouchPhase.Ended)
            {
                angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f;
                if(angle > -30 && angle < 30 && swipeDistanceY > 40)
                {
                    Debug.Log ("swipe");
                    player.position += new Vector3 (0, 0, 200);
                    if (player.name == "cupcake") {
                        shoplist.RefreshDisplay (player.name);
                    }
                    if (player.name == "friedegg") {
                        shoplist.RefreshDisplay (player.name);
                    }
                }

Приведенный выше скрипт вызывает ShopScrollList.cs

ShopScrollList.cs

[System.Serializable]
public class Item {
    public string itemName;
    public Sprite icon;
    public string price = 2 + "$";
}

public class ShopScrollList : MonoBehaviour {

    public List<Item> itemList;
    public Transform contentPanel;
    public Text myGoldDisplay;
    public SimpleObjectPool buttonObjectPool;
    private GameObject newButton;

public void RefreshDisplay(string name)
{
RemoveButton();
AddButton (name);
}

public void AddButton(string name){
if (name == "cupcake") {

                Item item = itemList [0];
                GameObject newButton = buttonObjectPool.GetObject ();  // get object from pool
                newButton.transform.SetParent (contentPanel);
                SampleButton samplebutton = newButton.GetComponent<SampleButton> ();
                samplebutton.Setup (item, this); // this refer to this scrolllist
        }
        if (name == "friedegg") {

                Item item = itemList [1];
                GameObject newButton = buttonObjectPool.GetObject ();  // get object from pool
                newButton.transform.SetParent (contentPanel);
                SampleButton samplebutton = newButton.GetComponent<SampleButton> ();
                    samplebutton.Setup (item, this); // this refer to this scrolllist


        }
}

private void RemoveButton(){
        while (contentPanel.childCount > 0) {

            GameObject toRemove = transform.GetChild (0).gameObject; // as long as there still has child game object there always be child zero, remove
            buttonObjectPool.ReturnObject(toRemove);
            Debug.Log ("What num" + toRemove);
        }
    }

public void TryTransferItemToOtherShop (Item item){
RemoveItem (item, this); // current
        RefreshDisplay();
}
    private void RemoveItem( Item itemToRemove, ShopScrollList shopList){
        Debug.Log ("Shoplist777" + shopList.itemList.Count);
        Debug.Log("itemremove88" + itemToRemove.itemName);
        for (int i = shopList.itemList.Count -1; i >=0; i--) {
            if (shopList.itemList [i] == itemToRemove) {
                shopList.itemList.RemoveAt (i);
                Debug.Log ("remove list" + itemToRemove.itemName);
                Debug.Log ("number" + i);

            }

        }
    }
}

SampleButton.cs (это префаб) (с официального сайта Unity)

public Button button;
    public Text nameLabel;
    public Text priceLabel;
    public Image iconImage;
    // Use this for initialization
    private Item item;
    private ShopScrollList scrollList;

public void Setup(Item currentItem, ShopScrollList currentShopList){

        item = currentItem;
        nameLabel.text = item.itemName;
        priceLabel.text = item.price;
        iconImage.sprite = item.icon;

        scrollList = currentShopList;

}

Я хочу, чтобы 2 префаба появились в списке пользовательского интерфейса магазина, как только я проведу 2 объекта.Как только он будет работать, я добавлю больше объектов, поэтому он должен динамически вызываться в соответствии со списком.

...