Unity3d calling VC and CSharp DLLs

Source: Internet
Author: User

1.c# encapsulated DLLs

Now create a class in VS, just write a simple static class and static method can be, as follows:

namespace pnsd{    publicstaticclass  pn    {        public   Staticstring getName (string  name)        {            return  name;        }    }}

Then compile it into a DLL file named Pnsd.dll.

2. Using custom C # encapsulated DLL components in Unity  

Create a Plugins folder in unity, and all externally referenced DLL components must be placed under this file to be used

 using   Unityengine;  using   System.Collections;  using  pnsd; //  Not the name of the DLL file, but the name of the namespace  public  class   Test:monobehaviour { void   Start () { string  name = Pn.getname ( "PNSD  ); //  class name. Method Name  Debug.Log (name); //  output result   

3. First build a DLL project with VC

4. Then create a testunity.h file inside. The contents are as follows

extern " C " int // defining the Out -of-the-way interface

Save, OK, at the establishment of a testunity.cpp, the code is as follows:

#include "testunity.h" int testunity () {   return 0;//This is a function that can write any function you want to implement}

Then compile, build. The Testunity.dll file is generated.

Then put this file in your Unity project's assert plugins (if you don't have this file, you'll have to create it)

Then create a new C # script file Dlltest inside unity. The code is as follows

usingUnityengine;usingSystem.Collections;usingSystem.Runtime.InteropServices; Public classDlltest:monobehaviour
{[DllImport ("testunity")] Private Static extern inttestunity (); //Use this for initialization intI=testunity (); voidStart () {print (i); } //Update is called once per frame voidUpdate () {}}

Then drag the file into the camera inside unity and it's OK.

Then run, you can achieve the effect of ha.

If it is a C # encapsulated DLL, it is referenced by using, and if it is a C + + DLL, dllimport[". dll" to add a reference to the DLL

Unity3d calling VC and CSharp DLLs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.