コード例 #1
0
ファイル: WeaponPopulator.cs プロジェクト: Honeybunch/Space
    void CreateWeaponListButtons()
    {
        RectTransform panelRectTrans = this.GetComponent <RectTransform>();
        float         Top            = panelRectTrans.rect.height / 2 - 130;

        //For every weapon, add a button to this game object
        for (int i = 0; i < WeaponList.Count; i++)
        {
            GameObject weapon  = WeaponList[i];
            string     wepName = weapon.name;

            //Get the texture of the weapon off of its material
            SpriteRenderer wepSpriteRenderer = weapon.GetComponent <SpriteRenderer>();
            Sprite         wepSprite         = wepSpriteRenderer.sprite;

            //Create button
            GameObject button = new GameObject();
            button.AddComponent <CanvasRenderer>();

            Image background = button.AddComponent <Image>();
            background.sprite = ButtonBackground;
            background.type   = Image.Type.Sliced;

            Toggle toggle = button.AddComponent <Toggle>();
            toggle.group = WeaponToggleGroup;

            button.name = wepName + "Button";

            //Set Button position and size
            RectTransform buttonRectTrans = button.GetComponent <RectTransform>();

            Vector3 pos = new Vector3();
            pos[0] = 0;
            pos[1] = Top - (i * ButtonHeight * 3);
            pos[2] = panelRectTrans.position.z + 1; // To make sure the buttons are above the background

            buttonRectTrans.sizeDelta = new Vector2(ButtonWidth, ButtonHeight);

            //Each button needs to store which attachment its using
            AttachmentToggle attachment = button.AddComponent <AttachmentToggle>();
            attachment.Attachment = weapon;

            //Create weapon image
            GameObject wepImage     = new GameObject();
            Image      wepImageComp = wepImage.AddComponent <Image>();
            wepImageComp.sprite = wepSprite;
            wepImage.name       = wepName + "ButtonImage";

            RectTransform weaponImageRectTrans = wepImage.GetComponent <RectTransform>();
            weaponImageRectTrans.sizeDelta = new Vector2(ButtonWidth - 25, ButtonHeight - 25);

            buttonRectTrans.SetParent(panelRectTrans);
            buttonRectTrans.position = pos;
            weaponImageRectTrans.SetParent(buttonRectTrans);

            //Have to set position after setting parents
            weaponImageRectTrans.anchoredPosition3D = Vector3.zero;
            buttonRectTrans.anchoredPosition3D      = pos;
        }
    }
コード例 #2
0
    void OnMouseOver()
    {
        if (WeaponToggles.AnyTogglesOn())
        {
            spriteRenderer.color = selectedColor;
            Vector2 pointPos = transform.position;

            // If it's left mouse button, attach
            if (Input.GetMouseButtonDown(0) && !attaching)
            {
                Toggle           selectedToggle   = WeaponToggles.ActiveToggles().FirstOrDefault();
                AttachmentToggle attachmentToggle = selectedToggle.GetComponent <AttachmentToggle>();

                GameObject attachment = attachmentToggle.Attachment;

                //If the ship has an attachment here we should remove it
                if (ship.Attachments[Index] != null)
                {
                    WeaponScript currentAttachment = ship.Attachments[Index];
                    Destroy(currentAttachment.gameObject);
                }

                //Now add the selected attachment to the ships' attached Weapons dictionary and instantiate it
                GameObject attachmentClone     = GameObject.Instantiate(attachment);
                Transform  attachmentTransform = attachmentClone.transform;

                attachmentTransform.position = new Vector3(pointPos.x, pointPos.y, ship.transform.position.z - .1f);
                attachmentTransform.SetParent(ship.transform);
                ship.Attachments[Index] = attachmentClone.GetComponent <WeaponScript>();

                //Don't let this happen again until the mouse is lifted
                attaching = true;

                Destroy(selectedToggle.gameObject);
            }

            //If it's right mouse button, clear
            else if (Input.GetMouseButtonDown(1) && !attaching)
            {
                WeaponScript currentAttachment = ship.Attachments[Index];
                if (currentAttachment != null)
                {
                    Destroy(currentAttachment);
                }
            }
            else if (Input.GetMouseButtonUp(0) && Input.GetMouseButtonUp(1))
            {
                attaching = false;
            }
        }
    }
コード例 #3
0
    void DisplayInventory()
    {
        AttachmentToggle[] buttons = scrollContent.GetComponentsInChildren <AttachmentToggle> ();
        foreach (AttachmentToggle _button in buttons)
        {
            Destroy(_button.gameObject);
        }

        for (int i = 0; i < GameMaster.playerData.playerInventory.Weapons.Count; i++)
        {
            int        _i     = i;
            GameObject button = Instantiate(buttonPrefab) as GameObject;
            button.name = i.ToString();
            button.transform.SetParent(scrollContent.transform, false);
            button.GetComponentInChildren <Text>().text = GameMaster.playerData.playerInventory.Weapons[i].Name;
            Toggle toggle = button.GetComponent <Toggle>();
            toggle.group = WeaponToggleGroup;
            AttachmentToggle attachment = button.GetComponent <AttachmentToggle>();
            attachment.Attachment = GameMaster.playerData.playerInventory.Weapons[i].WeaponPrefab;
        }
    }