天天看點

C++ plugin for Unity 接口資料傳輸問題

先了解 c++ 部分的代碼是托管模式還是非托管模式,當然托管模式就沒必要浪費時間去整這個C++ dll 了(除非項目有需要),我們這裡隻說非托管接口部分的一小内容。

1.接口定義

a):有return 傳回值的 

比如:int UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API  GetChildNodeCount(FbxNode* pNode);

b):沒有return傳回值,指派到入口指針

比如:void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API  GetPolygonVertexs(FbxMesh* pMesh, int* Indexs);

2.傳輸 結構體

比如:傳輸一個坐标值

a):單個值指針傳輸帶 ref

c++:

class Vector3{public:

float x, y, z;

Vector3() {}

Vector3(float ix, float iy, float iz) {this->x = ix;this->y = iy;this->z = iz;}

~Vector3() {}

};

void UNITY_INTERFACE_EXPORT   UNITY_INTERFACE_API  GetPosition(FbxNode* pNode, Vector3* value);

c#:

  [StructLayout(LayoutKind.Sequential)]

    public struct Vector3

    {

        public float x, y, z;

        public Vector3(float x, float y, float z)

        {

            this.x = x;

            this.y = y;

            this.z = z;

        }

    }

        [DllImport(DllName, EntryPoint = "GetPosition", CallingConvention = CallingConvention.Cdecl)]

        public static extern void GetPosition(IntPtr pNode, ref Vector3 value);

b):多值(數組)指針傳輸不帶 ref ,直接new等大的數組類型并放入就能取回值了

C++:

比如:取結構體數組

void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API  GetControlPoints(FbxMesh* pMesh, Vector3 * value);

C#:

 [DllImport(DllName, EntryPoint = "GetControlPoints", CallingConvention = CallingConvention.Cdecl)]

        public static extern void GetControlPoints(IntPtr  pMesh, Vector3[] value);

 Vector3[] ControlPoints = new Vector3[ControlPointCount];

  NativePlugins.GetControlPoints(pMesh, ControlPoints);

繼續閱讀