使用基于Unity3d的项目升级Xcode Objective-C IOS游戏



我有一款几年前推出的Xcode/ObC智力竞赛游戏,在swift之前,对我来说一直都很成功。至少是本地版本。

我现在在Unity3d c#中重写这个游戏的终点线。

我最近一直在思考的是如何保持;旧的";保存在IOS的plist文件中的统计信息。我一直在谷歌上搜索,但我需要更多的信息来真正了解如何进行。

  1. 当我用新的基于Unity的项目升级当前的Xcode/ObC时,stats-plist文件会发生什么?它还会在那里吗?是否可以轻松找到它?这个特定的plist是在添加第一个玩家时添加的,然后用统计数据和新玩家更新。

  2. 有没有一种好的、简单的方法从Unity读取plist并转换为普通文本文件?

  3. 为了能够从Unity找到文件,我正在考虑推出一个基于ObC的游戏的维护版本,只将这个plist文件复制到另一个目录(Document(,为新的大版本做准备。当第一次开始基于Unity的游戏时,我可以读取复制的文件并进行处理,这样玩家就不会丢失他/她的统计数据。

我的问题是,在过去的5-6年里,我唯一一次更新实际的ObC代码是当我将应用程序从32位更新到64位时,所以我在ObC上的技能目前非常有限。

我一直在考虑在plist中使用这样的东西:

NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
if ([filemgr copyItemAtPath: @"/tmp/myfile.txt" toPath: @"/Users/demo/newfile.txt" error: NULL]  == YES)
NSLog (@"Copy successful");
else
NSLog (@"Copy failed");

如果能给我一些建议,我将不胜感激。

这里有一些代码可以用来列出文档和应用程序支持的内容。我用#define来保护它,因为我不希望它出现在最终的应用程序中。我还用它来执行一些清理(注释掉的东西(,如果你需要删除一些东西,你可以使用它。

#ifdef DEBUG
// Auxiliary function to list directory contents
+ (void) docList
{
NSFileManager * fileManager = NSFileManager.defaultManager;
NSURL         * docUrl      = [NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject;
NSString      * docPath     = docUrl.path;
NSLog( @"User document directory" );
NSLog( @"%@ listing follows", docPath );
[fmUtil traverse:docPath tab:@"t" fileManager:fileManager];
}
// Auxiliary function to list application support path
+ (void) supList
{
NSFileManager * fileManager = NSFileManager.defaultManager;
NSURL         * docUrl      = [NSFileManager.defaultManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask].firstObject;
NSString      * docPath     = docUrl.path;
NSLog( @"Application support directory" );
NSLog( @"t%@", docPath );
NSLog( @"tAppending bundle identifier" );
docPath = [docPath stringByAppendingPathComponent:NSBundle.mainBundle.bundleIdentifier];
NSLog( @"t%@", docPath );
NSLog( @"%@ listing follows", docPath );
[fmUtil traverse:docPath tab:@"t" fileManager:fileManager];
}
+ (void) traverse:(NSString *)root tab:(NSString *)tab fileManager:(NSFileManager *)fileManager
{
NSArray * dir = [fileManager contentsOfDirectoryAtPath:root error:NULL];
for ( NSString * s in dir )
{
// See if this is a directory or not
NSString * t = [root stringByAppendingPathComponent:s];
BOOL isDir = NO;
[fileManager fileExistsAtPath:t isDirectory:& isDir];
if ( isDir )
{
// Report
NSLog(@"%@%@/",tab,s);
// Traverse
[fmUtil traverse:t tab:[tab stringByAppendingString:@"t"]
fileManager:fileManager];
}
else
{
// Get size of normal file
NSDictionary       * fa = [fileManager attributesOfItemAtPath:t error:NULL];
NSNumber           * fz = [fa objectForKey:NSFileSize];
NSDate             * fm = [fa objectForKey:NSFileModificationDate];
unsigned long long    n = fz.unsignedLongLongValue;
// Some formatting
NSString * f = s;
while ( f.length < 50 )
{
f = [f stringByAppendingString:@" "];
}
NSLog(@"%@%@ %15llu bytes (%@)", tab, f, n, [fm descriptionWithLocale:NSLocale.currentLocale] );
// To delete something for test purposes ...
/*
if ( [t.lastPathComponent isEqualToString:@"P5041-1-BuildingStatistics"] && [fileManager removeItemAtPath:t error:NULL] )
{
NSLog( @"%@%@ now xxxxxxxxxxxxxxxxxx", tab, f );
}
if ( [t.lastPathComponent isEqualToString:@"index"] && [fileManager removeItemAtPath:t error:NULL] )
{
NSLog( @"%@%@ now xxxxxxxxxxxxxxxxxx", tab, f );
}
*/
}
}
}
#endif

将其放在您的应用程序代理中,例如放在application:didFinishLaunchingWithOptions:中。

好吧,今天我开始回顾这个"问题";经过测试,结果证明这根本不是问题。我最初试图通过我的Objective-C代码(这篇文章(找到路径,但当测试时,我意识到这些文件存储在(IOS游戏(Document目录中,与";Application.persistentDataPath";。在Unity中,我使用了以下路径:

filePathPlayerData = Application.persistentDataPath + "/playerdata.plist";
filePathPlayerStats = Application.persistentDataPath + "/playerstatistics.plist";

读取和处理该文件很容易,因为它是XML结构。然而,由于格式简单,我以文本文件的形式打开了这些文件,并用c#(unity(编写了自己的简单解析器,而不是使用XML解析器。整个";"问题";结果没什么问题。

最新更新