Advertisement
Guest User

Display Inventory Items

a guest
Feb 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.IO;
  6.  
  7.  
  8. public class DisplayInventoryItems : MonoBehaviour {
  9.     public GameObject InventoryContentParent;
  10.     public GameObject InvObjectItem;
  11.     private string inventoryDataFilePath = "coreInventoryData.json";
  12.     private string CoreInvJsonDatabaseURLPath = "http://localhost/TicTactToe/inventory/";
  13.     private int parseid;
  14.     public bool doneupdating = false;
  15.     public GameObject[] IObjs;
  16.     public int itemnum;
  17.     public string playerID;
  18.  
  19.     string myString = "Inventory Editor";
  20.     bool groupEnabled;
  21.     bool ItemStatsEnabled;
  22.     bool MultipliersEnabled;
  23.     bool UnlockEnabled;
  24.     bool UpgradeEnabled;
  25.     bool IDGroupEnabled;
  26.     bool ExpireEnabled;
  27.     bool DurationEnabled;
  28.     int selected = 0;
  29.     int wasselected = 0;
  30.  
  31.  
  32.     bool myBool = true;
  33.     float myFloat = 1.23f;
  34.  
  35.  
  36.     public string InvName;
  37.     public int InvItemIndex;
  38.     public string InvItemDescr;
  39.     public string InvItemType;
  40.     public int InvID;
  41.     public Sprite InvItemOverlay;
  42.  
  43.     public Sprite InvItemArtWork;
  44.     public GameObject InvItemModel;
  45.     public GameObject InvItemPrefab;
  46.  
  47.     public string claimstatus;
  48.     public int energyCost;
  49.     public int supplyCost;
  50.     public int healthCost;
  51.     public int attackCost;
  52.     public int XPCost;
  53.  
  54.     public int dmgMultiplier;
  55.     public int shieldMultiplier;
  56.     public int bonusMultiplier;
  57.     public int rarityMultiplier;
  58.     public int factionMultiplier;
  59.     public int terrainMultiplier;
  60.  
  61.     public string unlockCost;
  62.     public string upgradeCost;
  63.     public string unlockState;
  64.     public string upgradeLvl;
  65.  
  66.     public int currencyReward;
  67.  
  68.     public float cooldown;
  69.     public bool iStackable = false;
  70.     public int mStack;
  71.     public string digitLength;
  72.     public int newItemID;
  73.  
  74.     public int expireHours;
  75.     public int durationHours;
  76.  
  77.     public string INVDatat;
  78.     public bool isVisible = true;
  79.     [System.Serializable]
  80.     public class INode
  81.     {
  82.         public int InvItemIndex;
  83.         public int InvItemID;
  84.         public string InvItemName;
  85.         public string InvItemDescr;
  86.         public string InvItemType;
  87.  
  88.         public Sprite InvItemOverlay;
  89.  
  90.         public Sprite InvItemArtWork;
  91.         public GameObject InvItemModel;
  92.         public GameObject InvItemPrefab;
  93.  
  94.  
  95.         public string claimstatus;
  96.         public int energyCost;
  97.         public int supplyCost;
  98.         public int healthCost;
  99.         public int attackCost;
  100.         public int XPCost;
  101.  
  102.         public int dmgMultiplier;
  103.         public int shieldMultiplier;
  104.         public int bonusMultiplier;
  105.         public int rarityMultiplier;
  106.         public int factionMultiplier;
  107.         public int terrainMultiplier;
  108.  
  109.         public string unlockCost;
  110.         public string upgradeCost;
  111.         public string unlockState;
  112.         public string upgradeLvl;
  113.  
  114.         public int currencyReward;
  115.  
  116.         public float cooldown;
  117.         public bool iStackable;
  118.         public int mStack;
  119.         public string digitLength;
  120.  
  121.  
  122.         public int expireHours;
  123.         public int durationHours;
  124.  
  125.         public bool isVisible;
  126.     }
  127.  
  128.     [System.Serializable]
  129.     public class InvData
  130.     {
  131.         public List<INode> inventoryItems;
  132.  
  133.     }
  134.     public void RefreshInventory() {
  135.         foreach (Transform child in InventoryContentParent.transform)
  136.         {
  137.             GameObject.Destroy(child.gameObject);
  138.         }
  139.     }
  140.     IEnumerator GetPlayerInventory()
  141.     {
  142.         RefreshInventory();
  143.         playerID = null;
  144.         GameObject HCanvas = GameObject.FindGameObjectWithTag("homeCanvas");
  145.         // Debug.Log(HCanvas.transform.GetChild(1).GetChild(0).GetChild(1).GetChild(0).GetChild(5).GetChild(0).GetChild(1).name);
  146.         playerID = HCanvas.transform.GetChild(1).GetChild(0).GetChild(1).GetChild(0).GetChild(4).GetChild(0).GetChild(1).GetChild(0).GetComponent<GetLoggedUserDetails>().user_userid;
  147.         if (playerID != "")
  148.         {
  149.             if (playerID != null)
  150.             {
  151.                 string url = "http://localhost/tictacttoe/players/" + playerID + "/playerInventoryData.json";
  152.                 WWW www = new WWW(url);
  153.                 yield
  154.                 return www;
  155.                 if (www.error == null)
  156.                 {
  157.                     LoadInventoryData(www.text, "All");
  158.                     //ProcessInventory(www.text);
  159.                 }
  160.                 else
  161.                 {
  162.                     Debug.Log(www.error);
  163.                 }
  164.             }
  165.         }
  166.     }
  167.  
  168.  
  169.     public void LoadInventoryData(string datatoparse, string itype)
  170.     {
  171.         //string filePath = Application.dataPath + inventoryDataProjectFilePath;
  172.         itemnum = 0;
  173.         if (datatoparse != null)
  174.         {
  175.  
  176.             // string dataAsJson = File.ReadAllText(filePath);
  177.  
  178.             InvData idata = JsonUtility.FromJson<InvData>(datatoparse);
  179.  
  180.             if (idata.inventoryItems.Count > 0)
  181.             {
  182.                 int itemnum = 0;
  183.  
  184.                 foreach (INode itemp in idata.inventoryItems)
  185.                 {
  186.                     //Debug.Log(itemp.InvItemID);
  187.                     if ((itype == itemp.InvItemType) || (itype == "All"))
  188.                     {
  189.                         SpawnInventoryNode(itemp.InvItemIndex, itemp.InvItemID, itemp.InvItemName, itemp.InvItemType, itemp.InvItemDescr, itemp.InvItemOverlay, itemp.InvItemArtWork, itemp.InvItemModel, itemp.InvItemPrefab, itemp.claimstatus, itemp.energyCost, itemp.supplyCost, itemp.healthCost, itemp.attackCost, itemp.XPCost, itemp.dmgMultiplier, itemp.shieldMultiplier, itemp.bonusMultiplier, itemp.rarityMultiplier, itemp.factionMultiplier, itemp.terrainMultiplier, itemp.unlockCost, itemp.upgradeCost, itemp.unlockState, itemp.upgradeLvl,itemp.XPCost, itemp.currencyReward, itemp.cooldown, itemp.iStackable, itemp.mStack, itemp.digitLength, itemp.expireHours, itemp.durationHours, itemp.isVisible);
  190.                     }
  191.  
  192.                     itemnum++;
  193.                 }
  194.             }
  195.  
  196.         }
  197.         //else
  198.         //{
  199.         //    InvData idata = new InvData();
  200.  
  201.         //}
  202.     }
  203.     public void SpawnInventoryNode(int itemindex, int itemid, string itemname, string itemtype, string itemdescr, Sprite itemoverlay, Sprite artWork, GameObject iItemModel, GameObject iItemPrefab, string cstatus, int eCost, int sCost, int hCost, int aCost, int xCost, int dMultiplier, int sMultiplier, int bMultiplier, int rMultiplier, int fMultiplier, int tMultiplier, string unCost, string upgCost, string unState, string upgLvl, int currReward, int xpCost, float cdown, bool istackable, int maxstack, string digLength, int expiHours, int durHours, bool isvisible)
  204.     {
  205.         //parent at first history node.
  206.         //Debug.Log(itemname);
  207.         string[] options = new string[]
  208.           {
  209.     "Card", "Consumable", "Booster", "One Time Reward", "Currency Reward", "Weapon", "Weapon Attatchment", "Tool", "Storage", "Crafting Material", "Ordnance","Ammo", "All",
  210.           };
  211.         foreach (string itype in options)
  212.         {
  213.  
  214.  
  215.             if ((itemtype == itype) && (itemtype != "Card"))
  216.             {
  217.                 // GUILayout.BeginArea(new Rect(10, 10, 300, 100));
  218.  
  219.  
  220.  
  221.                 if (itemname != null)
  222.                 {
  223.                     // Debug.Log("viewing this inventory item " + itemid);
  224.  
  225.  
  226.                     if (InventoryContentParent != null)
  227.                     {
  228.                         InvName = itemname;
  229.                         InvItemDescr = itemdescr;
  230.                         newItemID = itemid;
  231.                         InvItemIndex = itemindex;
  232.                         InvItemOverlay = itemoverlay;
  233.                         InvItemType = itemtype;
  234.                         InvItemArtWork = artWork;
  235.                         InvItemModel = iItemModel;
  236.                         InvItemPrefab = iItemPrefab;
  237.                         claimstatus = cstatus;
  238.                         energyCost = eCost;
  239.                         supplyCost = sCost;
  240.                         healthCost = hCost;
  241.                         attackCost = aCost;
  242.                         XPCost = xpCost;
  243.  
  244.                         dmgMultiplier = dMultiplier;
  245.                         shieldMultiplier = sMultiplier;
  246.                         bonusMultiplier = bMultiplier;
  247.                         rarityMultiplier = rMultiplier;
  248.                         factionMultiplier = fMultiplier;
  249.                         terrainMultiplier = tMultiplier;
  250.  
  251.                         unlockCost = unCost;
  252.                         upgradeCost = upgCost;
  253.                         unlockState = unState;
  254.                         upgradeLvl = upgLvl;
  255.  
  256.                         currencyReward = currReward;
  257.  
  258.                         cooldown = cdown;
  259.                         mStack = maxstack;
  260.                         iStackable = istackable;
  261.                         digitLength = digLength;
  262.  
  263.  
  264.                         expireHours = expiHours;
  265.                         durationHours = durHours;
  266.                         isVisible = isvisible;
  267.                         GameObject ii = (GameObject)Instantiate(InvObjectItem);
  268.                         ii.name = InvObjectItem.name + Random.Range(11111, 99999);
  269.                         //GameObject a = (GameObject)HistoryObjectItem;
  270.                         //a.transform.parent = ContentParent.transform.parent;
  271.                         ii.GetComponent<PlayerInventoryItem>().InvItemArtWork = InvItemArtWork;
  272.                         ii.GetComponent<PlayerInventoryItem>().InvItemOverlay = InvItemOverlay;
  273.                         ii.GetComponent<PlayerInventoryItem>().InvItemID = newItemID;
  274.                         ii.GetComponent<PlayerInventoryItem>().InvItemName = InvName;
  275.                         ii.GetComponent<PlayerInventoryItem>().InvItemType = InvItemType;
  276.                         ii.GetComponent<PlayerInventoryItem>().InvItemDescr = InvItemDescr;
  277.                         ii.GetComponent<PlayerInventoryItem>().InvItemModel = InvItemModel;
  278.                         ii.GetComponent<PlayerInventoryItem>().InvItemPrefab = InvItemPrefab;
  279.                        
  280.                         ii.GetComponent<PlayerInventoryItem>().claimstatus = cstatus;
  281.                         ii.GetComponent<PlayerInventoryItem>().energyCost = eCost;
  282.                         ii.GetComponent<PlayerInventoryItem>().supplyCost = sCost;
  283.                         ii.GetComponent<PlayerInventoryItem>().healthCost = hCost;
  284.                         ii.GetComponent<PlayerInventoryItem>().attackCost = aCost;
  285.                         ii.GetComponent<PlayerInventoryItem>().XPCost = xpCost;
  286.  
  287.                         ii.GetComponent<PlayerInventoryItem>().dmgMultiplier = dMultiplier;
  288.                         ii.GetComponent<PlayerInventoryItem>().shieldMultiplier = sMultiplier;
  289.                         ii.GetComponent<PlayerInventoryItem>().bonusMultiplier = bMultiplier;
  290.                         ii.GetComponent<PlayerInventoryItem>().rarityMultiplier = rMultiplier;
  291.                         ii.GetComponent<PlayerInventoryItem>().factionMultiplier = fMultiplier;
  292.                         ii.GetComponent<PlayerInventoryItem>().terrainMultiplier = tMultiplier;
  293.  
  294.                         ii.GetComponent<PlayerInventoryItem>().unlockCost = unCost;
  295.                         ii.GetComponent<PlayerInventoryItem>().upgradeCost = upgCost;
  296.                         ii.GetComponent<PlayerInventoryItem>().unlockState = unState;
  297.                         ii.GetComponent<PlayerInventoryItem>().upgradeLvl = upgLvl;
  298.  
  299.                         ii.GetComponent<PlayerInventoryItem>().currencyReward = currReward;
  300.  
  301.                         ii.GetComponent<PlayerInventoryItem>().cooldown = cdown;
  302.                         ii.GetComponent<PlayerInventoryItem>().mStack = maxstack;
  303.                         ii.GetComponent<PlayerInventoryItem>().iStackable = istackable;
  304.                         ii.GetComponent<PlayerInventoryItem>().digitLength = digLength;
  305.  
  306.  
  307.                         ii.GetComponent<PlayerInventoryItem>().expireHours = expiHours;
  308.                         ii.GetComponent<PlayerInventoryItem>().durationHours = durHours;
  309.                         ii.GetComponent<PlayerInventoryItem>().isVisible = isvisible;
  310.                         ii.transform.SetParent(InventoryContentParent.transform);
  311.                     }
  312.                 }
  313.  
  314.                 //GUILayout.EndArea();
  315.             }
  316.         }
  317.     }
  318.     // Use this for initialization
  319.     void Start() {
  320.         //LoadInvDataFile("All");
  321.        // StartCoroutine(GetPlayerInventory());
  322.  
  323.     }
  324.     public void tryToGetInventory() {
  325.         StartCoroutine(GetPlayerInventory());
  326.     }
  327.     // Update is called once per frame
  328.     void Update () {
  329.        
  330.     }
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement