VS C#生成dll,C#和unity工程调用

匿名 (未验证) 提交于 2019-12-03 00:25:02

1, C# 生成dll

a, vs新建C#类库工程生成dll,对应的cs文件。

DllSuccess.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; //using System.Threading.Tasks;  namespace DllSuccess {     public class Poem     {         public void PrintPoem()         {             Console.WriteLine("白日依山尽,黄河入海流!");         }          public static void PrintPoems()         {             Console.WriteLine("且将新火试新茶,诗酒趁年华。");         }          public static int UnityAdd(int a, int b)         {             int c = a + b;             return c;         }          public static void WritePoem(char[] strs_poem)         {             String str_poem = "人有悲欢离合,月有阴晴圆缺。此事古难全!";             char[] str = str_poem.ToCharArray();             int len = str_poem.Length;             // char[] strs_poem = new char[len];              Buffer.BlockCopy(str, 0, strs_poem, 0, len*2);         }     } }

b, 新建控制台应用程序工程

ActiaveDll.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DllSuccess;  namespace ActiaveDll {     class Program     {         static void Main(string[] args)         {             Poem poem = new Poem();             poem.PrintPoem();             Poem.PrintPoems();              char[] str = new char[20];             Poem.WritePoem(str);             Console.WriteLine(str);         }     } }

c,添加引用之前的dll.





通过把两个工程新建到同一个解决方案下,设置不同的启动项就可以。



2, Unity调用。

a, 把生成的dll放到unity工程的Assests下。




b, 新建工程,写脚本ActivateDll.cs

using System.Collections; using System.Collections.Generic; using UnityEngine; using DllSuccess;  public class ActivateDll : MonoBehaviour { 	 	int x = 10;   	int y = 100;    	int length = 0;   	string strs;   	//打字机效果   	private string text;   	private float letterPause = 0.1f;    	//添加背景音乐 	public AudioClip clip;   	private AudioSource source;   	private string word;    	private char[] vs_poem = new char[20]; 	// Use this for initialization   	void Start () { 		 		Debug.Log (Poem.UnityAdd (x, y)); 		Poem.WritePoem (vs_poem); 		string msg = new string (vs_poem); 		print (msg);  	 		//output chinese chacters   		length = msg.Length;   		strs = cutSubstring (msg, length*2);   		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);   		}   	} }

c, 附脚本到游戏对象。


d, 运行结果。


3,用unity调试。

a, 这儿添加unity安装目录的exe.这时调用dll的unity工程要关着。

这样的调试c#工程关闭,unity就自动关了。



但不知为何没有在断点处停留?

b,unity工程生成对应的exe.


c, C#工程的设置




但不知为何没有在断点处停止?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!