Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ash32044 · Dec 21, 2014 at 10:05 AM · c#uilistscriptableobject

Need help using new UI for inventory

Im trying to make an inventory system using scriptable objects and the new UI, I know InventoryDisplay looks horrible, its because ive been trying to get it to work for 5 hours. Im having trouble using the list from from the manager to create buttons in the menu. I was using a prefab of the button. Also the button has two text objects one for the item name the other for the quantity. How do I access each one through code. Thanks if anyone is able and willing to help.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class InventoryManager : MonoBehaviour {
 
     public List<BaseItem> Inventory;
 
     public void AddItem( BaseItem item ) {
 
         if ( item.Quantity < 1 ) {
             Debug.Log("yay");
             Inventory.Add( item );
             item.Quantity = 1;
 
         }
         else if ( item.Quantity < 99 ) {
             Debug.Log("yay");
             item.Quantity += item.itemsToAdd;
 
         }
         else {
 
             Debug.Log("Too Many Items");
 
         }
 
     }
     
 }
 
 
 
 
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class InventoryDisplay : ScriptableObject{
 
     public GameObject itemPrefab;
     public Text itemButtonName;
     public Transform inventoryMenu;
 
     InventoryManager DisplayItemList;
 
     void Start() {
 
     InventoryManager DisplayItemList = new InventoryManager();
 
     }
 
     public void PopulateInventory() {
 
         foreach( BaseItem item in DisplayItemList.Inventory ) {
 
             GameObject itemButton = GameObject.Instantiate( itemPrefab ) as GameObject;
             itemPrefab.transform.SetParent( inventoryMenu, false );
             itemButtonName = itemButton.GetComponent<Text>();
             itemButtonName.text = item.itemName;
             if( item.Quantity < 10 ) {
 
                 itemButton.GetComponent<Text>().text = ":0" + item.Quantity.ToString();
 
             }
             else {
 
                 itemButton.GetComponent<Text>().text = ":" + item.Quantity.ToString();
 
             }
 
         }
 
     }
 
 }

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Dec 21, 2014 at 10:08 AM 0
Share

Just doing exactly the same thing myself.

Do you want me to post an upload of what I'm working on at the moment?

avatar image Kiwasi · Dec 21, 2014 at 08:06 PM 1
Share

You may benefit from these tutorials. Particularly the one on drag and drop and the one on creating a dynamic menu.

avatar image Mmmpies · Dec 21, 2014 at 08:13 PM 0
Share

Yes you will benefit from pretty much anything @Bored$$anonymous$$ormon adds. It's what the Scroll Panel part of my scripts are based on. Although I ran into issues with panels not scaling when running. Looks like adding these 2 lines eventually fixed that for more complex panels

 newItem.transform.SetParent(ParentPanel, false);
 newItem.transform.localScale = new Vector3(1,1,1);

Well probably some other issue actually as I could manually drag the prefab onto the panel and it worked every time, but do that whilst running and without those 2 lines the scale was way off.

Plus thanks again @Bored$$anonymous$$ormon blew my head a bit when I first watched the resolution menu but I think I've got it now.

avatar image ash32044 · Dec 21, 2014 at 08:26 PM 0
Share

Thanks Bored$$anonymous$$ormon, ill be sure take a look at them.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Mmmpies · Dec 21, 2014 at 10:21 AM

Tell you what I'll do I'll post the scripts for the Menu of scriptable objects:

 using UnityEngine;
 using UnityEditor;
 using System.Collections;
 using System.IO;
 
 /*  This adds the MenuItems/Setup dropdown and when clicked creates the Resources/MenuItems path
  *   and the MenuItemManager Empty GameObject for us to hold the references in
  * 
  *  Essentially it just makes it easier to keep everything neat  when dealing with numerous scenes
  */
 
 public class SetupMenu : Editor {
         
     [MenuItem("MenuItems/Setup")]
         
     static void Init()
     {
         if(!Directory.Exists(Application.dataPath + "/Resources/MenuItems"))
             Directory.CreateDirectory(Application.dataPath + "/Resources/MenuItems");
         GameObject alchemyManager = new GameObject ("MenuItemManager");
         alchemyManager.AddComponent ("MenuItemManager");
     }
 }

That creates the gameObject that stores the list and creates the resources folder to store the scriptable objects in.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MI: ScriptableObject {
 
     public enum MIType
     {
         Contact,
         Ranged,
         Self
     }
 
     public string MIName;
     public int MIManaCost;
     public int MIDamage;
     public string MIDescription;
     public Sprite MIIcon;
     public MIType MI_type;
 
 }

That's the object feilds

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class MenuItemManager : MonoBehaviour {
 
     public List<MI> menuItemList = new List<MI>();
 }

The MenuItemManager script that's put on the empty gameObject and stores the list.

Finally the script that does the bulk of the work:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class CreateMenuItem : EditorWindow {
 
     [MenuItem("MenuItems/Create Menu Item")]
     static void Init()
     {
         CreateMenuItem menuItemWindow = (CreateMenuItem)EditorWindow.CreateInstance(typeof(CreateMenuItem));
         menuItemWindow.Show();
     }
 
     MI tempMenuItem = null;
     MenuItemManager menuItemManager = null;
 
     void OnGUI()
     {
         if(menuItemManager == null){
             //Instantiating our spell manager for adding our spells to the spell list.    
             menuItemManager = GameObject.Find("MenuItemManager").GetComponent<MenuItemManager>();
         }
 
     
         if(tempMenuItem)
         {
             
             tempMenuItem.MIName = EditorGUILayout.TextField("Name",tempMenuItem.MIName);
             tempMenuItem.MI_type = (MI.MIType)EditorGUILayout.EnumPopup("Type", tempMenuItem.MI_type);
             tempMenuItem.MIDescription = EditorGUILayout.TextField("Description",tempMenuItem.MIDescription);
             tempMenuItem.MIIcon = (Sprite)EditorGUILayout.ObjectField("Icon",tempMenuItem.MIIcon,typeof(Sprite),false);
             tempMenuItem.MIManaCost = EditorGUILayout.IntField("Mana Cost",tempMenuItem.MIManaCost);
             tempMenuItem.MIDamage = EditorGUILayout.IntField ("Damage", tempMenuItem.MIDamage);
 
             //tempAlchemyItem.attributeNeoAffected = (AttributeName)EditorGUILayout.EnumPopup("Neophyte Attribute Affected", tempAlchemyItem.attributeNeoAffected);
         }
         
         
         if(tempMenuItem == null){
             
             if(GUILayout.Button("Create MenuItem"))
             {
                 //This line of code instantiates of our temporary spell.
                 tempMenuItem = (MI)ScriptableObject.CreateInstance<MI>();
             }
             
         }
         else
         {
             
             if(GUILayout.Button("Create Scriptable Object"))
             {
                 //Creates a scriptable object that contains all of our spell properties.
                 AssetDatabase.CreateAsset(tempMenuItem,
                                           "Assets/Resources/MenuItems/" + tempMenuItem.MIName + ".asset");
                 AssetDatabase.SaveAssets();
                 //Adding our spell to spellDatabase and we can reach all of spells in game.
                 menuItemManager.menuItemList.Add(tempMenuItem);
                 Selection.activeObject = tempMenuItem;
                 
                 tempMenuItem = null;
                 
             }
             
             
             if(GUILayout.Button("Reset"))
             {
                 //Reseting spell properties.
                 Reset();
                 
             }
             
         }
         
     }
     
     void Reset()
     {
         if(tempMenuItem)
         {
             tempMenuItem.MIName = "";
             tempMenuItem.MIDescription = "";
             tempMenuItem.MIIcon = null;
             tempMenuItem.MIDamage = 0;
             tempMenuItem.MIManaCost = 0;
 
         }
     }
 
 }

You should be able to pick it apart to get what you want. But just put those scripts into an empty project to test how it works.

EDIT:

Just realized that's still not showing you how to reference the items. I have a panel with various Text/Image/Toggle objects on it. That's saved as a prefab and when the prefab is instantiated I call it newItem.

Before you loop through the items in the list you'll need to reference the list ...

 myMenuItemManager = GameObject.Find ("MenuItemManager").GetComponent<MenuItemManager>();

Then after instantiating the item you can reference the child object of the panel (or button in your case) a couple of ways. I'll not post all of it just a sample.

 // test what the children of this panel are, change the number in the GetChild and name each item in the
                 // inspector so we can see what the child is.
 
                 string testThis = newItem.gameObject.transform.GetChild(0).name.ToString();
                 Debug.Log (newItem.name + " child 0 " + testThis);
 
                 // Or you can reference the child object by its name with newItem.gameObject.transform.Find("Name").name.ToString();
 
                 // The Icon is stored in child 0 #### May be different on your setup so test with the lines above ###
                 newItem.gameObject.transform.GetChild(0).GetComponent<Image>().sprite = myMenuItemManager.menuItemList[myCount - 1].MIIcon;
                 newItem.gameObject.transform.GetChild(1).GetComponent<Text>().text = myMenuItemManager.menuItemList[myCount - 1].MIName;
                 newItem.gameObject.transform.GetChild(2).GetComponent<Text>().text = myMenuItemManager.menuItemList[myCount - 1].MIDescription;

Hope that helps, if you want the package I'm working on that's fine, it's not finished as I'm really doing a test of tabstrip and scrollable menu to show what it can do. Basically if you want it I'll post it on dropbox but ignore the buttons at the top as they do nothing at the moment.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mmmpies · Dec 21, 2014 at 06:58 PM 0
Share

Did this help or do you need more info? If it helped then tick it as answered.

I'm not really bothered about the points, as far as I know I can't cash them in anywhere, but people will search for similar questions to their own and marking it correct means they're more likely to find an answer.

Of course there's the possibility I've sent you a load rubbish, if so feel free not to tick answered :¬)

avatar image ash32044 · Dec 21, 2014 at 07:31 PM 0
Share

Sorry, I went to bed, just got up and checked let me look it over and see if it helps. Anyhow thanks a bunch.

avatar image ash32044 · Dec 21, 2014 at 07:47 PM 0
Share

Im not sure if this will accomplish exactly what I want. I need to play around will it and see if I can get to work. I have trouble understanding the relationship between classes and the need to instantiate objects. In my $$anonymous$$d I want a list and methods that is on my Inventory $$anonymous$$anager that I can access without instantiating new objects, so that the information is stored on the Inventory$$anonymous$$anager script. Now, i'm not even sure if thats possible lol. I think im going to read more on singletons and the static modifier. Anything I find out though i will update this post. Also i'm not sure if you know but with the new UI you can create prefabs of the Button or Text or what ever UI element with it setup how you would want it displayed, then call it and populate it with the information. I just seen that you were using OnGui. Then again im still completely new at program$$anonymous$$g and probably dont understand your script well enough. You probably knew that and still wanted to use ONGui lol.

avatar image Mmmpies · Dec 21, 2014 at 07:59 PM 0
Share

Yep I use OnGUI for the menu that appears in Unity when the game isn't running. I added the $$anonymous$$enuItems add in to unity $$anonymous$$yUnity$$anonymous$$enu

But I use the 4.6 GUI to populate an example menu with a scrollRect that build from prefabs.

4.6GUI$$anonymous$$enu

Hey it's a bit basic but I can add spells easily and then in game the menu build dynamically. All returns all spells in the list and then ranged just ranged, contact just contact.

Or it will when I finish the demo.

Should say again, create an empty project and copy the FULL scripts in and you'll get an additional menu in Unity called $$anonymous$$enuItems.

Click it and click Setup and it'll create a gameobject with the menu manager on it.

Then click $$anonymous$$enuItems and click Create$$anonymous$$enuItem and add in whatever fake spell information you want as a test.

You can use elements of the last partial script to access the menu manager and all the sriptable objects in there.

If you want I can post a link to my current setup so you can see how I have it setup.

Oh but if you do want an export of my setup don't laugh too hard at the icons, at my folks for Christmas so don't have my graphics tablet!

4.6guimenu.png (97.7 kB)
createmenuitem.png (21.0 kB)
avatar image ash32044 · Dec 21, 2014 at 08:23 PM 0
Share

Haha thats neat man. You've been really helpful. I'm going to keep working on it though because I have alot to learn.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

List appears null? C# 2 Answers

How to compare a string in a list with a gameobject.name from another list of gameobjects 1 Answer

Filling array with Scribtable Objects automaticly? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges