1, c++生成dll和之前的一样。
unity调用dll打开双目
TestUnity.dll生成
Test.cpp
//F:\UnityBuffer\dllDebug\Assets\Plugins
#include <iostream>
extern "C" __declspec(dllexport)
int calAdd(int a, int b)
{
int c = a + b;
return c;
}
extern "C" __declspec(dllexport)
double calSubtract(double a, double b)
{
double result = a - b;
return result;
}
extern "C" __declspec(dllexport)
double calMult(double a, double b)
{
return a*b;
}
extern "C" void __declspec(dllexport) myPoem(char* poetry1)
{
char poet[] = "\t\t三月七日,沙湖道中遇雨。 \n\
雨具先去,同行皆狼狈, \n\
余独不觉,已而遂晴,故作此词。 \n\
莫听穿林打叶声,何妨吟啸且徐行。 \n\
竹杖芒鞋轻胜马,谁怕? \n\
一蓑烟雨任平生。 \n\
料峭春风吹酒醒,微冷,山头斜照却相迎。 \n\
回首向来萧瑟处,归去,也无风雨也无晴。 ";
char poet1[] = " What does not kill you makes you stronger!";
memcpy(poetry1, poet, sizeof(poet));//这儿只能是memcpy,不能=赋值,好像会出错
}
2 unity
dlltest.cs脚本挂在一个游戏对象上即可
[DllImport("D:\\Michael\\SC\\Testunity")] 这表示dll的全路径,下面脚本中是默认的路径,但有时候不对unity调用时会显示not found dll 应该是在untiy的哪儿可以设置
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;//用 c++中 dll 文件需要引入
public class DllTest : MonoBehaviour {
[DllImport("Testunity")]
static extern int calAdd(int x , int y);
[DllImport("Testunity")]
static extern double calSubtract (double a, double b);
[DllImport("Testunity")]
static extern double calMult (double a, double b);
[DllImport("Testunity")]
static extern void myPoem (byte[] poetry1);
int x = 10;
int y = 100;
private byte[] poetry1= new byte[300];
//public byte[] poetry1;
//public byte[] poetry2= new byte[5000];
//public char[] cs = new char[3000];
int length = 0;
string strs;
//打字机效果
private string text;
private float letterPause = 0.1f;
public AudioClip clip;
private AudioSource source;
private string word;
// Use this for initialization
void Start () {
Debug.Log(calAdd (x,y));
Debug.Log(calSubtract (x,y));
Debug.Log(calMult (x,y));
myPoem (poetry1);
//ASCIIEncoding encoding = new ASCIIEncoding ();
//string msg = encoding.GetString (poetry1);
//string msg = Encoding.ASCII.GetString (poetry1);
//output chinese chacters
string msg = System.Text.Encoding.Default.GetString (poetry1);
length = msg.Length;
strs = cutSubstring (msg, length);
print (strs);
source = GetComponent<AudioSource> ();
word = strs;
text = "";
StartCoroutine (TypeText ());
//print (str);
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUIStyle style = new GUIStyle ();
style.fontSize = 20;
//style.alignment = TextAnchor.UpperCenter;
style.normal.textColor = new Color (0,0,0);
style.name = "poem";
GUI.Label (new Rect(0,100,500,500),text,style);
}
private static string cutSubstring(string str, int length)
{
if (str == null || str.Length == 0 || length < 0)
{
return "";
}
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str);
int n = 0; // 表示当前的字节数
int i = 0; // 要截取的字节数
for (; i < bytes.GetLength(0) && n < length; i++)
{
// 偶数位置,如 0、 2、 4 等,为 UCS2 编码中两个字节的第一个字节
if (i % 2 == 0)
{
n++; // 在 UCS2 第一个字节时 n 加 1
}
else
{
// 当 UCS2 编码的第二个字节大于 0 时,该 UCS2 字符为汉字,一个汉
//字算两个字节
if (bytes[i] > 0)
{
n++;
}
}
}
// 如果 i 为奇数时,处理成偶数
if (i % 2 == 1)
{
// 该 UCS2 字符是汉字时,去掉这个截一半的汉字
if (bytes[i] > 0)
i = i - 1;
// 该 UCS2 字符是字母或数字,则保留该字符
else
i = i + 1;
}
return System.Text.Encoding.Unicode.GetString(bytes, 0, i);
}
//打字机效果
private IEnumerator TypeText()
{
foreach (var letter in word.ToCharArray())
{
text += letter;
if (clip) {
source.PlayOneShot(clip);
}
yield return new WaitForSeconds (letterPause);
}
}
}
在unity中调试vs的dll可放在对应工程的Assets下或者Assets/Plugins文件夹里(如果这个文件夹没有新建一个,其文件名可以不是plugins自己随便命名一个)或者和Assets同级目录都可以。测试都可运行vs2013x64, unity20170301x64
3,除unity调用dll打开双目中介绍的两种调试方法,下面介绍第三种。
A 在VS工程的属性窗口中做两个设置:
命令-------》放unity软件安装的exe的位置
工作目录-------》VS生成的dll在unity工程中放的位置(这个不用写也是可以的,如下面4和5所示)
B 然后在生成dll的vs工程中加断点,运行程序。
出现这个界面后,选择调用dll的unity工程。
点击运行
这样也可以调试vs生成的dll的源文件了。
4,一些细微的区别
A,如果unity工程已经是运行的,则可以如下就不用运行vs时重新打开unity.
然后运行unity,程序就过来了。
unity工程如果已经打开,附加这儿为:是,
则运行结果是:
5,如果unity工程是关着的,则应该是把附加设置为:否,这时运行vs则会像3一样自动打开unity.
打开调用dll的unity工程后运行unity,则
最后如果一切都调试完成,生成的dll可以用release版,应该运用的速度会比较快。