public static void Init(Products.Item targetBase = null, Action <Products.Item> OnComplete = null)
        {
            CompletedAction = OnComplete;

            if (targetBase != null)
            {
                CurrentBase = (Products.Item)targetBase.Clone();
            }
            else
            {
                CurrentBase = new Products.Item();
            }

            appSettings = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings");

            selection = appSettings.GameItemList.ToList().FindIndex(x => x.Equals(CurrentBase.Id));

            // Create window.
            var window = (ItemEditor)GetWindow(typeof(ItemEditor));

            window.minSize = new Vector2(320, 235);
            window.maxSize = new Vector2(320, 235);
            window.Show();
        }
    private void TemplateLoaded(PaymentTemplate template, PaymentsAppSettings appSettings)
    {
        Debug.Log("[SHOP] Template loaded.");

        currentTemplate = template;

        // Initialize UI.
        foreach (var product in template.Products)
        {
            AddProduct(product);
        }

        foreach (var bundle in template.Bundles)
        {
            AddBundle(bundle);
        }

        // Load filters by items.
        Transform filterHolder = filterSelection.transform;
        var       filterPrefab = filterHolder.GetChild(0);

        foreach (var item in template.ItemDefinitions)
        {
            var filter = Instantiate(filterPrefab, filterHolder);
            filter.gameObject.SetActive(true);

            filter.GetComponentInChildren <Text>().text = item.Name;
            filter.gameObject.name = item.Id;
        }

        filterSelection.Initialize(); // first child is the template. use the first generated one.
        // filter event
        filterSelection.OnValueChanged.AddListener((List <int> values) => {
            int count        = values.Count;
            string[] itemIds = new string[count];
            for (int i = 0; i < count; i++)
            {
                // get item id.
                itemIds[i] = filterHolder.GetChild(values[i]).name;
            }

            FilterShopByItemId(itemIds);
        });

        filterSelection.SelectAll();

        #region Create sorting orders.
        ///
        var options = new List <Dropdown.OptionData>();

        //  DEFAULT SORTING ORDERS.
        string[] defaultSortings = new string[] {
            "by name (ascending)",
            "by name (descending)",
            "by price (high to low)",
            "by price (low to high)"
        };

        foreach (var sorting in defaultSortings)
        {
            options.Add(new Dropdown.OptionData()
            {
                text = sorting
            });
        }

        /// Item sortings from the app settings.
        int sortingOrdersCount = appSettings.SortingOrders.Length;
        for (int i = 0; i < sortingOrdersCount; i++)
        {
            options.Add(new Dropdown.OptionData()
            {
                text = appSettings.SortingOrders[i].Name
            });
        }

        sortingDropdown.AddOptions(options);

        sortingDropdown.onValueChanged.AddListener((index) => {
            if (index >= defaultSortings.Length)
            {
                string[] order = appSettings.SortingOrders[index - defaultSortings.Length].SortingOrderItemIds;
                SortByItem(order);
            }
            else
            {
                switch (index)
                {
                case 0:
                case 1:
                    SortByName(index == 0);
                    break;

                case 2:
                case 3:
                    SortByPrice(Payments.Products.Price.Currency.USDollars, index == 2);
                    break;
                }
            }
        });
        #endregion

        // Update sorting.
        sortingDropdown.onValueChanged?.Invoke(0);

        onShopLoaded?.Invoke();

        purchaseButton.onClick.AddListener(() => {
            OnPurchaseItem?.Invoke(activeItem);
        });
    }
Пример #3
0
        static void Init()
        {
            lastMessage      = "";
            templatesEnabled = false;
            appIdDoesntExist = false;
            dataPulled       = false;

            // payments app settings
            var paymentsAppSettings = Resources.Load <PaymentsAppSettings>("PaymentsAppSettings");

            if (paymentsAppSettings == null)
            {
                // Create app settings.
                PaymentsAppSettings asset = CreateInstance <PaymentsAppSettings>();

                AssetDatabase.CreateAsset(asset, "Assets/Resources/PaymentsAppSettings.asset");
                AssetDatabase.SaveAssets();

                EditorUtility.FocusProjectWindow();

                Selection.activeObject = asset;
            }
            //

            // resources folder.
            string targetFolder = Application.dataPath + "/Resources";

            ConfigPath = targetFolder + "/" + "PaymentsConfig.json";
            if (!!!File.Exists(ConfigPath))
            {   // File doesnt Exist. Create.
                if (!!!Directory.Exists(targetFolder))
                {
                    // Directory is also Null??
                    Directory.CreateDirectory(targetFolder);
                }

                string NewConfig = "{ \"_AppId\": \"unassignedId\" }";
                File.WriteAllText(ConfigPath, NewConfig);

                // Load asset database again.
                AssetDatabase.Refresh();
            }

            // Read config.
            string readed = File.ReadAllText(ConfigPath);

            if (string.IsNullOrEmpty(readed))  // READ DATA IS NULL!!!
            {
                Debug.LogError("[Payments] 'Error code 001': Not sure what happened. But config data is not found & not created for some reason.");
                return;
            }

            CurrentConfig = JsonUtility.FromJson <PaymentsConfig>(readed);
            if (CurrentConfig == null)
            {
                Debug.LogError("[Payments] 'Error code 002': Broken config.");
                return;
            }

            // Get existing open window
            //or if none, make a new one:
            PaymentsManager window = (PaymentsManager)GetWindow(typeof(PaymentsManager));

            window.Show();
        }