解决Unity每开一次c#脚本都打开新visual studio窗口问题
在Unity中打开c#(.cs)脚本每单独打开新的visual studio窗口。
要想所有c#脚本都在同一窗口打开怎么办?
解决:
打开visual studio-解决方案资源管理器中-选择你的项目-右键,选择“设为启动项目” 。
在Unity中打开c#(.cs)脚本每单独打开新的visual studio窗口。
要想所有c#脚本都在同一窗口打开怎么办?
解决:
打开visual studio-解决方案资源管理器中-选择你的项目-右键,选择“设为启动项目” 。
在Unity中调用TexturePacker打包图集,核心代码如下:
Process p = new Process ();
p.StartInfo.FileName = "TexturePacker";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
在Windows上运行正常,Mac上报异常:
Win32Exception: ApplicationName='TexturePacker', CommandLine='',
CurrentDirectory='' System.Diagnostics.Process.Start_noshell
(System.Diagnostics.ProcessStartInfo startInfo,
System.Diagnostics.Process process)
System.Diagnostics.Process.Start_common
(System.Diagnostics.ProcessStartInfo startInfo,
System.Diagnostics.Process process) System.Diagnostics.Process.Start
() (wrapper remoting-invoke-with-check)
System.Diagnostics.Process:Start ()
一番查找发现,Mac上应该用open命令调用目标应用,Mac版本的代码修改如下:
在Unity中使用Mono库System.Drawing.dll处理图像,Windows上一切正常,在Mac上运行报异常:
DllNotFoundException: gdiplus.dll
System.Drawing.GDIPlus..cctor ()
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for System.Drawing.GDIPlus
System.Drawing.Bitmap..ctor (Int32 width, Int32 height, PixelFormat format)
System.Drawing.Bitmap..ctor (Int32 width, Int32 height)
(wrapper remoting-invoke-with-check) System.Drawing.Bitmap:.ctor (int,int)
System.Drawing.dll内部依赖于gdiplus.dll,gdiplus.dll在Window上属于系统自带库所以一切正常。Mac上不存在gdiplus.dll,也未在网上找到独立的Mono版gidplus.dll。
但在Unity和MonoDevelop安装路径下有个libgdiplus.dylib,倒腾到dllmap中依旧异常,这两个路径下带的libgdiplus.dylib都不能正常使用。
最后安装了完整的Mono环境才搞定,Unity自带的Mono运行时是经过定制的这也能理解。
解决步骤如下:
在Unity3D开发项目时,有时需要进行路径是否存在的断定:
System.IO.File.Exist(string filePath);
System.IO.Directory(string dirPath);
注意这接口只是判定磁盘(闪存)物理路径上是否存在某个文件(目录),我们知道Application.streamingAssetsPath
取到的是StreamingAssets资源包路径,此资源包是Unity3D在编译时将工程目录StreamingAssets中的资源编译打包后生成的。
所以,我们尝试:
System.IO.File.Exist(Application.StreamingAssets + "/file.txt");
这样去判定文件是否存在时,会返回False,因为这本就不是一个文件物理路径。
Application.persistentDataPath
所指向路径,及其下的文件不会被Unity3D编译打包,其中的文件或目录是可以使用System.IO.File.Exist()接口判断是否存在的
c#中直接以XmlDocument.Save()
接口以UTF-8编码保存的XML文件是包含BOM头的,怎么以无BOM头UTF-8编码保存XML呢?
参考代码如下:
/// <summary>
/// 以UTF-8无BOM编码保存xml至文件。
/// </summary>
/// <param name="savePath">保存至路径</param>
/// <param name="xml"></param>
public static void SaveXmlWithUTF8NotBOM(string savePath, XmlDocument xml)
{
StreamWriter sw = new StreamWriter(savePath, false, new UTF8Encoding(false));
xml.Save(sw);
sw.WriteLine();
sw.Close();
}