I'm creating a multiplayer game, including different heros with different values (eg health, damage). I initially fetch these values from a database and store them into a struct .
-> I have little to no knowledge about structs
-> and got told this is probably a performance loss / bad approach
Storing the values in the struct and creating a dictionary (int, my-struct) to obtain the values worked perfectly fine before.
I've come to the point where I want to store specific sprites for each hero inside this struct . I consider fetching these sprites through Resources.Load (which I guess is the most simple way)
public struct data
{
public string Name;
public int ID, hp, dmg, range, magazin;
public float tbtwb, rltime;
public Sprite heroSprite; //Structs don't seem to like Unity's sprite type
}
public Dictionary<int, data>_HeroDict;
This is how I feed the struct and add an entry into the dictionary.
while (rdr.Read())
{
//Store it into the Dictionary
data itm = new data();
itm.Name = rdr["Name"].ToString();
itm.ID = int.Parse (rdr["ID"].ToString());
itm.hp = int.Parse (rdr["Hp"].ToString());
itm.dmg = int.Parse (rdr["Dmg"].ToString());
itm.magazin = int.Parse (rdr["Magazin"].ToString());
itm.range = int.Parse (rdr["Range"].ToString());
itm.tbtwb = float.Parse(rdr["Tbtwb"].ToString());
itm.rltime = float.Parse(rdr["Rltime"].ToString());
//What I thought of:
itm.heroSprite = Resources.Load ("Sprites/Sprite1", typeof(Sprite)) as Sprite;
_HeroDict.Add(itm.ID, itm);
}
My issue: I can't store sprites inside a struct and don't know about a workaround.
I've been told I could/should use a class instead of a struct , but I couldn't store a Sprite as well, not knowing why.
Can you point me into the right direction and give me an example approach?
Thanks in advance, Csharpest
You can't store a sprite inside a struct or class. A sprite is a class internal to Unity. What you can store is a reference to a sprite, which is exactly what happens when you declare
Sprite sprite = whatever()
It is also important to know just how sprites are stored. A sprite is an asset. Assets are serialized into packages by Unity when your game is built. When you import a sprite, Unity does some magic and later gives it back to you just as it were when you imported it. This is why you can't store a sprite in your code/custom files/whatever. Even if you did find some way to do it, it would be highly inappropriate to do so, since Unity performs many optimizations you don't know of.
With this knowledge, what you can do to retrieve sprites (or any asset) from Unity's asset database is:
In your case, your best bet is to give each hero some sort of ID and use that as a name to load it from the resources.
I should also mention two more things: One, always use classes unless you absolutely know what you're doing. The way you worded your question indicates you don't, so use a class instead. More reading: Design guidelines from MSDN
Two, your code looks like you come from a C++ background and you're trying to somehow use your existing knowledge and old ways of doing things inside Unity. Don't. What you should really do is to create a prefab for each hero and load those from resources.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.