- Home /
Prevent UnityVS continually re-writing VS project files (e.g. "target framework")
How can one have UnityVS keep changes made manually to the project files? (I'm using "UnityVS 2015 preview" with "VS 1025 Ultimate preview" btw in case it's relevant.)
Details:
I'm using a Unity asset "Snowify" (excellent, btw) which is supplied as a DLL. I get the following error when I try to compile in UnityVS:
Show Details Severity Code Description File Line Project
Warning The primary reference "SnowCreator" could not be resolved because it has an indirect dependency on the .NET Framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0" than the version "2.0.0.0" in the current target framework. UnityVS.SnowmanScuffle.CSharp.Editor
Error CS0246 The type or namespace name 'SnowCreator' could not be found (are you missing a using directive or an assembly reference?) Snowify.cs 34 UnityVS.SnowmanScuffle.CSharp.Editor
To resolve, I have a painful manual process that requires re-doing every compile:
Find and right click Editor project | properties
change target framework to ".NET Framework 4"
save
compile
...
but as soon as I visit Unity Editor (or even before sometimes), it re-writes the project file and requires it be reloaded! (declining reload causes another problem -- vaguely recall it seemed the change had been partially loaded? Can investigate if necessary.)
The only potential answer I've found so far is to write a project file generator that makes the change I want manually = a pain. (yes, I was inspired by How can I make Unity open MonoDevelop with a target framework of .NET 4.0 instead of 3.5?.)
Hope I'm not missing something obvious -- I'm new to UnityVS (and VS). I've unticked "Generate solution file" and "Generate mdb files" but no luck.
Thanks!
Answer by Arkade · Jan 04, 2015 at 10:57 PM
Decided to bite the bullet and write a ProjectFileGeneration util. Works. Here it is for anyone else who runs into the same problem:
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using UnityEngine;
using UnityEditor;
using SyntaxTree.VisualStudio.Unity.Bridge;
namespace Assets.UGS.UnityUtils {
/// <summary>
/// Modify Editor project file generation to use <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>.
/// Based on http://unityvs.com/documentation/api/project-file-generation/
/// </summary>
[InitializeOnLoad]
class UnityVSProjectFileHook {
// necessary for XLinq to save the xml project file in utf8
class Utf8StringWriter : StringWriter {
public override Encoding Encoding {
get { return Encoding.UTF8; }
}
}
static UnityVSProjectFileHook() {
ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) => {
Debug.Log(string.Format("{0} starting on \"{1}\"", typeof(UnityVSProjectFileHook).Name, name));
var document = XDocument.Parse(content);
var assemblyName = document.Descendants(XName.Get(ASSEMBLY_NAME, SCHEMA)).FirstOrDefault();
if (null != assemblyName && assemblyName.Value.Contains(ASSEMBLY_CSHARP_EDITOR)) {
var target = document.Descendants(XName.Get(TARGET_FRAMEWORK_VERSION, SCHEMA)).FirstOrDefault();
if (null != target && target.Value.Contains(FRAMEWORK_EXPECTED)) {
target.SetValue(FRAMEWORK_DESIRED);
Debug.Log(string.Format("{0} set \"{1}\"'s {2} to \"{3}\"", typeof (UnityVSProjectFileHook).Name, name, TARGET_FRAMEWORK_VERSION, FRAMEWORK_DESIRED));
}
var targetProfile = document.Descendants(XName.Get(TARGET_FRAMEWORK_PROFILE, SCHEMA)).FirstOrDefault();
if (null != targetProfile && targetProfile.Value.Contains(PROFILE_EXPECTED)) {
targetProfile.SetValue(PROFILE_DESIRED);
Debug.Log(string.Format("{0} set \"{1}\"'s {2} to \"{3}\"", typeof(UnityVSProjectFileHook).Name, name, TARGET_FRAMEWORK_PROFILE, PROFILE_DESIRED));
}
}
var str = new Utf8StringWriter();
document.Save(str);
return str.ToString();
};
}
private const string SCHEMA = @"http://schemas.microsoft.com/developer/msbuild/2003";
private const string ASSEMBLY_NAME = "AssemblyName";
private const string ASSEMBLY_CSHARP_EDITOR = "Assembly-CSharp-Editor";
private const string TARGET_FRAMEWORK_VERSION = "TargetFrameworkVersion";
private const string TARGET_FRAMEWORK_PROFILE = "TargetFrameworkProfile";
private const string FRAMEWORK_EXPECTED = "v3.5";
private const string FRAMEWORK_DESIRED = "v4.0";
private const string PROFILE_EXPECTED = "Unity Full v3.5";
private const string PROFILE_DESIRED = "";
}
}
just fyi, as of today (at least)... I was able to add my dependencies to the generated UnityVS solution and have them "stick" to the solution file... but only after I generated my solution in UnityVS options, then disabled "generate solution file". After which, I was able to open the project, add the "existing project" (my dependency plugins), save the solution in Visual Studio (using 2013 update 4 currently), close VS, and reopen via UnityVS in Unity.
perhaps they fixed something somewhere :)
(I'm using Unity 5.1.3p1 & latest UnityVS 2.0.0)
Where is supposed to put this file? What are the steps to implement it? Thanks! I have the same issue!