简介
在热更工程中使用Json序列化和反序列化和在Unity中差不多,只是需要在Unity中进行热更新Json注册就行。
使用流程
- 和之前文章一样初始化appdomain
AppDomain appdomain;
System.IO.MemoryStream fs;
System.IO.MemoryStream p;
void Start()
{
StartCoroutine(LoadHotFixAssembly());
}
IEnumerator LoadHotFixAssembly()
{
appdomain = new ILRuntime.Runtime.Enviorment.AppDomain();
#if UNITY_ANDROID
WWW www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.dll");
#else
WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.dll");
#endif
while (!www.isDone)
yield return null;
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] dll = www.bytes;
www.Dispose();
#if UNITY_ANDROID
www = new WWW(Application.streamingAssetsPath + "/HotFix_Project.pdb");
#else
www = new WWW("file:///" + Application.streamingAssetsPath + "/HotFix_Project.pdb");
#endif
while (!www.isDone)
yield return null;
if (!string.IsNullOrEmpty(www.error))
UnityEngine.Debug.LogError(www.error);
byte[] pdb = www.bytes;
fs = new MemoryStream(dll);
p = new MemoryStream(pdb);
try
{
appdomain.LoadAssembly(fs, p, new ILRuntime.Mono.Cecil.Pdb.PdbReaderProvider());
}
catch
{
Debug.LogError("加载热更DLL失败,请确保已经通过VS打开Assets/Samples/ILRuntime/1.6/Demo/HotFix_Project/HotFix_Project.sln编译过热更DLL");
}
#if DEBUG && (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)
appdomain.UnityMainThreadID = System.Threading.Thread.CurrentThread.ManagedThreadId;
#endif
InitializeILRuntime();
OnHotFixLoaded();
}
- Unity项目InitializeILRuntime方法中注册LitJson
void InitializeILRuntime()
{
LitJson.JsonMapper.RegisterILRuntimeCLRRedirection(appdomain);
}
- Unity项目中调用热更工程中Json序列化方法,类中和常规C#工程基本一样,没有特殊做法
void OnHotFixLoaded()
{
appdomain.Invoke("HotFix_Project.TestJson", "RunTest", null, null);
}
- 热更工程中的Json序列化测试类
public class TestJson
{
enum JsonTestEnum
{
Test1,
Test2,
Test3,
}
class JsonTestClass
{
public int IntProp { get; set; }
public string StringProp { get; set; }
public JsonTestEnum EnumProp { get; set; }
public JsonTestSubClass SubClassProp { get; set; }
public Dictionary<string, JsonTestSubClass> DicTest { get; set; }
public Dictionary<string, int> DicTest2 { get; set; }
}
class JsonTestSubClass
{
public long LongProp { get; set; }
public List<JsonTestSubClass> SubClassList { get; set; }
public JsonTestSubClass[] ArrayProp { get; set; }
}
public static void RunTest()
{
JsonTestClass cls = new JsonTestClass();
cls.IntProp = 1;
cls.StringProp = "2";
cls.EnumProp = JsonTestEnum.Test3;
var sub = new JsonTestSubClass();
sub.LongProp = 4;
var sub2 = new JsonTestSubClass();
sub2.LongProp = 5;
var sub3 = new JsonTestSubClass();
sub3.LongProp = 6;
cls.SubClassProp = sub;
sub.ArrayProp = new JsonTestSubClass[2];
sub.ArrayProp[0] = sub2;
sub.ArrayProp[1] = sub3;
sub.SubClassList = new List<JsonTestSubClass>();
sub.SubClassList.Add(sub2);
sub.SubClassList.Add(sub3);
cls.DicTest = new Dictionary<string, JsonTestSubClass>();
cls.DicTest["11111"] = sub;
cls.DicTest2 = new Dictionary<string, int>();
cls.DicTest2["111222"] = 333444;
var str = JsonMapper.ToJson(cls);
Debug.Log("---------------");
Debug.Log(str);
Debug.Log("---------------");
var cls2 = JsonMapper.ToObject<JsonTestClass>(str);
Debug.Log(cls2.SubClassProp.ArrayProp[0].LongProp);
Debug.Log(cls2.SubClassProp.ArrayProp[1].LongProp);
Debug.Log(cls2.SubClassProp.SubClassList[0].LongProp);
Debug.Log(cls2.SubClassProp.SubClassList[1].LongProp);
Debug.Log(cls2.DicTest["11111"].LongProp);
Debug.Log(cls2.DicTest2["111222"]);
}
}
|