예제 #1
0
    // Use this for initialization
    void Start()
    {
        RectTransform rt = BagSlotPrefab.GetComponent <RectTransform>();

        float pixelWidth  = rt.sizeDelta.x;
        float pixelHeight = rt.sizeDelta.y;

        bagSlots = new BagSlot[BagWidth, BagHeight];

        for (int x = 0; x < BagWidth; x++)
        {
            for (int y = 0; y < BagHeight; y++)
            {
                GameObject go = Instantiate(BagSlotPrefab, this.transform);
                BagSlot    bs = go.GetComponent <BagSlot>();
                bagSlots[x, y] = bs;
                bs.PosX        = x;
                bs.PosY        = y;
                bs.Backpack    = this;
                go.name        = bs.BagName();

                rt = go.GetComponent <RectTransform>();
                rt.anchoredPosition = new Vector2(
                    (pixelWidth + Padding) * x + Padding + rt.sizeDelta.x / 2,
                    (pixelHeight + Padding) * -y + Padding - rt.sizeDelta.y / 2);

                Image img = bs.GetComponent <Image>();
                img.sprite = BagSlotImages[Random.Range(0, BagSlotImages.Length)];

                //rt.pivot = new Vector2(.5f, .5f);
                rt.rotation = Quaternion.Euler(0, 0, 90 * Random.Range(0, 4));
                //rt.pivot = new Vector2(0, 0);
            }
        }
    }