Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 UnbreakableOne · Feb 18, 2014 at 12:30 AM · textxmlsettings

Best way to store settings?

Hi,

I tried to make our game as data driven as possible and for some settings that are final now, I rather to store them out of source codes in text format that would act like DOS days ini files used to do, just store settings.

I think the new days approach is XML or something but since XML is not very intuitive to edit since it's DOM-based and you have to tag everything, I was wondering if there is a simple way that lets me store some integer and floats in a text file and the game would read them upon compilation.

Thanks.

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

Answer by whydoidoit · Feb 18, 2014 at 01:06 AM

Sure you could stick them in a .txt file and make it a TextAsset then use something like this to read them (not tested, scripted in the answer box):

   using System.Linq;
   using UnityEngine;
   using System.Collections;
   using System.Collections.Generic;

   public class Settings { 
        static Dictionary<string, string> properties;
        static Settings() {
              var contents = (Resources.Load("settings") as TextAsset).text;
              properties = contents.Split(new [] {'\n', '\r'}).Where(s=>s.length>2).Select(s=>{
                   var parts = s.Split(new [] {':'});
                   return new { key: s[0].Trim(), value: s[1].Trim() };
              }).ToDictionary(s=>s.key, s=>s.value);
        }
        static string Get(string name, string defaultValue = "") {
              return properties.ContainsKey(name) ? properties[name] : defaultValue;
        }
        static float GetFloat(string name, float defaultValue = 0f) {
               return properties.ContainsKey(name) ? System.Convert.ToSingle(properties[name]) : defaultValue;
        }
        static int GetInteger(string name, int defaultValue = 0) {
                return properties.ContainsKey(name) ? System.Convert.ToInt32(properties[name]) : defaultValue;
        }
   }

This requires the settings to be in a "Resources" folder with the name "settings" - the file looks like:

        someName: someValue
        someOtherName: 123

You read them like this:

       var myValue = Settings.Get("someName");
       var myOtherValue = Settings.GetInteger("someOtherName");
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 whydoidoit · Feb 18, 2014 at 01:08 AM 0
Share

Personally I think X$$anonymous$$L is a pretty good format as it's great at describing more complex things, arrays etc.

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Displaying text from xml at runtime on different planes 0 Answers

Best way to display long texts 0 Answers

How to update text every time I press space? 1 Answer

How To Make Settings File Over XML? 1 Answer

WWW requests are exponentially slow on large web pages (xml) 1 Answer


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