public void CreateInventory() { if (bg.activeSelf) { Debug.Log("true"); } else { Debug.Log("false"); } bg.GetComponent <RectTransform>().sizeDelta = new Vector2(sizeW * cellW, sizeH * cellH); bg.GetComponent <RectTransform>().sizeDelta += new Vector2(cellW, cellH * (1.5f)); panel.GetComponent <RectTransform>().localPosition = Vector3.zero; panel.GetComponent <RectTransform>().localPosition += new Vector3(cellW / 2, -cellH, 0); panel.GetComponent <RectTransform>().sizeDelta = new Vector2(sizeW * cellW, sizeH * cellH); //panel.GetComponent<RectTransform>().localPosition += new Vector3(sizeW,cellH,0); DestroyInventory(); for (int y = 0; y < sizeH; y++) { for (int x = 0; x < sizeW; x++) { GameObject slot = new GameObject(); if (slot.GetComponent <Image>() == null) { slot.AddComponent <Image>(); } if (slot.GetComponent <CanvasRenderer>() == null) { slot.AddComponent <CanvasRenderer>(); } if (slot.GetComponent <RectTransform>() == null) { slot.AddComponent <RectTransform>(); } slot.transform.SetParent(panel.transform); slot.GetComponent <RectTransform>().anchorMin = new Vector2(0.0f, 1.0f); slot.GetComponent <RectTransform>().anchorMax = new Vector2(0.0f, 1.0f); slot.GetComponent <RectTransform>().pivot = new Vector2(0.0f, 1.0f); slot.GetComponent <RectTransform>().localScale = new Vector3(1f, 1f, 1f); slot.GetComponent <RectTransform>().sizeDelta = new Vector2(cellW, cellH); float h = panel.GetComponent <RectTransform>().rect.height; slot.GetComponent <Image>().sprite = Slot; slot.GetComponent <Image>().color = new Color(1, 1, 1, .5f); slot.transform.name = x.ToString() + ":" + y.ToString(); slot.GetComponent <RectTransform>().localPosition = new Vector3(x * cellW, 0 - (y * cellH), 0); slot.transform.tag = "Inventory"; } } foreach (var i in inventory) { GameObject item = new GameObject(); item.AddComponent <RectTransform>(); item.AddComponent <CanvasRenderer>(); item.AddComponent <Image>(); //set image settings ItemsTexture it = FindObjectOfType <ItemsTexture>(); item.GetComponent <Image>().sprite = it.GetItemSprite(i.item.id); item.GetComponent <Image>().color = new Color(1, 1, 1, 1f); //set transform settings item.transform.SetParent(panel.transform); item.transform.SetParent(panel.transform); item.GetComponent <RectTransform>().anchorMin = new Vector2(0.0f, 1.0f); item.GetComponent <RectTransform>().anchorMax = new Vector2(0.0f, 1.0f); item.GetComponent <RectTransform>().pivot = new Vector2(0.0f, 1.0f); item.GetComponent <RectTransform>().localScale = new Vector3(1f, 1f, 1f); item.GetComponent <RectTransform>().sizeDelta = new Vector2(cellW * i.item.width, cellH * i.item.height); item.transform.name = i.item.id.ToString(); item.GetComponent <RectTransform>().localPosition = new Vector3(i.x * cellW, -(i.y * cellH), 0); item.transform.tag = "Inventory"; } bg.SetActive(true); }
public void CreateInventory() { Destroy(); parentBG.GetComponent <RectTransform>().sizeDelta = new Vector2(cols * cellSize, rows * cellSize); parentBG.GetComponent <RectTransform>().sizeDelta += new Vector2(cellSize, cellSize * (1.5f)); parentPanel.GetComponent <RectTransform>().localPosition = Vector3.zero; parentPanel.GetComponent <RectTransform>().localPosition += new Vector3(cellSize / 2, -cellSize, 0); parentPanel.GetComponent <RectTransform>().sizeDelta = new Vector2(cols * cellSize, rows * cellSize); //creating slots for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { GameObject slot = new GameObject(); if (slot.GetComponent <Image>() == null) { slot.AddComponent <Image>(); } if (slot.GetComponent <CanvasRenderer>() == null) { slot.AddComponent <CanvasRenderer>(); } if (slot.GetComponent <RectTransform>() == null) { slot.AddComponent <RectTransform>(); } slot.transform.SetParent(parentPanel.transform); slot.GetComponent <RectTransform>().anchorMin = new Vector2(0.0f, 1.0f); slot.GetComponent <RectTransform>().anchorMax = new Vector2(0.0f, 1.0f); slot.GetComponent <RectTransform>().pivot = new Vector2(0.0f, 1.0f); slot.GetComponent <RectTransform>().localScale = new Vector3(1f, 1f, 1f); slot.GetComponent <RectTransform>().sizeDelta = new Vector2(cellSize, cellSize); float h = parentPanel.GetComponent <RectTransform>().rect.height; slot.GetComponent <Image>().sprite = Slot; slot.GetComponent <Image>().color = new Color(1, 1, 1, .5f); slot.transform.name = x.ToString() + ":" + y.ToString(); slot.GetComponent <RectTransform>().localPosition = new Vector3(x * cellSize, 0 - (y * cellSize), 0); slot.transform.tag = tag; } } //creating items on slots foreach (var i in inventory) { GameObject item = new GameObject(); item.AddComponent <RectTransform>(); item.AddComponent <CanvasRenderer>(); item.AddComponent <Image>(); item.AddComponent <MoveItem>(); //set image settings ItemsTexture it = FindObjectOfType <ItemsTexture>(); item.GetComponent <Image>().sprite = it.GetItemSprite(i.item.id); item.GetComponent <Image>().color = new Color(1, 1, 1, 1f); item.GetComponent <Image>().preserveAspect = true; //set transform settings item.transform.SetParent(parentPanel.transform); item.transform.SetParent(parentPanel.transform); item.GetComponent <RectTransform>().anchorMin = new Vector2(0.0f, 1.0f); item.GetComponent <RectTransform>().anchorMax = new Vector2(0.0f, 1.0f); item.GetComponent <RectTransform>().pivot = new Vector2(0.5f, 0.5f); item.GetComponent <RectTransform>().localScale = new Vector3(1f, 1f, 1f); item.GetComponent <RectTransform>().sizeDelta = new Vector2(cellSize * i.item.width, cellSize * i.item.height); item.transform.name = i.item.id.ToString(); item.GetComponent <RectTransform>().localPosition = new Vector3 ( item.GetComponent <RectTransform>().sizeDelta.x / 2 + i.x * cellSize, -(i.y * cellSize) - item.GetComponent <RectTransform>().sizeDelta.y / 2, 0 ); item.transform.tag = tag; } //show result on canvas isVisible = true; parentBG.SetActive(true); }