writelines和writeline(我使用c#计算出大量的点(x,y,z),现在想将这些点写进一个记事本文件*.txt)
本文目录
我使用c#计算出大量的点(x,y,z),现在想将这些点写进一个记事本文件*.txt
追加写入就行了,方式有很多,比如,至于你怎么区分那就随你的了,比如全用A,x,y,z这种格式,读取的时候读出一行后用split切割就行了。
The File class provides three methods to append an existing file.
AppendText
AppendAllText
AppendAllLines
The AppendText method creates a StreamWriter object that appends UTF-8 encoded text to an existing text file.
string fileName = @"C:\Temp\Mahesh.txt";
//示例
int x=3,y=5,z=7;
using (StreamWriter sw =File.AppendText(fileName))
{
sw.WriteLine(string.Format("{0},{1},{2},{3}","A",x,y,z));//示例
sw.WriteLine("--------- Append Text Start ----------");
sw.WriteLine("New author started");
sw.WriteLine("a book on Files Programming ");
sw.WriteLine("using C#");
sw.WriteLine("--------- Append Text End ----------");
}
The AppendAllText method opens a file, appends a specified string to the file and then closes the file. If the file does not exist, this method creates a new file, writes the specified string to the file, and then closes the file.
string fileName = @"C:\Temp\Mahesh.txt";
string appendText = "Append all text method ----";
File.AppendAllText(fileName, appendText);
File.AppendAllText(fileName,"Append some more text");
File.AppendAllText(fileName,"Append all text end");
// Read all text
string readText = File.ReadAllText(fileName);
C***ole.WriteLine(readText);
The AppendAllLines method appends lines to a file and then closes the file.
// We can read a line from one file and write to other file
string fileName = @"C:\Temp\Mahesh.txt";
string lines = File.ReadAllLines(fileName);
// Write all lines to a new file
string outputFileName = @"C:\temp\WriteLinesFile.txt";
File.WriteAllLines(outputFileName, lines);
// Append more lines
string{"Line 1 goes here", "Line 2 goes here" };
File.AppendAllLines(outputFileName, moreLines);
// Read all text
string readText = File.ReadAllText(outputFileName);
C***ole.WriteLine(readText);
u3d assetbundle能热更代码吗
原先的AssetBundle系统需要自己写一大坨导出的代码(BuildPipeline),这个新手会无从下手,老手也经常会被坑到。想正确处理好资源的依赖关系从而保证资源完整而又不会产生重复资源,确实不是一件非常容易的事情。
Unity5新的AssetBundle系统大大简化了这一操作。Unity打包的时候会自动处理依赖关系,并生成一个.manifest文件,这个文件描述了assetbundle包大小、crc验证、包之间的依赖关系等等,是一个文本文件。加载资源的时候Unity会自动处理好其依赖包的加载。(参见最后的补充,Unity并没有如此智能,新的assetbundle简化很多,但是也有一些新的坑)
打包代码简化为一个函数(其实也没什么必要了,因为流程固定了,做成内嵌的菜单选项也没什么影响)
view plain copy
BuildPipeline.BuildAssetBundles(outputPath);
执行这个函数,它会自动打包工程目录下的所有的assetbundle,函数足够智能,它只会打包有修改的资源。
如何添加一个AssetBundle呢?
很简单,在资源属性窗口底部有一个选项,这个地方设置AssetBundle的名字。它会修改资源对应的.meta文件,记录这个名字。 AssetBundle的名字固定为小写。另外,每个AssetBundle都可以设置一个Variant,其实就是一个后缀,实际AssetBundle的名字会添加这个后缀。如果有不同分辨率的同名资源,可以使用这个来做区分。
我手头的模型资源非常多,所以我又写了个脚本自动遍历prefab的meta文件,添加AssetBundle名字。有一个需要注意的地方就是.meta文件貌似权限问题,无法直接写入,需要删除原文件,然后使用新的文件替换。。
view plain copy
# -*- coding: utf-8 -*-
import os, sys, shutil;
EXT_LIST = ;
def doWork(path):
for root, dirs, files in os.walk(path):
for file in files:
for ext in EXT_LIST:
if file.endswith(ext):
fullPath = os.path.join(root, file)
fullPath = fullPath.replace(’\\’, ’/’)
prefabName = fullPath.replace(path, ’’);
prefabName = prefabName + ’.data’;
fileData = ;
fp = open(fullPath, ’r’);
for line in fp:
if line.find(’assetBundleName:’) != -1:
fileData.append(’ assetBundleName: ’ + prefabName.lower() + ’\n’);
else:
fileData.append(line);
fp.close();
# os.remove(fullPath);
# return;
fpw = open(fullPath + ’.tmp’, ’w’);
fpw.writelines(fileData);
fpw.close();
os.remove(fullPath)
shutil.copy(fullPath + ’.tmp’, fullPath);
os.remove(fullPath + ’.tmp’)
break;
doWork(r’Assets/Resources/Prefab/’)
os.system(’PAUSE’)
c#器扩展(与Python代码功能一样,喜欢哪个用哪个)
view plain copy
public class ExportAssetBundles : Editor
{
// 设置assetbundle的名字(修改meta文件)
static void OnSetAssetBundleName()
{
UnityEngine.Object obj = Selection.activeObject;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
string { ".prefab.meta", ".png.meta", ".jpg.meta" , ".tga.meta" };
EditorUtil.Walk(path, extList, DoSetAssetBundleName);
//刷新器
AssetDatabase.Refresh();
Debug.Log("AssetBundleName修改完毕");
}
static void DoSetAssetBundleName(string path)
{
path = path.Replace("\\", "/");
int index = path.IndexOf(EditorConfig.PREFAB_PATH);
string relativePath = path.Substring(path.IndexOf(EditorConfig.PREFAB_PATH) + EditorConfig.PREFAB_PATH.Length);
string prefabName = relativePath.Substring(0, relativePath.IndexOf(’.’)) + EditorConfig.ASSETBUNDLE;
StreamReader fs = new StreamReader(path);
List《string》 ret = new List《string》();
string line;
while((line = fs.ReadLine()) != null) {
line = line.Replace("\n", "");
if (line.IndexOf("assetBundleName:") != -1) {
line = " assetBundleName: " + prefabName.ToLower();
}
ret.Add(line);
}
fs.Close();
File.Delete(path);
StreamWriter writer = new StreamWriter(path + ".tmp");
foreach (var each in ret) {
writer.WriteLine(each);
}
writer.Close();
File.Copy(path + ".tmp", path);
File.Delete(path + ".tmp");
}
static void OnCreateAssetBundle()
{
BuildPipeline.BuildAssetBundles(EditorConfig.OUTPUT_PATH);
//刷新器
AssetDatabase.Refresh();
Debug.Log("AssetBundle打包完毕");
}
}
补充:
之前我简单的认为Unity既然已经做了资源依赖链,那么加载资源的时候理所应当的会自动处理好依赖关系。结果是我想的太简单了。我们必须自己手动加载依赖的assetbundle。
我们直接调用打包函数,在打包目录会生成一个与打包目录同名的文件,比如我们打包到AssetsBundle/windows目录下,则此目录下会生成一个windows文件,以及一个windows.manifest文件。
这个windows文件就包含了当前assetbundle的列表和依赖关系。我们需要在游戏开始的时候手动加载这个assetbundle(即windows),获取一个AssetBundleManifest对象。这个对象有GetAllAsssetBundles()函数还有一个GetAllDependencies函数。
我们可以使用GetAllDependencies传入一个assetbundle的名字来获取其依赖的assetbundle,然后分别加载这些assetbundle。注意传入的文件名是我们生成assetbundle设置的名字,而不是全路径。可以参考windows.manifest文件查看assetbundle名字形式。
新的问题:
由于我的资源量比较大,所以我拆分了多个生成工程。而生成assetbundle的时候指向的是一个路径,最终合并后就是一个完整的游戏资源包。我们在游戏运行加载资源的时候需要windows这个manifest文件,而它又是Unity自己生成的,并且只保存了当前项目生成的assetbundle信息,无法自动合并。从这点来说它无法满足我的需求。
我们之所以需要这个manifest文件,是因为我们要从中获取依赖关系。所以我在项目中自己维护资源列表。它可以根据生成的manifest自动生成、合并资源列表(一个json格式的文本文件)。我们在游戏开始时解析这个json文件获取到资源列表。这个资源列表同时也可以作为自动更新的文件列表。
实现方式很简单,assetbundle打包完毕后,加载manifest文件(注意这里有个坑,我会在另一篇文章里面讲解如何在Editor下加载AssetBundle),获取所有资源信息,与当前已存在的资源列表进行比对,合并。多个项目之间只要维护好一份统一的资源列表就可以了。
补充2:
1、似乎没有必要给所有的模型创建prefab来使用,我们可以直接把fbx打进AssetBundle,然后实例化这个fbx就可以得到正确的GameObject。这个在我的地图文件里面非常必要,因为地图文件很小很碎很多,如果非要创建prefab的话,可能prefab都要占几十兆的空间。而在使用过程中,理论上我不会对这种模型做特殊的修改,仅仅是当做资源来使用。 其实人物等模型也是如此,如果仅仅当做资源来使用的话,是没有必要创建prefab的。不过如果要绑定一些脚本(理论上也可以通过代码来搞定),设置物理参数就需要一个prefab来保存这些信息了。
2、不可以给一个文件夹设置assetbundle名字,会报错。 把文件夹下面的所有文件设置成一样的名字,就可以把这些资源打包到一个assetbundle里面。
3、所有可能共用的资源(比如公共纹理,尤其是shader)一定要设置assetbundle的名字,这样这些会单独打包,并且其他使用这些资源的assetbundle会自动依赖这些包。如果shader不设置打包的话,则每个assetbundle里面都有一份shader,这样就浪费了很多空间,并且当修改shader内容的时候,所有的包都需要重新制作。
4、加载完assetbundle,然后使用LoadAsset加载资源。传入的名字要么是以Assets/开头的全路径,要么只有一个文件名。其他形式的名字无**确匹配到对应资源。如果只有一个文件名,要保证这个assetbundle内不包含同名,同类型的文件,否则会返回第一个查找到的资源。 名字大小写无关。
C#如何向文本文件中写入
写文本不需要弹出窗体。给你个函数
public static void MakeFile(string FileName,string Content)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(FileName,false,System.Text.Encoding.GetEncoding("gb2312"));
try
{
sw.Write(Content);
sw.Flush();
}
finally
{
if ( sw != null ) sw.Close();
}
}// MakeFile
把这个函数放你的代码里,filename是文件名字,content是内容。
读文件
System.IO.FileInfo fi = new FileInfo(@"c:\test.text");
StreamReader sr = fi.OpenText();
string s1 = sr.ReadToEnd(); //全读出来
或者一行一行的读。
string strLine = string.Empty;
while ((strLine = sr.ReadLine()) != null)
{
strLine 就是一行,
}