- Home /
How to create a lightweight Item/Mineral/etc System
Hi people
I want to create a space game. There are asteroids and they contain minerals which I want to mine and sell... For now each asteroid is 'pure' meaning that it only contains one mineral type. I created several classes:
[Serializable]
public class Mineral {
public string name;
public int value;
public Mineral() {
name = "New Mineral";
value = 0;
}
public override string ToString() {
return "v:" + value;
}
public void OnGUI() {
GUILayout.Label(name, EditorStyles.boldLabel);
value = EditorGUILayout.IntField ("Value", value);
}
}
public class Asteroid : MonoBehaviour {
public Mineral mineral = null;
}
public class Pool {
public static Dictionary<string, Mineral> minerals = new Dictionary<string, Mineral> ();
public static void AddMineral(Mineral m) {
minerals.Add (m.name, m);
}
}
Because I have no custom property drawers or something in the inspector for the asteroid there are fields for the name and value fields. But I don't want to create new mineral objects for each asteroid, I want to be able to select mineral objects from the Pool class. How do I do that? I assume I have to go with serialization and creating custom editors, because the mineral objects and the references to them in the asteroid objects have to exist in edit mode AND play mode. Any idea?
This is a more abstract and general questions, I don't need concrete code, except it helps you explaining :)
Is it that you want, say, Asteroids A, B, and C all to have references to the same instance of $$anonymous$$ineral, while Asteroids D and E have references to a different instance, Asteroids F, G, and H yet another, etc?
You could use an enumeration to do this, and set up your instances in a static property on the $$anonymous$$ineral class.
(Warning: haven't tried the following code... Call it conceptual pseudo-code.)
public class $$anonymous$$ineral {
public readonly string name ;
public readonly uint value ;
public $$anonymous$$ineral( string name, uint value ) {
this.name = name ;
this.value = value ;
}
// $$anonymous$$ineral.$$anonymous$$ind will be our comprehensive list of $$anonymous$$eral types
public enum $$anonymous$$ind { Iron, Quartzite, Iridium, Dilithium }
// registry will hold our "Platonic ideal" $$anonymous$$erals
protected static $$anonymous$$ineral[] registry ;
// initialize registry when loading class
static $$anonymous$$ineral() {
registry = new $$anonymous$$ineral[4] ;
registry[ $$anonymous$$ind.Iron ] = new $$anonymous$$ineral( name:"iron", value:"10" ) ;
registry[ $$anonymous$$ind.Quartzite ] = new $$anonymous$$ineral( name:"quartzite", value:"3" ) ;
registry[ $$anonymous$$ind.Iridium ] = new $$anonymous$$ineral( name:"iridium", value:"36" ) ;
registry[ $$anonymous$$ind.Dilithium ] = new $$anonymous$$ineral( name:"dilithium", value:"6000" ) ;
}
// get a $$anonymous$$eral in the registry
static $$anonymous$$ineral Of$$anonymous$$ind( $$anonymous$$ind kind ) {
return registry[ kind ] ;
}
}
// Asteroid will have a public property referencing the type of $$anonymous$$eral
public class Asteroid : $$anonymous$$onoBehaviour {
public $$anonymous$$ineral.$$anonymous$$ind $$anonymous$$eral ;
}
I like this idea! But is there a way to create the enums from unity itself? I think of a window that has a list of enums for example. Like a $$anonymous$$eral browser, kind of like the project tab.
I want to avoid having to hardcode them into the scripts, yes it works and isn't that hard, but I don't like it very much.
There's no way to create an enum from the Unity editor, no.
It sounds like you're looking to create a custom Editor. That's a lot of work, just to define a list...
Answer by Glurth · Jan 09, 2015 at 12:51 AM
To make a list of options appear in the editor, I use an ENUM.
Once you have your list of enumerated values, create a public member of that type, and you will be presented with a drop down list in the editor. Each item on an Enumeration list is really just a "named number", and should be used like an integer in your code (which makes them great for working with arrays).
public enum MineralType {NONE=0,GOLD=1,IRON=2,COPPER=3,KRYPTON=4};
public Asteroid:MonoBehaviour
{
public MineralType composition; // this looks like a drop down list in the editor,
// showing all the values in the enum MineralType
}