Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created September 30, 2012 07:21
Show Gist options
  • Save AngryAnt/3806151 to your computer and use it in GitHub Desktop.
Save AngryAnt/3806151 to your computer and use it in GitHub Desktop.
Example code for "Downloading the hydra" blog post on AngryAnt.com
mcs -target:library -out:MyAssembly.dll -r:/Applications/Unity/Unity.app/Contents/Frameworks/UnityEngine.dll MyAssembly.cs
using UnityEngine;
public class MyClass
{
public static string myString = "This is my string from my class in my assembly";
public int LogMyString ()
{
Debug.Log (myString);
return 2 + 2;
}
}
using UnityEngine;
using System.Collections;
using System.Reflection;
public class NewBehaviourScript : MonoBehaviour
{
private string m_MessageString = "Waiting for assembly";
void OnAssemblyLoaded (WWWAssembly loadedAssembly)
{
m_MessageString = "Assembly " + loadedAssembly.URL + "\n";
System.Type type = loadedAssembly.Assembly.GetType ("MyClass");
FieldInfo field = type.GetField ("myString");
m_MessageString += (field.GetValue (null) as string) + "\n";
object instance = loadedAssembly.Assembly.CreateInstance ("MyClass");
MethodInfo method = type.GetMethod ("LogMyString");
m_MessageString += "Return value: " + method.Invoke (instance, null).ToString ();
}
void OnAssemblyLoadFailed (string url)
{
m_MessageString = "Failed to load assembly at " + url;
}
void OnGUI ()
{
GUILayout.BeginArea (new Rect (0.0f, 0.0f, Screen.width, Screen.height));
GUILayout.FlexibleSpace ();
GUILayout.BeginHorizontal ();
GUILayout.FlexibleSpace ();
GUILayout.Box (m_MessageString);
GUILayout.FlexibleSpace ();
GUILayout.EndHorizontal ();
GUILayout.FlexibleSpace ();
GUILayout.EndArea ();
}
}
using UnityEngine;
using System.Collections;
using System.Reflection;
public class WWWAssemblyLoader : MonoBehaviour
{
public string m_AssemblyURL;
private string m_ErrorString = "";
private WWW m_WWW;
private bool m_Complete = true;
public void Start ()
{
if (m_AssemblyURL != "")
{
ReloadAssembly (m_AssemblyURL);
}
}
public string AssemblyURL
{
get
{
return m_AssemblyURL;
}
set
{
if (m_AssemblyURL != value)
{
ReloadAssembly (value);
}
}
}
public float Progress
{
get
{
return m_Complete ? 1.0f : m_WWW.progress;
}
}
public string Error
{
get
{
return m_ErrorString;
}
}
public void ReloadAssembly (string url)
{
m_Complete = false;
m_ErrorString = "";
m_AssemblyURL = url;
m_WWW = new WWW (m_AssemblyURL);
}
public void Update ()
{
if (!m_Complete)
{
if (m_WWW.error != null)
{
m_ErrorString = m_WWW.error;
m_Complete = true;
SendMessage ("OnAssemblyLoadFailed", m_AssemblyURL);
}
else if (m_WWW.isDone)
{
Assembly assembly = LoadAssembly ();
m_Complete = true;
if (assembly != null)
{
Debug.Log ("Done");
SendMessage ("OnAssemblyLoaded", new WWWAssembly (m_AssemblyURL, assembly));
}
else
{
Debug.Log ("Failed");
SendMessage ("OnAssemblyLoadFailed", m_AssemblyURL);
}
}
}
}
private Assembly LoadAssembly ()
{
try
{
return Assembly.Load (m_WWW.bytes);
}
catch (System.Exception e)
{
m_ErrorString = e.ToString ();
return null;
}
}
}
public class WWWAssembly
{
private string m_URL;
private Assembly m_Assembly;
public string URL
{
get
{
return m_URL;
}
}
public Assembly Assembly
{
get
{
return m_Assembly;
}
}
public WWWAssembly (string url, Assembly assembly)
{
m_URL = url;
m_Assembly = assembly;
}
}
@Rinzler13
Copy link

Can you update this gist to work with unity 4?

@truongson-devgame
Copy link

Hello i using your tutorial. with AssemblyURL = https://www.dropbox.com/s/uucth632rb809p4/MyAssemble.dll?dl=0.
But not run. see again, please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment