How to create a config file?

 
Post new topic   Reply to topic    Unity Community Index // iPhone Development
View previous topic :: View next topic  
Author Message
Minevr



Joined: 04 Mar 2008
Posts: 590
Location: China

PostPosted: Mon Nov 23, 2009 6:49 pm    Post subject: How to create a config file? Reply with quote
I would like to create a configuration file, use the "config.xml", but can not on the "Documents" directory. How can I do? I tried on the "Resources", but Build, it disappeared.

Application.dataPath return don't contain "myappname.app".

like:/var/mobile/Applications/2808624E-16E7-4AC9-8771-C9241248F69F/Data


I created a "/myappname.app/config" directory, put the configuration file into?

Mad

_________________
My Blog:Http://Www.1Vr.Cn
Unity Tencent QQ Group:2453819,34643309
Unity Forum: http://www.iu3d.com
Development Environment:Unity Pro & iPhone Advanced & MacBook Pro MB226 & iPhone & iPod & iPad.
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number
rozgo



Joined: 07 Feb 2008
Posts: 142
Location: San Francisco & Caribbean

PostPosted: Tue Nov 24, 2009 10:19 am    Post subject: Reply with quote
I have a text file in Resources/English.txt with:

Code:

[EnergyTank]
(You found an energy tank)

[Hotspot|CNBO|10]
We made it!
Now lets get out of here.

[Hotspot|CNDN|1]
Look, over here!
A checkpoint, finally.
Save your progress
before something terrible happens.

[Fallen|CN|1]
Looking up or down while standing still
may reveal lurking enemies, threats,
and other secrets.
Can you see the orb up there?

[Fallen|CN|2]
You can enter doors on ceilings
by shooting straight up
and jumping into to them.



And use this to read from it:

Code:

using UnityEngine;
using System.Collections;

public class TextTable {

   public static string[] GetLines (string id) {
      ArrayList lines = new ArrayList();
      string line;
      TextAsset textFile = (TextAsset)Resources.Load("English", typeof(TextAsset));
      System.IO.StringReader textStream = new System.IO.StringReader(textFile.text);
      string lineID = "[" + id + "]";
      bool match = false;
      while((line = textStream.ReadLine()) != null) {
         if (match) {
            if (line.StartsWith("[")) {
               break;
            }
            if (line.Length > 0) {
               lines.Add(line);
            }
         }
         else if (line.StartsWith(lineID)) {
            match = true;
         }
      }
      textStream.Close();
      if (lines.Count > 0) {
         return (string[])lines.ToArray(typeof(string));
      }
      return new string[0];
   }
}


Ignore the parsing part, the loading is what may be of interest to you.

_________________
rozgo aka Alexander Orozco
independent game developer
past: Munkyfun | Lucas Arts
http://www.grokion.com
Back to top
View user's profile Send private message Visit poster's website
Minevr



Joined: 04 Mar 2008
Posts: 590
Location: China

PostPosted: Tue Nov 24, 2009 5:16 pm    Post subject: Reply with quote
Thanks,But I build to Xcode project.

I did not see a txt.... Confused

_________________
My Blog:Http://Www.1Vr.Cn
Unity Tencent QQ Group:2453819,34643309
Unity Forum: http://www.iu3d.com
Development Environment:Unity Pro & iPhone Advanced & MacBook Pro MB226 & iPhone & iPod & iPad.
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number
silentchujo



Joined: 25 Nov 2008
Posts: 229
Location: Portland, OR

PostPosted: Tue Nov 24, 2009 8:53 pm    Post subject: Reply with quote
If you put a .txt file into the Resources directory it doesn't show up as a file with the same name in any directory.

If you want to write to a file in the Documents directory on the iPhone you have to modify the path returned by Application.DataPath.

Code:
string docsPath = Application.dataPath.Replace("/Data", "/Documents");


It's okay that this path doesn't contain your app's name. The GUID is what's needed.

I've only tested the ReadFile function, but these should work for modifying files in the Documents directory. All other directories on the iPhone are read only.
Code:
using UnityEngine;
using System.Collections;
using System.IO;

public class FileUtilities {
   
   public static byte[] ReadFile(string path) {
      Debug.Log("Attempting to read file "+ path);
      FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
      byte[] thebytes = new byte[(int) fs.Length];
      fs.Read(thebytes, 0, (int) fs.Length);
      fs.Close();
      
      return thebytes;
   }
   
   public static void WriteFile(string path, byte[] bytes) {
      Debug.Log("Attempting to write file "+ path);
      FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);
      fs.Write(bytes, 0, bytes.Length);
      fs.Close();
   }
   
   public static bool FileExists(string path) {
      return File.Exists(path);   
   }
}

_________________
Go get Azplode!
Back to top
View user's profile Send private message
Minevr



Joined: 04 Mar 2008
Posts: 590
Location: China

PostPosted: Wed Nov 25, 2009 6:12 am    Post subject: Reply with quote
Very Happy Thanks for all.

But,I want put config file in "MyAppName.app".

On Xcode,Can seen it,Cen edit it. Laughing

_________________
My Blog:Http://Www.1Vr.Cn
Unity Tencent QQ Group:2453819,34643309
Unity Forum: http://www.iu3d.com
Development Environment:Unity Pro & iPhone Advanced & MacBook Pro MB226 & iPhone & iPod & iPad.
Back to top
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number
silentchujo



Joined: 25 Nov 2008
Posts: 229
Location: Portland, OR

PostPosted: Wed Nov 25, 2009 7:16 am    Post subject: Reply with quote
Did you see this thread? They were successful loading files from the app bundle:
http://forum.unity3d.com/viewtopic.php?t=37510

I don't know if it's possible to write files to the app bundle though. OS might not allow it.

_________________
Go get Azplode!
Back to top
View user's profile Send private message
Post new topic   Reply to topic    Unity Community Index // iPhone Development All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum