Unity DLL Hot update

Source: Internet
Author: User

The simplest case code, which needs to be used after preparation

Using System.Collections;
Using System.Collections.Generic;
Using System.Xml;
Using Unityengine;
Using System.Reflection;
Using System.IO;
Using System;
<summary>
Load Web Code
</summary>
public class Loadwebscript:monobehaviour {
Static Loadwebscript _instance;
public static Loadwebscript Instance {
Get
{
if (_instance! = null)
{
return _instance;
}
else {
Gameobject g = new Gameobject ("Loadweb");
return g.addcomponent<loadwebscript> ();
}
}

}


<summary>
Code Network Address
</summary>
public string NetPath {
get {
XmlDocument doc = new XmlDocument ();
Doc. Load (application.streamingassetspath+ "/webconfig/webconfig.xml");
XmlElement list= Doc. DocumentElement;
XmlElement ele = (XmlElement) list. selectSingleNode ("remote");
String path = Ele. GetAttribute ("path");
if (string. IsNullOrEmpty (Scriptspacename)) {
Scriptspacename = Ele. GetAttribute ("script");
}
return path;
}
}
private string Scriptspacename;
private string ScriptFullName {
get {
return Scriptspacename + ". dll";
}
}
private string Outscriptpath {
get {
return Application.streamingassetspath + "/outscript/";
}
}

private void Awake ()
{

Startcoroutine (Updatescript ());
}
IEnumerator Updatescript () {

Print (NetPath);
www www = new www (netpath+ "/" +scriptfullname);
yield return www;

if (www.error! = null)
{
Debug.logwarning (Www.error);
Load ();
}
Else
{

Startcoroutine (Savescript (www.bytes,Load));
}


}
IEnumerator savescript (byte[] B,action Act) {
FileStream fs = File.Open (Outscriptpath+scriptfullname, FileMode.Create);
Fs. Write (b, 0, b.length);
Yield return FS;
Fs. Close ();
Act. Invoke ();
}

private void Load ()
{

if (file.exists (Outscriptpath + scriptfullname))
{

Print (Outscriptpath + scriptfullname);
byte[] _byte = file.readallbytes (Outscriptpath + scriptfullname);
Print (_byte. Length);
Assembly-Assembly.Load (_byte);
Type t = ass.gettype (Scriptspacename + ". Outmanager ");
Print (Scriptspacename + ". Outmanager ");
Print (t);
MethodInfo m = T.getmethod ("Main");
M.invoke (null, NULL);
}
}
}

Unity DLL Hot update

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.