Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ronronmx · Sep 18, 2011 at 04:20 AM · editoreditorwindow

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronronmx · Sep 18, 2011 at 06:35 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can i get the center of the entire editor window 0 Answers

Editor window script only works when window is open? 1 Answer

SetReplacementShader in Editor Utility? 0 Answers

OnSelectionChange Without EditorWindow 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges