I recently learned about using System.Reflection in CSharp and I’m wondering how I would use Reflection in the following code to shorten it and make it more reusable?
using UnityEngine;
using UnityEditor;
using TextFileParser;
public class BikeUpgradesTool : EditorWindow {
// Store UpgradeSettings file from Resources
TextAsset file;
// Store section block names for quick access
string upgradePoints = "UpgradePoints";
string camUpgrades = "CamUpgrades";
string boostUpgrades = "BoostUpgrades";
string landingUpgrades = "LandingUpgrades";
string startUpgrades = "StartUpgrades";
int upgradePointsTotalAmount, pointsNeededForNextUpgrade;
float camSwitchDurationBase, camSwitchDurationMultiplier;
int camSwitchCountBase, camSwitchCountMultiplier;
float boostPowerBase, boostPowerMultiplier;
float boostChargeBase, boostChargeMultiplier;
float boostTopSpeedBase, boostTopSpeedMultiplier;
float boostRechargeWaitBase, boostRechargeWaitMultiplier;
float perfectLandingBoostBase, perfectLandingBoostMultiplier;
float perfectLandingDelayBase, perfectLandingDelayMultiplier;
float perfectStartBoostBase, perfectStartBoostMultiplier;
float perfectStartDelayBase, perfectStartDelayMultiplier;
private void SaveSettings ( ) {
// Create the data to save
IniParser.IniData data = Parser.GetFileData( file.text );
data[this.upgradePoints]["UpgradePointsTotalAmount"] = upgradePointsTotalAmount.ToString();
data[this.upgradePoints]["PointsNeededForNextUpgrade"] = pointsNeededForNextUpgrade.ToString();
data[this.camUpgrades]["CamSwitchDurationBase"] = camSwitchDurationBase.ToString();
data[this.camUpgrades]["CamSwitchDurationMultiplier"] = camSwitchDurationMultiplier.ToString();
data[this.camUpgrades]["CamSwitchCountBase"] = camSwitchCountBase.ToString();
data[this.camUpgrades]["CamSwitchCountMultiplier"] = camSwitchCountMultiplier.ToString();
data[this.boostUpgrades]["BoostPowerBase"] = boostPowerBase.ToString();
data[this.boostUpgrades]["BoostPowerMultiplier"] = boostPowerMultiplier.ToString();
data[this.boostUpgrades]["BoostChargeBase"] = boostChargeBase.ToString();
data[this.boostUpgrades]["BoostChargeMultiplier"] = boostChargeMultiplier.ToString();
data[this.boostUpgrades]["BoostTopSpeedBase"] = boostTopSpeedBase.ToString();
data[this.boostUpgrades]["BoostTopSpeedMultiplier"] = boostTopSpeedMultiplier.ToString();
data[this.boostUpgrades]["BoostRechargeWaitBase"] = boostRechargeWaitBase.ToString();
data[this.boostUpgrades]["BoostRechargeWaitMultiplier"] = boostRechargeWaitMultiplier.ToString();
data[this.landingUpgrades]["PerfectLandingBoostBase"] = perfectLandingBoostBase.ToString();
data[this.landingUpgrades]["PerfectLandingBoostMultiplier"] = perfectLandingBoostMultiplier.ToString();
data[this.startUpgrades]["PerfectStartBoostBase"] = perfectStartBoostBase.ToString();
data[this.startUpgrades]["PerfectStartBoostMultiplier"] = perfectStartBoostMultiplier.ToString();
Parser.SaveDataToFile( Application.dataPath + "/Resources/UpgradeSettings.txt", data );
}
}
Any help or example will be greatly appreciated, as always ![]()
Thanks for your time,
Stephane
Got it, thanks for the tips and example! Making a separate class for those settings does make more sense. I need to learn more about Reflection so that I know when and where to use it if needed. Thanks again for your help! Stephane
– ronronmx