- Home /
External Libraries in C# scripts
Just want to say first of all that I have a C/Objective-C programming background. I am new to Unity and C#.
That said, is there a way to #include or #import in unity? I cannot seem to figure out how to import classes from other files without assigning them to a game object (which seems a little complicated considering how easy unity makes other things). So, is there an import method, and how do I use it? If not how should I be using external code sources?
Answer by kmeboe · Sep 06, 2012 at 05:10 PM
Hey,
There is no "#include" directive in c#. Any .cs files in your asset folder are accessible to all other scripts; you don't need to attach them to a game object first. Note that unless a method in a class is declared "static" (like "alloc" in objective c), you will need to have an instance of that class before you can use it. For example:
public class TestClass
{
static void ThisMethodIsStatic()
{
// do stuff
}
void ThisMethodIsNonStatic()
{
// do other stuff
}
}
// inside a different script.....
void Update()
{
TestClass.ThisMethodIsStatic(); //okay
TestClass.ThisMethodIsNonStatic(); // Won't compile, instead use the below
TestClass instanceOfTestClass = new TestClass();
instanceOfTestClass.ThisMethodIsNonStatic();
}
In addition, I believe that any .net assemblies in your "Assets" folder will also be available for use, but I haven't tried this myself.
Does this help?
-Kevin
Absolutely! thank you very much. Can you help me with one more thing? What is the deal with first and second passes?
Okay, I have a confession...I'm a little new to Unity myself. :)
Are those passes a compilation thing? I haven't noticed them.
I think it has something to do with javascript vs c#, but I am not sure how it works. if you look in the project explorer in monodevelop, it shows 2 different sections for the passes.
Oh right...the "...firstpass.csproj" files. I'm not sure why Unity has those, but they are essentially "temporary" files that are used for the build. You can delete them, and the framework will recreate them. If you're using an SC$$anonymous$$ system (like Perforce), you don't need to add these files to the repository.
http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html That should probably answer that.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OpenFilePanel, but how? - SOLVED! -1 Answers
Question on UnityScript and C# 1 Answer