private int AddResource(string resource, int count) { Debug.Log("ADD resource: " + count + " " + resource); GameObject gameObject = Resources.Load <GameObject>(resource); return(inventoryStackGroup.AddItemFromPrefab(gameObject.GetComponent <StackItem>(), count)); }
/// <summary> /// Update this instance. /// </summary> void Update() { // On click if (Input.GetMouseButtonDown(0) == true) { PointerEventData pointerEventData = new PointerEventData(EventSystem.current); pointerEventData.position = Input.mousePosition; List <RaycastResult> results = new List <RaycastResult>(); EventSystem.current.RaycastAll(pointerEventData, results); // If clicked not on UI if (results.Count <= 0) { DummyItemPickUp dummyItemPickUp = null; // Raycast to colliders2d RaycastHit2D hit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Camera.main.transform.forward); if (hit2D.collider != null) { dummyItemPickUp = hit2D.collider.gameObject.GetComponent <DummyItemPickUp>(); } if (dummyItemPickUp == null) { // Raycast to colliders3d RaycastHit[] hits3D = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition)); if (hits3D.Length > 0) { foreach (RaycastHit hit in hits3D) { dummyItemPickUp = hit.collider.gameObject.GetComponent <DummyItemPickUp>(); if (dummyItemPickUp != null) { break; } } } } if (dummyItemPickUp != null) { // Hitted on DummyItemPickUp item if (dummyItemPickUp.itemPrefab != null) { // Check allowed space in inventory for this item int pickUpAmount = inventoryStackGroup.GetAllowedSpace(dummyItemPickUp.itemPrefab); pickUpAmount = Mathf.Min(pickUpAmount, dummyItemPickUp.stack); // Try to place item into inventory dummyItemPickUp.stack -= inventoryStackGroup.AddItemFromPrefab(dummyItemPickUp.itemPrefab, pickUpAmount); if (dummyItemPickUp.stack <= 0) { Destroy(dummyItemPickUp.gameObject); } // Set item's price using current sell modifier priceGroup.UpdatePrices(); // Show item price if vendor is active priceGroup.ShowPrices(vendor.activeSelf); } } } } }