- Home /
Swipe to place order in ShopScrollList (prefabs, object pooling, instantiating)
I am building an Augmented Reality Food Project. I am trying to swipe the 3D object(food) to the Shop list for make the order of the food. Then, it should display the prefab for the object. The problem is I can't create new prefab at the second swipe of other 3D object. I mean that once I swipe one 3D Object, it should create one prefab in list and swipe other 3D Object, it also should create one more prefab on the same list. But how?
I follow the sample tutorials HERE
This swipe.cs script is attached to each 3D objects
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);
}
}
This ShopScrollList.cs is attached to the content panel
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);
}
}
}
}
This SampleButton.cs is the prefab that is called from shopScrollList.cs
SampleButton.cs
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;
}
I want to have more than one prefabs appear on the same Shop UI list once I swipe continuously with 3D objects.