Hi, guys,
this is my first post here. But i have been working on unity3d for a while.
The reason why How to create was capitalized is because i want to emphasize that i am not asking how to use it.
I am asking how to create them.
What i am trying to do currently is that i want to create my own plugins.
i downloaded almost every example projects, searched forum and google for 2 weeks.
most people’s answers are " create your own", “use c#/c/c++ to create”
i mean thank you. but they are not helpful at all.
so, can anyone please share some experience about how to creating plugins?
do you just create a class library in c#?(assuming you are working on windows)
if so, how did you get “.bundle” folder?
or do you use .bundle folde at all?
and if you have time. would you please share the steps that how did you create/generate them in visual studio?
Thank you so much.
With C# you would actually not create whats normally called “plugin”, as its just additional .NET functionality. They though, if you want to create them, can easily be created with MonoDevelop or Visual C#, in both cases you would create a class library project and compile that later on which yields in a managed dll that will work on windows and osx without any further work
“Real plugins” normally yield in an unmanaged DLL on Windows, its counterpart on OSX is the .bundle (or .so which are in there).
These bundles are created by XCode or GCC if you don’t want to use XCode for whatever strange reason.
The unmanaged DLLs on windows can be created with different tools as there is far less of a focus, most resources are normally found for Visual C++ and Visual Studio though, where you just create an unmanaged dynamic library there as project and then expanding them.
The requirements are on both sides (windows and osx) equal: your functions you expose are within an extern “C” block and on the Unity end you have a corresponding class present in the plugins folder that declares the DLLImports with the right function names and corresponding attributes for the data types if they arent numeric or strings.
To find out more about it, search on C# and Marshaling or Interfacing with unmanaged libraries / code or PInvoke, this should yield several dozen tutorials and a few hundred thousand hits on google
most holds for unity too, the only thing thats different is that unity expects you to specify the dll without the .dll extension part, it will add this on its own and set it to dll / bundle depending on if its windows or osx you create your Unity Pro standalone build for
To safe you one question later on too: Unity does not allow unsafe code, so you really need to explicitely declare the stuff like this.
qiaoyifu, thank you for asking this question which I am also keen to learn, and thank you dreamora for very informative starting information.
We can easily create a library project which produces a dll and a pdb file.
Attached zip file contains a library project in Mono. Here is the class code;
using System;
namespace Test01
{
public class MyClass
{
public MyClass ()
{
}
public int [B]Add[/B] (int a, int b) {
return a+b;
}
}
}
What is the next step to be able to use this class in Unity? i.e How can I access from my Unity script to the Add function in the Test01.dll?
As this is a .NET assembly, all you have to do is dropping the dll into the unity projects and then you can use it. (if you are not importing the namespace with using Test01, the full class name is Test01.MyClass for declaration and instantiation)
For easyness, you might want to put it into the Plugins folder within your project (needs to be created) as thats the first thing compiled.
The only requirement .NET assemblies have is that they are compatible iwth C# 2.0 (which normally holds true for any assembly thats targeting .NET 2.0 up to .NET 3.5)
Unity has complained with the following error message;
“Assets/MyAssets/Scripts/XXXX.js(27,34): BCE0020: An instance of type ‘Test01.MyClass’ is required to access non static member ‘Add’.”
It worked after following mods; I changed the function decleration from this;
public int Add (int a, int b)
to this
public [B]__static__[/B] int Add (int a, int b)
I do not know what is the difference between “static” and non-static version of the function decleration.
Does the function has to be declared as “static” ?
For a given dll from someone else, is it possible to find out what class and member functions it provides and what are the parameters and return types?
Is it possible do convert dll to its original source code?
Static are class functions so to access them you only need the class.
if you don’t make them static they are instance functions, in this case the code would look like
Test01.MyClass myObject = new Test01.MyClass();
myObject.Add(1,2);
For a functionality like the one there, where you have a utility function that only needs and wants a and b to work, making it static makes very well sense.
Use monodevelop or visual studio as coding IDE, their autocompletition will show you all available functions, input and output data
Not through conversion. But if not obfuscated, it can be gotten out through reflection.
thank you, dreamora for the precious information.
i can not say that i fully digested what you said.
still, that was quite informative and helpful.
i will look up the page you linked.
and thank you tamer for sharing your example.
I was looking for information for that part , too.
One thing that I would not understand is your anser “2.”;
What I have asked;
which is if we have a dll __from someone els__e i.e. we don’t have its source code, but, only have the dll (nothing else), then how can we find which classes, functions, parameters, return types that dll provides?
Is it still possible to use “…monodevelop or visual studio…” some how to find out what the dll (from third parties) exports?
The answer on 2 was exactly in relation to this
.NET assemblies always contain at least the declarations so you can always see all publically exposed classes and the publically exposed variables as well as all functions they expose for you to work with, including the return and input values for them.
These data will show up in the autocomplete on their own and can also be checked in the reference inspector if you want to look at the whole stuff.
All thats required is that you drop the assembly into your unity project and potentially resync the solution file, then its present as reference and will just work as if it was a standard .NET framework or unity engine / unity editor framework, no difference there at all, its “just another framework that happens to be there” (part of the beauty of .NET)
Ok,
As a novice user, there are many things that I need to digest;
I’ve copied (drag-drop) the Test01.dll (a .NET assembly) in to my Plugins directory in my Unity project.
When I click on the name Test01 under Plugins directory I don’t see anything on the Inspector.
As I know that Test01.dll has a class MyClass and the class has Add function with two int arguments and returning an int, I can write the following; and it works. Debug.Log(Test01.MyClass.Add(10,123));
Assume that I do not have the source code of the Test01.dll (a .NET assembly), therefore, I do not know that Test01.dll has a class MyClass and the class has Add function with two int arguments and returning an int.
Where I can see the list of classes and functions provided by the Test01.dll?
I was able to see the source in far fewer steps.
I did a release build, then in the open solution, right click on the Solution Name (Solution Test01), choose Add → Add Files, Browse to the built .dll, once that is imported, double click on it, and the Assembly Browser opens, and you can drill down to the source directly in the current solution.
Now what I would love for someone to write up is an idiots guide to setting up the MonoDevelop project so that we can build plugins correctly for Unity.
I like the ability of setting it up in MonoDevelop, and not needing any external tools to do this.
I haven’t done any research into the setup process in MonoDevelop, so if it really is dead simple, just point me to the docs for it and i will sus it out myself.
i hope someone make a tutorial about creating plugins for Unity3D . In my resarch,there are 2 types of plugins. firstly, we may create it as a DLL , secondly, we create it by MonoDevelopment and we put it inside “Editor” folder.