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 slayer29179 · Aug 09, 2012 at 10:03 AM · buttondataitemshandling

Scripted Data Line Reading

Hello all! I am currently trying to make a scripted data line for each ID number.

I am making a GUI Shop which when the user presses a button they buy the item(which works perfectly) however I am wanting to write all the details about the item in a line which can be read, for example;

so; User Presses Button


if Items.itemID equals 0

then read the line from Items.itemID with the ID of 0.

So it would read ID = 0, Name = "Wood", Price = 10, Texture = WoodIcon

however

if Items.itemID equals 5

then read the line from Items.itemID with the ID of 5.

So it would read ID = 5, Name = "Steel", Price = 50, Texture = SteelIcon


So the script for the Items would look something along the lines of;

 ID = 0, Name = "Wood", Price = 10, Texture = WoodIcon;
 ID = 1, Name = "Iron", Price = 15, Texture = IronIcon;
 ID = 2, Name = "Gold", Price = 18, Texture = GoldIcon;
 ID = 3, Name = "Power", Price = 5, Texture = PowerIcon;
 ID = 4, Name = "Vile", Price = 2, Texture = VileIcon;
 ID = 5, Name = "Steel", Price = 50, Texture = SteelIcon;

So this makes the script/File easy to looks down and and add new items. So my question is, what is the best way to approach this? The If statement to determine the ID of the Button I know do with a simple Switch statement. If it means reading from an XML file or Database that can work too. how do I write the lines and allow unity to read them? thank you!

Comment
Add comment · Show 5
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 Mizuho · Aug 09, 2012 at 10:10 AM 0
Share

Think about using a custom editor window for this?

avatar image slayer29179 · Aug 09, 2012 at 10:12 AM 0
Share

sorry I don't know what that is? Do you mean a GUI window?

avatar image Mizuho · Aug 09, 2012 at 10:16 AM 0
Share

Yeah. You can make one to edit this stuff using Unity's GUI x_x.

avatar image Mizuho · Aug 09, 2012 at 10:17 AM 1
Share

http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html

avatar image slayer29179 · Aug 09, 2012 at 10:26 AM 0
Share

Thank you! that does look really like what I wanted; however with a quick scan of it It doesn't say how I can link each line up to the ID, for example if ID = 0 then price is this etc? any help?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hathol · Aug 09, 2012 at 12:10 PM

We are using a simple xml file to store some of our configuration data. Feel free to use the code below. GetItemByID returns an xml node that has an attribute "id" with the respective value. You can then use returnedXmlNode.Attributes["yourattribute"].Value to get price etc. out of it.

 XmlDocument m_xmldata;

 
 

 private void Initialize()
  {
      // load the config file into the XmlDocument
      TextAsset textAsset = (TextAsset) Resources.Load("ItemData");  
      m_xmldata = new XmlDocument ();
      m_xmldata.LoadXml ( textAsset.text );
  }
 
  public XmlNode GetItemByID(string p_id)
  {
      foreach(XmlNode node in m_xmldata.ChildNodes)
      {
          XmlNode currentnode = SearchChildNodesForID(node, p_id);
          if(currentnode != null)
          return currentnode;
      }
      return null;
  }
  
  private XmlNode SearchChildNodesForID(XmlNode p_node, string p_id)
  {
      if(p_node.Attributes != null && p_node.Attributes["id"] != null)
          if(p_node.Attributes["id"].Value == p_id)
              return p_node;
  
      if(p_node.HasChildNodes)
      {
          foreach(XmlNode node in p_node.ChildNodes)
          {
              XmlNode currentnode = SearchChildNodesForID(node, p_id);
                  if(currentnode != null)
                      return currentnode;
          }
      }
  
      return null;
  }

Quick note: There is an XmlDocument.GetElementById(), but that requires you to have a DTS schema defined and unity doesn't play along with that (at least it didn't for me)

Comment
Add comment · Show 10 · 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 hathol · Aug 09, 2012 at 12:51 PM 1
Share

sry, my bad ... forgot the includes :) You need to add "using System.Xml;" on top

avatar image hathol · Aug 09, 2012 at 04:50 PM 1
Share

ItemData is an xml file, so basically a standard xml layout. For you it could look something like

 <Item>
     <Item id="0" Name = "Wood" Price = "10" Texture = "WoodIcon" />
     <Item id="1" Name = "Wood2" Price = "10" Texture = "WoodIcon" />
     <Item id="2" Name = "Wood3" Price = "10" Texture = "WoodIcon" />
     <Item id="3" Name = "Wood4" Price = "10" Texture = "WoodIcon" />
 </Item>

sry, I was too lazy to copy all the data ;)

avatar image hathol · Aug 09, 2012 at 07:22 PM 1
Share

No problem :) Put the xml into your resources folder (simply a folder in your assets calles "Resources"), call Initialize (probably in the start function) and then whenever you need data for an i$$anonymous$$m, call GetItemByID with the respective item id(remember that you will need to include System.Xml to work with the returned xml node)

avatar image hathol · Aug 09, 2012 at 08:50 PM 1
Share

Ah, sorry :) Would be:

 XmlNode dataNode = GetItemByID("0");
 string name = dataNode.Atributes["Name"].Value;
 string price = dataNode.Attributes["Price"].Value;
 string texture = dataNode.Attributes["WoodIcon"].Value;

Here's more info about the XmlNode class: http://msdn.microsoft.com/en-gb/library/system.xml.xmlnode(v=vs.100).aspx ... $$anonymous$$SDN is your best friend when it comes to c# ;)

avatar image hathol · Aug 09, 2012 at 08:51 PM 1
Share

Oh, and in case you need it: System.Convert helps you with converting things from string to int, float etc.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

finish button animation 0 Answers

Best way to handle this in JS (Complex point cloud management) 0 Answers

toggle highlight script ? 0 Answers

How to save and load the game of the corresponding player by Serialization 1 Answer

How to right-click a button on unity 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