- Home /
Using System.Reflection for this code?
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
Answer by Bunny83 · Sep 18, 2011 at 05:17 AM
Reflection should be avoided if possible. It's quite slow and can cause errors that are hard to spot if used unwarily.
What i can tell is that you defined all your variables in an custom EditorWindow class. Usually if you need those variable set at different places you should create a seperate data-class.
I never used the IniParser but the IniData class seems a quite complex 2 dimensional dictionary which would be a pain with reflection. I guess to write your script with reflection it would have the same length but is even harder to understand. Also your section names can't be extracted from the variable names so it's pointless here.
A seperate data class could look like this:
public class BikeSettings
{
public int upgradePointsTotalAmount;
public int pointsNeededForNextUpgrade;
public float camSwitchDurationBase;
public float camSwitchDurationMultiplier;
/* ... */
private static string upgradePoints = "UpgradePoints";
private static string camUpgrades = "CamUpgrades";
private static string boostUpgrades = "BoostUpgrades";
private static string landingUpgrades = "LandingUpgrades";
private static string startUpgrades = "StartUpgrades";
public void SaveToFile( string fileName )
{
IniParser.IniData data = Parser.GetFileData( System.IO.File.ReadText(fileName) );
data[upgradePoints]["UpgradePointsTotalAmount"] = upgradePointsTotalAmount.ToString();
data[upgradePoints]["PointsNeededForNextUpgrade"] = pointsNeededForNextUpgrade.ToString();
/* ... */
Parser.SaveDataToFile( fileName, data );
}
public void LoadFromFile( string fileName )
{
IniParser.IniData data = Parser.GetFileData( System.IO.File.ReadText(fileName) );
/* ... */
}
}
This class can also be used at runtime to hold this information.
Got it, thanks for the tips and example! $$anonymous$$aking 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