コード例 #1
0
        private GameObject CreateCategory(GameObject container, ShopItems.Categories categorie)
        {
            GameObject Panel = new GameObject("Element"); //Create the GameObject#

            Image  img = Panel.AddComponent <Image>();
            Button btn = Panel.AddComponent <Button>();

            img.color = notBuyable;

            GameObject textObj = new GameObject("Text");

            textObj.AddComponent <Text>();
            textObj.GetComponent <RectTransform>().SetParent(btn.GetComponent <RectTransform>());
            textObj.GetComponent <RectTransform>().anchorMin = new Vector2(0, 0f);
            textObj.GetComponent <RectTransform>().anchorMax = new Vector2(1, 1f);
            textObj.GetComponent <RectTransform>().pivot     = new Vector2(0.5f, 0.5f);
            //Left Bottom
            textObj.GetComponent <RectTransform>().offsetMin = new Vector2(0, 0);
            //-Right -Top
            textObj.GetComponent <RectTransform>().offsetMax = new Vector2(0, 0);
            textObj.GetComponent <Text>().font = Resources.GetBuiltinResource <Font>("Arial.ttf");

            btn.GetComponentInChildren <Text>().color     = Color.white;
            btn.GetComponentInChildren <Text>().alignment = TextAnchor.MiddleCenter;
            btn.GetComponentInChildren <Text>().text      = categorie.ToString();

            /*EventTrigger.Entry entry = new EventTrigger.Entry();
             * entry.eventID = EventTriggerType.PointerClick;
             * entry.callback.AddListener( (eventdata) => { Debug.Log("TEST"); } );
             *
             * btn.triggers.Add(entry);*/
            btn.onClick.AddListener(delegate()
            {
                List <GameObject> gameObjects            = allBuyMenuItems.FindAll(x => x.name != categorie.ToString());
                List <GameObject> gameObjectsOfCategorie = allBuyMenuItems.FindAll(x => x.name == categorie.ToString());

                foreach (GameObject item in gameObjects)
                {
                    item.SetActive(false);
                }

                foreach (GameObject item in gameObjectsOfCategorie)
                {
                    item.SetActive(true);
                }

                foreach (GameObject item in allBuyMenuCategories)
                {
                    item.GetComponent <Image>().color = notBuyable;
                }
                img.color = buyable;
            });

            Panel.GetComponent <RectTransform>().SetParent(container.transform);
            Panel.SetActive(true); //Activate the GameObject

            Panel.AddComponent <LayoutElement>();
            Panel.GetComponent <LayoutElement>().preferredWidth = 80;
            Panel.GetComponent <LayoutElement>().flexibleHeight = 1;


            Panel.transform.localScale = Vector3.one;

            return(Panel);
        }
コード例 #2
0
        private GameObject CreateElement(GameObject container, GameObject descriptionOBJ, string name, int price, Func <PlayerCharacterMasterController, int> func, int index, ShopItems.Categories categorie, bool isFoldout, Sprite itemIcon = null, string description = null)
        {
            GameObject Empty = new GameObject("ElementHolder");

            Empty.AddComponent <RectTransform>();
            Empty.name = categorie.ToString();

            Empty.AddComponent <ItemOnHover>();
            Empty.GetComponent <ItemOnHover>().descriptionOBJ = descriptionOBJ;
            Empty.GetComponent <ItemOnHover>().description    = description;

            GameObject Panel = new GameObject("Element"); //Create the GameObject
            Image      img   = Panel.AddComponent <Image>();
            Button     btn   = Panel.AddComponent <Button>();

            Panel.GetComponent <RectTransform>().anchorMin = new Vector2(0, 1f);
            Panel.GetComponent <RectTransform>().anchorMax = new Vector2(0, 1f);
            Panel.GetComponent <RectTransform>().pivot     = new Vector2(0.0f, 1f);

            Panel.GetComponent <RectTransform>().sizeDelta = new Vector2(400, 50);

            img.color = notBuyable;

            GameObject textObj = new GameObject("Text");

            textObj.AddComponent <Text>();
            textObj.GetComponent <RectTransform>().SetParent(btn.GetComponent <RectTransform>());
            textObj.GetComponent <RectTransform>().anchorMin = new Vector2(0, 0f);
            textObj.GetComponent <RectTransform>().anchorMax = new Vector2(1, 1f);
            textObj.GetComponent <RectTransform>().pivot     = new Vector2(0.5f, 0.5f);
            //Left Bottom
            textObj.GetComponent <RectTransform>().offsetMin = new Vector2(0, 0);
            //-Right -Top
            textObj.GetComponent <RectTransform>().offsetMax = new Vector2(0, 0);
            textObj.GetComponent <Text>().font = Resources.GetBuiltinResource <Font>("Arial.ttf");

            btn.GetComponentInChildren <Text>().color     = Color.white;
            btn.GetComponentInChildren <Text>().alignment = TextAnchor.MiddleCenter;

            if (isFoldout)
            {
                btn.GetComponentInChildren <Text>().text = string.Format("{0}", name);

                btn.onClick.AddListener(delegate()
                {
                    List <GameObject> gameObjects = allBuyMenuItems.FindAll(x => x.name == categorie.ToString());

                    foreach (GameObject item in gameObjects)
                    {
                        if (item == Panel)
                        {
                            continue;
                        }

                        item.SetActive(!item.activeSelf);
                    }
                });
            }
            else
            {
                GameObject icon = new GameObject("Icon");
                icon.AddComponent <Image>();
                icon.GetComponent <Image>().sprite = itemIcon;
                icon.GetComponent <RectTransform>().SetParent(btn.GetComponent <RectTransform>());
                icon.GetComponent <RectTransform>().anchorMin = new Vector2(0, 0f);
                icon.GetComponent <RectTransform>().anchorMax = new Vector2(0, 1f);
                icon.GetComponent <RectTransform>().pivot     = new Vector2(0f, 0.5f);
                //Left Bottom
                icon.GetComponent <RectTransform>().offsetMin = new Vector2(25, 0);
                //-Right -Top
                icon.GetComponent <RectTransform>().offsetMax = new Vector2(-50, 0);
                icon.GetComponent <RectTransform>().sizeDelta = new Vector2(50, 0);

                btn.GetComponentInChildren <Text>().text = string.Format("{0} ({1} Souls)", name, price);

                shopItems.GetShopItems()[index].OnBought += delegate()
                {
                    btn.GetComponentInChildren <Text>().text = string.Format("{0} ({1} Souls)", name, shopItems.GetShopItems()[index].Price);
                };

                btn.onClick.AddListener(delegate()
                {
                    if (shopItems.GetShopItems()[index].Price > kills)
                    {
                        return;
                    }

                    //Save the Price
                    int oldPrice = shopItems.GetShopItems()[index].Price;
                    //Call the function (Updates the price)
                    func(LocalUserManager.GetFirstLocalUser().cachedMasterController);
                    //Deduct the souls
                    kills -= oldPrice;
                    //Update the text
                    //btn.GetComponentInChildren<Text>().text = string.Format("{0} ({1} Souls)", name, shopItems.GetShopItems()[index].Price);
                    shopItems.GetShopItems()[index].OnBought();
                });

                //btn.OnPointerEnter

                OnKillsChanged += delegate()
                {
                    if (shopItems.GetShopItems()[index].Price > kills)
                    {
                        img.color = notBuyable;
                    }
                    else
                    {
                        img.color = buyable;
                    }
                };
            }


            Empty.GetComponent <RectTransform>().SetParent(container.transform);
            Panel.GetComponent <RectTransform>().SetParent(Empty.transform);
            Panel.SetActive(true); //Activate the GameObject

            Empty.AddComponent <LayoutElement>();
            Empty.GetComponent <LayoutElement>().preferredHeight = 50;
            Empty.GetComponent <LayoutElement>().flexibleWidth   = 1;

            AddXButtons(Empty, price, func, index);

            Panel.GetComponent <RectTransform>().anchoredPosition = new Vector2(0, 0);

            return(Empty);
        }