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 sarith · Jan 23, 2013 at 05:49 PM · c#gameobjectlistxmldictionary

Runtime instantiation based on XML

Before I start, I'm pretty sure this is not an XML serialization issue. If it is, definitely let me know!

Let's say I have an XML structure similar to the following:

 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
 <animals>
     <section name="Weird Animal Group 1">
         <animal qty="3">Three-Legged Cow</card>
         <animal qty="3">Tim the Horse</card>
         <animal qty="2">Dog</card>
         <animal qty="3">Puppy</card>
         <animal qty="3">Cat</card>
         <animal qty="3">Kitty</card>
         <animal qty="2">Monkey 2.0</card>
         <animal qty="2">Robot Dinosaur</card>
         
         ...
     
         <animal qty="1">Pen_guin</card>
         <animal qty="2">h-y-p-h-e-n-a-t-e-d  r-a-b-b-i-t</card>
     </section>
 </animals>


Let's also say (as I've tried to illustrate with the elipsis) that there's some huge amount of these animal nodes (hundreds).

In my scene, I have an individual prefab for every type of animal that could come in through this XML file. What I need to be able to do is, at runtime, instantiate as many GOs of a particular type as specified by the "qty" attribute in the XML.

I've given every animal in my example strange and varied names using all sorts of weird string structures to illustrate that in my real game there is a comparable amount of unpredictable variety to the way things are named. The only thing I can count on is that they are consistent -- i.e. "Monkey 2.0" will always be called "Monkey 2.0".

Is there some way in C# to have some sort of object pool or database of objects that are mapped to strings like these? I've looked into dictionaries and hashtables -- I think a dictionary might be the way to go -- but I'm worried about efficiency. As I've said, there will be hundreds of these animal types.

I think setting up a structure like this (my full animal database, before reading XML) might be ok:

 Dictionary<string, GameObject> dictionary = new Dictionary<string, GameObject>();
 dictionary.Add("Three-Legged Cow", ThreeLeggedCow);
 dictionary.Add("Tim the Horse", TimTheHorse);
 dictionary.Add("Dog", Dog);
 dictionary.Add("Puppy", Puppy);
 
 ...
 
 dictionary.Add("Pen_guin", Penguin);
 dictionary.Add("h-y-p-h-e-n-a-t-e-d  r-a-b-b-i-t", HyRabbit);


and then, as I loop through and parse each node:

 for (int i=0; i<qty; i++) {
     GameObject currentAnimal = animals[animalNameAsStringFromXml];
     GameObject.Instantiate(currentAnimal);
 }


I'm just worried that using a dictionary in this manner might not be very efficient, especially considering there will be hundreds of these. Any ideas?

Thanks so much in advance for any suggestions. Any help is greatly appreciated :)

Comment
Add comment
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

1 Reply

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

Answer by $$anonymous$$ · Jan 23, 2013 at 06:20 PM

Have you tried this method, is it has any performance issues? Generate a bunch of data and test it out, to make sure. I think this method makes your code bloated, but performance-wise it doesn't look bad.

You could run the parsing/instantiation in a new thread, so you don't block your main thread while parsing in all those stuff, then it should be seamless.

Also take a look at this: Loading Resources. If you can set up a naming convention for the XML and parsing it with a suitable regex pattern, you could get the actual name of your prefabs from those strings in XML. I mean you would remove whitespaces and special characters, and make the words' first characters capitalized. So with using a regex parser, "Three Legged Cow", "Three-Legged Cow", or "threeLegged Cow" would all mean "ThreeLeggedCow", which is the name of your resource. Then you do:

Instantiate(Resources.Load(resourceName));

This would mean much smaller code since you don't have to set up everything by hand, but I expect it to be slower. Whatever method you use, do it on a different thread, because that much instantiation would slow down your main thread in the beginning for sure.

Comment
Add comment · Show 3 · 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 sarith · Jan 23, 2013 at 07:11 PM 0
Share

Thanks so much for the response! Some quick comments regarding your answer:

  1. Can you point me in the right direction for running the parsing/instantiation in a new thread? I'm sure you mean another thread on the CPU, but I'm not very sure how that works in Unity or C#.

  2. I've definitely thought about using a regex function of some sort to standardize the names co$$anonymous$$g in through the X$$anonymous$$L file. Only problem is that there's no standardization that I can see in the names themselves -- Ultimately I think I can force something like in your example, but the regex itself would probably need to be pretty complex. What I'll have to do if I go this route I guess is to run the regex parser ahead of time on all the possible "animals" that I currently have a list of, and then use the output to name my resources (or GOs).

Either way, looks like I'm going to be hand-typing a whole lot! I'll likely do whatever I can in a super-powered text editor like Text$$anonymous$$ate before bringing it all into code in $$anonymous$$onoDevelop.

avatar image $$anonymous$$ · Jan 23, 2013 at 08:10 PM 0
Share
  1. Read this: http://unitygems.com/threads/ It's a pretty good site with multiple advanced, non-trivial tutorials. I haven't tried this out myself yet, but it seems they have a script (Loom) with which you can easily do stuff on a new thread using anonymus functions (which looks very cool).

  2. I'm not a regex guru myself, but I think if you dig into it you should be able to come up with some complex regex for those names. I don't know much about your situation, but if you're able to hand-code a bunch of string-prefab pairings, you can write a regex (or multiple regexes) for them too. But if you can't predict their format... well that's not so good :)

Yeah definitely use a decent text editor.. you can check out Sublime Text 2, it's pretty neat, and you can even use textmate bundles in it. As for me, I use it for the whole process, I touch $$anonymous$$D only if I need the debugger (which is rare, fortunately).

avatar image sarith · Jan 23, 2013 at 10:46 PM 0
Share

Whoa. Great resource.

I would love to use a more compact editor like Text$$anonymous$$ate or Sublime Text for all my coding -- but I'm pretty new to Unity and even newer to C#, so all the helpers and code completion in $$anonymous$$D are super helpful for me right now.

Thanks for everything!

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

A node in a childnode? 1 Answer

Subtracting the position of transform from the position of Game Objects in a list. 1 Answer

How to select an Game Object from an Item List 0 Answers

C# List foreach problem 1 Answer

An element with the same key already exists in the dictionary 0 Answers


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