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
1
Question by The-Oddler · Mar 31, 2011 at 06:09 PM · arraystruct

Smart Tile Info ?

Hi,

I'm making a tile-based game, and I have a lot of different types of tiles. Each tile should have a number, a name and a GUIContent (later maybe even more info). What I need is a fast and easy to use way to get info about tiles: when I have the number, I want to be able to get the name very easely, when I have the name I want to get the number easely, same for GUIContent and perhaps more info when added later. (all these values can't be changes at runtime, so all const/readonly).

I made a struct which contains all the info:

struct TileInfo { public TileInfo(int num, string name, GUIContent guiContent) { Number = num; Name = name; GuiContent = guiContent; }

 readonly int Number;
 readonly string Name;
 readonly GUIContent GuiContent;

}

Now I was thinking of making an array of TileInfo's, and then writing some methods to extract the needed info (eg: int GetNumer(string name); or string GetName(int number);).

Though perhaps there's a better way ? Faster to code ? 'Easier' to use (without Get-methods) ? ...

Thanks ! -The Oddler

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

2 Replies

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

Answer by Statement · Mar 31, 2011 at 08:20 PM

You could make use of extension methods that work on any IEnumerable of TileInfo (Arrays, Lists, Queues...):

using System.Linq; using System.Collections.Generic; using UnityEngine;

public static class TileInfoExt { public static string NameOf(this IEnumerable<TileInfo> tiles, int number) { return tiles.Single(t => t.Number == number).Name; }

 public static string NumberOf(this IEnumerable&lt;TileInfo&gt; tiles, string name)
 {
     return tiles.Single(t =&gt; t.Name == name).Number;
 }

 public static string GuiContentOf(this IEnumerable&lt;TileInfo&gt; tiles, int number)
 {
     return tiles.Single(t =&gt; t.Number == number).GuiContent;
 }

 public static string GuiContentOf(this IEnumerable&lt;TileInfo&gt; tiles, string name)
 {
     return tiles.Single(t =&gt; t.Name == name).GuiContent;
 }

}


using System.Collections.Generic; using UnityEngine;

public class Example : MonoBehavior { TileInfo[] array = / imagine it being set. /; List<TileInfo> list = / imagine it being set. /;

 void Usage()
 {
      string nameOf4 = array.NameOf(4);
      GUIContent guiContentOfBrick = list.GuiContentOf("Brick");
 }

}

Comment
Add comment · Show 2 · 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 Statement · Mar 31, 2011 at 08:25 PM 0
Share

Fastest for lookup would be using a combination of hashmaps and tables. Like a TileInfoCollection that has a HashSet, TileInfo[] and depending if you search by String or Number, access the appropriate container. Insertion and deletion would become slower since you need to maintain two containers but that isn't an issue if you aren't going to add or remove tiles while playing.

avatar image The-Oddler · Mar 31, 2011 at 09:09 PM 0
Share

Hmm, I'm currently indeed maintaining multiple containers, but I hadn't heard of hashmaps before, so I'll look in tot that further :D Thanks in advance, looks promissing :D I'm going to take a look at that hopefully tomorrow :D Thanks !

avatar image
1

Answer by DaveA · Mar 31, 2011 at 08:05 PM

Look into Hashtables. With that you can do things like

hashTab['tile1'] = new TileInfo();
var ti : TileInfo = hashTab[name];

and it's got iterators and so forth

Comment
Add comment · Show 1 · 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 The-Oddler · Mar 31, 2011 at 08:18 PM 0
Share

I'll look in to this, I'll report back as soon as I can ! Thanks in advance :D

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

No one has followed this question yet.

Related Questions

array is pointer and float variable is not - why not? C# 1 Answer

Is it possible to Change mesh also multi materials by a button or a key 0 Answers

Array or Data Structure with Vector3 as keys 2 Answers

Nullreferenceexception, Struct, Array, List 1 Answer

Changing one array element changes the rest 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