[Range(0,1)]
public float EnemyTilesPercent;
[Range(0,1)]
public float EventTilesPercent;
[Range(0,1)]
public float ItemTitlesPercent;
So basically what I am trying to do is populate a randomly generated game board with tiles. There are 5 different types of tiles, Enemy, Item, Event, Blank, and the starting tile. The starting tile is placed at the beginning, and the blank tiles just fill in the spaces that the other tiles don’t use.
So basically there is 100% of the game board, and Enemy, Item, and Event tiles need to share that 100%. I want it so when in editor I increase one of them, if there isn’t enough % left of the board it will decrease the other 2 to make room. Any thoughts?
If you define an OnValidate() method in your monobehaviour, it will be called anytime a change is made in the inspector. Then you can perform validation on the values you are interested in.
You will need to keep track of the known values though, so you can detect which one of those variables has changed, otherwise you will have problems. The changed variable should be the dominant one. As in, the two variables that were NOT changed, should be adjusted so they all sum to the max range.
Since you want to sync more than 2 values, there is another issue that you should be aware of. If you change variable A in the editor to say 0.7, you will have to figure out how you will compensate in the other variables. Basically, you will have to split the difference. You can do something a little more fancy by splitting based on the weight of the existing values. Eg, B=0.5 and C=0.25 and you change A=0.7. You have to figure out how to adjust B and C to sum to 0.3. Since B is twice the size of C, you could set B=0.2 and C=0.1
basic example:
[Range(0,1)] public float A = 0.50f;
[Range(0,1)] public float B = 0.25f;
[Range(0,1)] public float C = 0.25f;
private float oldA, oldB, oldC;
private void OnValidate() {
// I didn't test these calculations. Take it as example only.
float remaining, ratio;
if (A != oldA) {
remaining = 1.0f - A;
ratio = B / C;
C = remaining / (1 + ratio);
B = remaining - C;
} else if (B != oldB) {
remaining = 1.0f - B;
ratio = A / C;
C = remaining / (1 + ratio);
A = remaining - C;
} else if (C != oldC) {
remaining = 1.0f - C;
ratio = B / A;
A = remaining / (1 + ratio);
B = remaining - A;
}
oldA = A;
oldB = B;
oldC = C;
}
To prime oldA, oldB and oldC for the first time, you should call your OnValidate() method from within your Start() or Awake() method.
void Start() {
OnValidate(); // sync the 'old' variables with the public ones for the first time
}