Consider the following example of planetary enumeration :
public enum Planet {
MERCURY,
VENUS,
EARTH,
MARS,
JUPITER,
SATURN,
URANUS,
NEPTUNE,
PLUTO // Pluto is a planet!!!
}
at first , That's a good definition , Until you need to produce the mass of a planet . So we do things like this :
// Returns the mass of the planet in 10^24 kg
public float GetMass(Planet planet) {
switch(planet) {
case Planet.MERCURY:
return 0.330;
case Planet.VENUS:
return 4.87f;
case Planet.EARTH:
return 5.97f;
...
case Planet.PLUTO:
return 0.0146f;
}
}
What about the diameter of the planet ? the other one switch sentence ? What about the density ? gravity ? Escape speed ? Just think about what you're going to maintain switch Number of statements .
You can argue , You can use one Dictionary, But it's still bulky . Of each data Dictionary All have to be mapped ? no way .
There is a better way , I'll tell you how . This may have been wrong
Unity Common sense of programmers , But I want to bring up this redundant topic again in my blog , For those who may not know that , Especially for beginners . I also want to keep it simple .
Basically , You can use classes as enumerations . Why use classes ? It did perform better You can store any amount of any data . You can even store routines or functions . You can do a lot of things .
The only requirement is that it is immutable , This means that the state of an instance of a class cannot change throughout the program . Here are Planet Enumerates a version represented by a class :
public class Planet {
// The different values
public static readonly Planet MERCURY = new Planet(0, 0.330f, 4879,
5427, 3.7f);
public static readonly Planet VENUS = new Planet(1, 4.87f, 12104, 5243,
8.9f);
public static readonly Planet EARTH = new Planet(2, 5.97f, 12756, 5514,
9.8f);
public static readonly Planet MARS = new Planet(3, 0.642f, 6792, 3933,
3.7f);
public static readonly Planet JUPITER = new Planet(4, 1898.0f, 142984,
1326, 23.1f);
public static readonly Planet SATURN = new Planet(5, 568.0f, 120536,
687, 9.0f);
public static readonly Planet URANUS = new Planet(6, 86.8f, 51118,
1271, 8.7f);
public static readonly Planet NEPTUNE = new Planet(7, 102.0f, 49528,
1638, 11.0f);
public static readonly Planet PLUTO = new Planet(8, 0.0146f, 2370,
2095, 0.7f);
// Use readonly to maintain immutability
private readonly int id;
private readonly float mass; // in 10^24 kg
private readonly int diameter; // in km
private readonly int density; // in kg/m^3
private readonly float gravity; // in m/s^2
// We use a private constructor because this should not be instantiated
// anywhere else.
private Planet(int id, float mass, int diameter, int density, float
gravity) {
this.id = id;
this.mass = mass;
this.diameter = diameter;
this.density = density;
this.gravity = gravity;
}
public int Id {
get {
return id;
}
}
public float Mass {
get {
return mass;
}
}
public int Diameter {
get {
return diameter;
}
}
public int Density {
get {
return density;
}
}
public float Gravity {
get {
return gravity;
}
}
}
In order to keep invariance , All member variables should be read-only . Once they are assigned , They will not be able to be changed . It's important , Because as an enumeration , Its internal value should not change .
Each enumeration value is then implemented as a static read-only instance of the class .
How is this used ? It's the same as a normal enumeration , Use the following :
// Use it like an enum
ship.TargetPlanet = Planet.NEPTUNE;
// Want to know the target planet's mass?
float mass = ship.TargetPlanet.Mass;
// Density?
int density = ship.TargetPlanet.Density;
We have eliminated the need to switch statements or dictionaries to maintain information about different planets . Want a new planetary state ? Just add a new member variable and specify them on the instantiation .
How to convert from other data types ? Like to say from int id Convert to Planet example ? It's easy Usually I add a public and static method to these transformations . for example :
public class Planet {
// The different values
public static readonly Planet MERCURY = new Planet(0, 0.330f, 4879, 5427,
3.7f);
public static readonly Planet VENUS = new Planet(1, 4.87f, 12104, 5243,
8.9f);
public static readonly Planet EARTH = new Planet(2, 5.97f, 12756, 5514,
9.8f);
public static readonly Planet MARS = new Planet(3, 0.642f, 6792, 3933,
3.7f);
public static readonly Planet JUPITER = new Planet(4, 1898.0f, 142984,
1326, 23.1f);
public static readonly Planet SATURN = new Planet(5, 568.0f, 120536, 687,
9.0f);
public static readonly Planet URANUS = new Planet(6, 86.8f, 51118, 1271,
8.7f);
public static readonly Planet NEPTUNE = new Planet(7, 102.0f, 49528, 1638,
11.0f);
public static readonly Planet PLUTO = new Planet(8, 0.0146f, 2370, 2095,
0.7f);
// This can be used to loop through all planets
public static Planet[] ALL = new Planet[] {
MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE, PLUTO
};
// Converts the specified id to a Planet instance
public static Planet Convert(int id) {
for(int i = 0; i < ALL.Length; ++i) {
if(ALL.Id == id) {
return ALL;
}
}
// return ALL[id] could also work here but what if a non sequential id
is used?
throw new Exception("Cannot convert {0} to a Planet.".FormatWith(id));
}
...
}
// Usage
Planet planet = Planet.Convert(someIntPlanet);
From the string ID transformation ? Add a string member variable that will hold the value . Instead of using ALL [] Array of , You can use the Dictionary:
private static Dictionary<string, planet="" style="color: rgb(34, 34, 34);
font-family: Microsoft YaHei ; font-size: 15px; font-style: normal; font-variant-ligatures:
normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal;
orphans: 2; text-align: start; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width:
0px; background-color: rgb(255, 255, 255); text-decoration-style: initial;
text-decoration-color: initial;"> ALL = new Dictionary<string, planet="">() {
{ MERCURY.TextId, MERCURY },
{ VENUS.TextId, VENUS },
{ EARTH.TextId, EARTH },
...
{ PLUTO.TextId, PLUTO },
};
// Converts the specified string to a Planet instance
public static Planet Convert(string id) {
return ALL[id];
}
You can support any type of conversion you like .
There's more you can do . You can now add features . You can do this :
Planet currentPlanet = Planet.VENUS;
currentPlanet.ApplyGravity(ship);
The coolest thing for me is you can specify different actions or behavior to
the enum values. Something like this (It’s very contrived but you get the
idea.):
public static readonly Planet EARTH = new Planet(2, 5.97f, 12756, 5514, 9.8f,
delegate(Ship ship) {
// Actions on land of ship
ship.AddFood(1000);
ship.RetireCrew();
ship.RecruitNewCrew();
});
public static readonly Planet MARS = new Planet(3, 0.642f, 6792, 3933, 3.7f,
delegate(Ship ship) {
// Actions on land of ship
ship.DeductFood(50);
ship.Research();
ship.Mine();
});
Simply turn your enumeration into a class , You've upgraded it to something more organized , And more powerful . You can also use advanced features such as reflection and inheritance , But most of the time , You don't need to .
more unity2018 Please visit paws3d Learning Center Search .
Technology