[Unity基础]unity调用dll文件以及反射加载dll

参考链接:

http://liweizhaolili.blog.163.com/blog/static/1623074420144313825921/

http://blog.csdn.net/janeky/article/details/25923151

http://m.blog.csdn.net/blog/ordinary0214/20935321


一、调用dll文件中的c#类

a.新建项目,选择类库,选择.NET Framework 2.0

b.

using System;
using System.Collections.Generic;
using System.Text;

public class CSharpDll
{
    public static int Add(int a, int b)
    {
        return a + b;
    }
}

c.菜单栏,生成 / 生成解决方案,生成的dll文件会出现在bin\Debug下,然后把它复制到Assets目录下

d.

using UnityEngine;
using System.Collections;

public class TestDll : MonoBehaviour {

	void Start () 
	{
        print(CSharpDll.Add(1, 2));
	}
}

二、调用dll文件中的MonoBehaviour类

a.新建项目,选择类库,选择.NET Framework 2.0

b.菜单栏,项目,添加引用,unity安装目录\Editor\Data\Managed,找到UnityEngine.dll直接添加(即项目/添加引用,脚本中使用的命名空间来自哪些dll,就引用哪些dll)

using UnityEngine;
using System.Collections;

public class MonoDll : MonoBehaviour {

    void Start()
    {
        print("Start");
    }

    void Update()
    {
        print("Update");
    }

    public void PrintInfo()
    {
        print("Hello");
    }
}

c.菜单栏,生成 / 生成解决方案,生成的dll文件会出现在bin\Debug下,然后把它复制到Assets目录下

d.

using UnityEngine;
using System.Collections;

public class TestMonoDll : MonoBehaviour {

	// Use this for initialization
	void Start () 
    {
        MonoDll m = gameObject.AddComponent<MonoDll>();
        m.PrintInfo();
	}

}


反射就是得到程序集中的属性和方法

a.将导入的.dll文件改后缀为.bytes

b.打包上面的文件,可以参考:http://blog.csdn.net/lyh916/article/details/46289407

c.

using UnityEngine;
using System.Collections;
using System.Reflection;
using System;

public class TestDll : MonoBehaviour {

    private static readonly string path = "file://" + Application.dataPath + "/StreamingAssets/" + "ALL.assetbundle";

    void Start()
    {
        StartCoroutine(loadDllScript());
    }

    IEnumerator loadDllScript()
    {
        WWW www = WWW.LoadFromCacheOrDownload(path, 10);
        yield return www;
        AssetBundle bundle = www.assetBundle;

        TextAsset asset = bundle.Load("ClassLibrary1", typeof(TextAsset)) as TextAsset;
        Assembly assembly = Assembly.Load(asset.bytes);
        Type t = assembly.GetType("CSharpDll");
        MethodInfo method = t.GetMethod("Add");
        System.Object[] p = new System.Object[] { 1, 2 };
        print(method.Invoke(t,p));

        TextAsset asset2 = bundle.Load("ClassLibrary2", typeof(TextAsset)) as TextAsset;
        Assembly assembly2 = Assembly.Load(asset2.bytes);
        Type t2 = assembly2.GetType("MonoDll");
        gameObject.AddComponent(t2);
    } 
}


/

dll反编译:

使用工具:Reflector

下载地址:http://pan.baidu.com/s/1kTJDyyb


dll调用:

在vs菜单栏,项目/添加引用


dll混淆:

http://liweizhaolili.blog.163.com/blog/static/1623074420145110502776/


dll热更新:

http://www.cnblogs.com/plateFace/p/4790437.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity调用DLL有几种常见的方法。以下是其中的两种方法: 1. 使用DllImport特性: - 将DLL文件放置在Unity项目的Assets/Plugins文件夹下(如果没有该文件夹,可以手动创建)。 - 在C#脚本中,使用[System.Runtime.InteropServices.DllImport]特性声明要调用DLL函数。例如: ```csharp using System.Runtime.InteropServices; public class MyScript : MonoBehaviour { [DllImport("mydll")] // 替换为实际的DLL名称 private static extern void MyDllFunction(); void Start() { MyDllFunction(); // 调用DLL函数 } } ``` 2. 使用Unity的插件系统: - 创建一个新的C#类,然后将其放置在Unity项目的Assets/Editor文件夹下。 - 在该类中,使用UnityEditor命名空间下的PluginImporter类来导入DLL并设置相关属性。例如: ```csharp using UnityEditor; public class MyDllImporter : AssetPostprocessor { private void OnPreprocessAssembly() { if (assetPath.Contains("mydll")) // 替换为实际的DLL名称 { PluginImporter pluginImporter = (PluginImporter)assetImporter; pluginImporter.SetCompatibleWithEditor(true); pluginImporter.SetCompatibleWithAnyPlatform(false); pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, true); // 替换为实际的目标平台 } } } ``` - 在Unity编辑器中,导入DLL文件,并确保在Inspector窗口中设置了正确的平台兼容性。 请注意,调用DLL可能涉及到特定的函数签名和参数传递方式,具体取决于DLL的实现。因此,请确保你了解DLL的使用方法和要求,并按照其提供的文档进行调用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值