I have am developing in Unity 2019.2.3f1. I am trying to write a script that can Customize project files created by VSTU. In case the link ever gets removed, I have included a script very similar to the one from the link.
#if ENABLE_VSTU
using System.IO;
using System.Text;
using System.Xml.Linq;
using UnityEditor;
using SyntaxTree.VisualStudio.Unity.Bridge;
[InitializeOnLoad]
public class ProjectFileHook
{
private class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
static ProjectFileHook()
{
ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
{
// parse the document and make some changes
XDocument document = XDocument.Parse(content);
document.Root?.Add(new XComment("FIX ME"));
// save the changes using the Utf8StringWriter
Utf8StringWriter str = new Utf8StringWriter();
document.Save(str);
return str.ToString();
};
}
}
#endif
The issue is, using SyntaxTree.VisualStudio.Unity.Bridge; fails to compile due to the error error CS0246: The type or namespace name 'SyntaxTree' could not be found (are you missing a using directive or an assembly reference?).
I have checked both Unity and Visual Studio that the Visual Studio Tools for Unity are installed and enabled.
Why is the code failing to compile? Am I missing something that is preventing it from compiling?
Just incase anyone else has this issue.
This issue was being caused by the scripts position. The script has to be under the Editor folder, otherwise the script won't work as expected.
Credits to therealjohn for pointing it out here.
Related
I'm trying to compile a project I'm working on and this error mesages pops out:
Invalidating makefile for SpaceShooterEditor (SpaceShooter.Build.cs modified)
While compiling E:\ue projects\SpaceShooter\Intermediate\Build\BuildRules\SpaceShooterModuleRules.dll:
e:\ue projects\SpaceShooter\Source\SpaceShooter\SpaceShooter.Build.cs(3,29) : error CS0246: The type or namespace name 'ModuleRules' could not be found (are you missing a using directive or an assembly reference?)
e:\ue projects\SpaceShooter\Source\SpaceShooter\SpaceShooter.Build.cs(5,25) : error CS0246: The type or namespace name 'ReadOnlyTargetRules' could not be found (are you missing a using directive or an assembly reference?)
ERROR: Unable to compile source files.
Any idea why this might happen? Last night i was trying to code a widget, modified the build.cs, then replaced the modified version (which chrashed the game) with a build.cs that worked previously, and still nothing? Is there any hope to make it work or should i start over? Moreover, how can this be avoided?
I already did the restarts and refreshes. I went to even delete the binaries and some cashed files and it didn't work.
Below you'll find the content of the Build.cs:
public class SpaceShooter : ModuleRules
{
public SpaceShooter(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
When i try to input specify the using UnrealBuildTool;, Visual Studio, for some reason, deletes it when i hit compile or save.
After some digging, i found out that wrapping the content in a namespace UnrealBuidTool.Rules{} solves this. In the end, the build file looks like this:
namespace UnrealBuildTool.Rules
{
public class SpaceShooter : ModuleRules
{
public SpaceShooter(ReadOnlyTargetRules Target) : base(Target)
{
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" });
PrivateDependencyModuleNames.AddRange(new string[] { });
// Uncomment if you are using Slate UI
// PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
// Uncomment if you are using online features
// PrivateDependencyModuleNames.Add("OnlineSubsystem");
// To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true
}
}
}
As you do not share what is included in your Build.cs file I will just explain what the error is and how you could fix it. The error basically says that it couldn't compile the Build.cs file based on the lines 29 and 25, and that the types that are used there called 'ModuleRules' and 'ReadOnlyTargetRules' could not be found in any of the namespaces that were included by the using statements at the top of Build.cs. These types are both stored in the UnrealBuildTool namespace and can thus be included in your file by typing:
using UnrealBuildTool;
This however seems like a trivial solution to your problem, but as you do not share a lot of info this is the best solution I can give you for now.
I installed flawless this package:
https://www.nuget.org/packages/ini-parser/
https://github.com/rickyah/ini-parser
It shows up in my project dependencies and i am using the directive suggested
by the author:
using IniParser;
using IniParser.Model;
But the "code" is not seen in my Form1.cs where i put the directive ?
Error CS0246.
I am a beginner so you know.
you may need to understand how to apply [using][1] and differences between Directive and static Directive and normally diagnostic with fully qualified type name to understand better.
I installed the nuget and put the using directives in and could instantly use it
namespace WindowsFormsApp1
{
using System.Windows.Forms;
using IniParser;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var parser = new FileIniDataParser();
var data = parser.ReadFile("Configuration.ini");
var useFullScreenStr = data["UI"]["fullscreen"];
var useFullScreen = bool.Parse(useFullScreenStr);
data["UI"]["fullscreen"] = "true";
parser.WriteFile("Configuration.ini", data);
}
}
}
It was an easy solution. I had to refresh the library or package dependencies and it instantly found it. Thank you for the answers.
I am trying to process XML with C# in Visual Studio and it will not allow me to import System.Xml. I looked at other posts about this problem and this was usually caused by misspelling System.Xml to System.XML. I have the right spelling so I am not sure what is causing me problems. I looked under references and there is no System.Xml and I am using Visual Studio 2013 The error message was
Error 1 The type or namespace name 'Xml' does not exist in the namespace 'System' (are you missing an assembly reference?)
Do I have to download System.Xml?
Here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace WebTesting
{
class Test1
{
public static void Main(string [] args)
{
using(XmlReader reader = XmlReader.Create("myData.Xml"))
{
while(reader.Reader())
{
if(reader.IsStartElement())
{
Console.Write("The start element is " + reader.ReadString());
}
}
}
Console.ReadLine();
}
}
}
You'll need to add an assembly reference. Right-click "References" under your project in Solution Explorer and select "Add Reference..." Switch to the .NET tab and find System.Xml. Click OK.
by using XDocument (in using System.Xml.Linq;) you'll get more flexible functionality to handling XML-documents
Okay, I'm a newbie in C# but I need to create a simple GUI, but I don't have Visual Studio (I use Geany and Mono).
The problem is, when I tried the following code that I found by Google:
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
public class FirstForm : Form
{
private Container components;
private Label howdyLabel;
public FirstForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
components = new Container ();
howdyLabel = new Label ();
howdyLabel.Location = new Point (12, 116);
howdyLabel.Text = "Howdy, Partner!";
howdyLabel.Size = new Size (267, 40);
howdyLabel.AutoSize = true;
howdyLabel.Font = new Font (
"Microsoft Sans Serif",
26, System.
Drawing.FontStyle.Bold);
howdyLabel.TabIndex = 0;
howdyLabel.Anchor = AnchorStyles.None;
howdyLabel.TextAlign = ContentAlignment.MiddleCenter;
Text = "First Form";
Controls.Add (howdyLabel);
}
public static void Main()
{
Application.Run(new FirstForm());
}
}
I just get these errors when trying to compile:
C:\C#\test2.cs(2,14): error CS0234: The type or namespace name 'Windows' does not exist in the namespace 'System'. Are you missing an assembly reference?
C:\C#\test2.cs(4,14): error CS0234: The type or namespace name 'Drawing' does not exist in the namespace 'System'. Are you missing an assembly reference?
C:\C#\test2.cs(9,11): error CS0234: The type or namespace name 'Label' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 3 error(s), 0 warnings
I downloaded both DLL's, but I don't know what to do next.
Link to the code: http://www.informit.com/articles/article.aspx?p=27316
You're using the Microsoft WinForms UI library, which Mono does not include.
You need to use a Mono-compatible UI library, such as GTK#.
You can also use the Mono port of WinForms.
You are using WinForm on mono, maybe will be better use GTK#, however if you want use WinForms on mono it is possible. Take a look at this documentation for more information .
You have to use gmcs in this way to compile your program :
gmcs Program.cs -r:System.Windows.Forms.dll
I'm trying to create a Silverlight application (for the first time) that involves parsing XML from a site and displaying information. To do this I am using Visual Studio 2008 on Windows XP Service Pack 3. I also have .NET Framework 3.5 SP1 installed.
My problem is that no XML-parser I have seen on the internet works. The top of my code I have both lines I believe are necessary (using "System.xml;" and using "System.linq;") but XDocument, XMLReader, XMLDocument, and any others I have found do not work, returning the error that the type or namespace cannot be found. I do have .NET Framework.
I have turned absolutely nothing up on the internet regarding this problem. Does anyone have any ideas?
EDIT: I just discovered that when I open the file outside of the context of a Silverlight project, it is able to use XDocument. It is only when I open the entire project that my problem occurs
Here is some sample code showing the problem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq; //Error 1 (See below)
namespace LastfmAmazon
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
public void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XDocument doc = XDocument.Parse(e.Result); //Error 2: see below
}
public void Button_Click(object sender, RoutedEventArgs e)
{
if (uname.Text != String.Empty)
{
App app = (App)Application.Current;
app.UserName = uname.Text;
String getTopArtists = "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=" + app.UserName + "&api_key=d2d620af554a60f228faed8d502c4936";
uname.Text = "Try Another One!";
WebClient web = new WebClient();
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted);
client.DownloadStringAsync(new Uri(getTopArtists));
}
}
}
}
Error 1: This line contains the following error: The type or namespace name 'Linq' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)
Error 2: This line contains the following error: The type or namespace name 'XDocument' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)
EDIT 2: Once I Googled what it meant to "add a reference" to a library, Anthony's answer solved the problem.
By default a Silverlight project will contain the System.Xml dll however XDcoument is contained in the System.Xml.Linq dll, this you will have to add to your project.
Make sure you add a reference to the appropriate XML library
For XMLDocument, XMLReader, etc ...: System.Xml.Dll
For XDocument, XNode, etc ...: System.Xml.Linq.dll