Unity3d 利用dll C#腳本反射

1、構建ClassLibrary工程,生成:ABTestImport.dll。將dll放到web項目目錄下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ABTest : MonoBehaviour 
{
	// Use this for initialization
	void Start () 
	{
            Debug.Log("Run to ABTest.");		
	}
}

2、將腳本掛到攝像機。

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

public class Load : MonoBehaviour
{
    // Use this for initialization
    IEnumerator Start()
    {
        yield return StartCoroutine(StartLoad());
        LoadScript();
    }

    Assembly assembly = null;
    private IEnumerator StartLoad()
    {
        //string url = "file://" + Application.streamingAssetsPath + "/ABTestImport.dll";
        string url = "http://localhost:8080/examples/servlets/StreamingAssets/ABTestImport.dll";

        WWW www = new WWW(url);

        yield return www;

        if (www.error != null)
        {
            Debug.Log("www error = " + www.error);
            yield return null;
        }
        else
        {
            assembly = Assembly.Load(www.bytes);
        }
    }

    private void LoadScript()
    {
        Debug.Log("LoadScript");
        if (assembly != null)
        {
            Type t = assembly.GetType("ABTest");
            GameObject go = new GameObject("ABTest");
            go.AddComponent(t);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章