当前位置: 首页 > news >正文

东营智能网站设计展示型商城订单网站建设

东营智能网站设计,展示型商城订单网站建设,wordpress title插件,河南经天路桥建设总公司网站以下内容是根据Unity 2020.1.0f1版本进行编写的   目前游戏开发厂商主流还是使用lua框架来进行热更#xff0c;如xlua#xff0c;tolua等#xff0c;也有的小游戏是直接整包更新#xff0c;这种小游戏的包体很小#xff0c;代码是用C#写的#xff1b;还有的游戏就是通过…以下内容是根据Unity 2020.1.0f1版本进行编写的   目前游戏开发厂商主流还是使用lua框架来进行热更如xluatolua等也有的小游戏是直接整包更新这种小游戏的包体很小代码是用C#写的还有的游戏就是通过热更C#代码来实现热更新的。本篇就来学习一下。   1、热更C#代码的方法 AI时代遇事不决先问AI下面就是百度问AI的答案 可以看到AI给出的答案大部分都是将C#代码编译成dll然后在需要时动态加载对应的dll来实现代码的热更新的。下面就来尝试一下。   2、使用ILRuntime框架 ILRuntime官方文档https://ourpalm.github.io/ILRuntime/public/v1/guide/tutorial.html “scopedRegistries”: [ { “name”: “ILRuntime”, “url”: “https://registry.npmjs.org”, “scopes”: [ “com.ourpalm” ] } ], 如上图一新建一个unity项目然后在工程目录Packages下的manifest文件中增加图一框住的代码保存。然后关闭Unity项目再重新打开。点击菜单栏Window-Package Manager打开PackageManager窗口切换Packages切换到My Registries可以看到刚刚加上去的ILRuntime包如图二。 选中后点击右下角箭头所指的install按钮就可以导入包体了这里因为我已经导入过了所以显示的按钮是Remove。这个包体还有示例Demo有需要也可以一并导入到工程中。 导入的Demo用的是unsafe代码导入后可能会有很多报错点击菜单栏Edit-Project Settings打开ProjectSettings窗口设置player页签中的OtherSettings使工程允许使用unsafe代码。 接着尝试运行Demo直接运行会报错需要生成dll。 先用VisualStudio打开一次项目工程的sln文件再打开下载的Demo包内工程的sln文件如上图。 在打开的HotFix_Project中点击菜单栏生成-生成解决方案按钮等待VS左下角出现生成成功提示。 此时回到Unity随便运行一个Examples场景都不会有报错了。 接下来简单尝试一下先做一个简单的界面如上图功能是点击下方左右两个按钮点击哪边的按钮就在中间的文本显示点击了哪边的按钮。 using UnityEngine; using System.Collections; using System.IO; using System;public class AppCommon : MonoBehaviour {static AppCommon instance;System.IO.MemoryStream fs;System.IO.MemoryStream p;public bool isLoaded false;public static AppCommon Instance{get { return instance; }}//AppDomain是ILRuntime的入口最好是在一个单例类中保存整个游戏全局就一个这里为了示例方便每个例子里面都单独做了一个//大家在正式项目中请全局只创建一个AppDomainpublic ILRuntime.Runtime.Enviorment.AppDomain appdomain;//在awake方法中先加载好appdomainvoid Awake(){instance this;StartCoroutine(LoadHotFixAssembly());}IEnumerator LoadHotFixAssembly(){//首先实例化ILRuntime的AppDomainAppDomain是一个应用程序域每个AppDomain都是一个独立的沙盒appdomain new ILRuntime.Runtime.Enviorment.AppDomain();//正常项目中应该是自行从其他地方下载dll或者打包在AssetBundle中读取平时开发以及为了演示方便直接从StreammingAssets中读取//正式发布的时候需要大家自行从其他地方读取dll//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//这个DLL文件是直接编译HotFix_Project.sln生成的已经在项目中设置好输出目录为StreamingAssets在VS里直接编译即可生成到对应目录无需手动拷贝 #if UNITY_ANDROIDWWW www new WWW(Application.streamingAssetsPath /HotFix_Project.dll); #elseWWW www new WWW(file:/// Application.streamingAssetsPath /HotFix_Project.dll); #endifwhile (!www.isDone)yield return null;if (!string.IsNullOrEmpty(www.error))UnityEngine.Debug.LogError(www.error);byte[] dll www.bytes;www.Dispose();//PDB文件是调试数据库如需要在日志中显示报错的行号则必须提供PDB文件不过由于会额外耗用内存正式发布时请将PDB去掉下面LoadAssembly的时候pdb传null即可 #if UNITY_ANDROIDwww new WWW(Application.streamingAssetsPath /HotFix_Project.pdb); #elsewww new WWW(file:/// Application.streamingAssetsPath /HotFix_Project.pdb); #endifwhile (!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);}InitializeILRuntime();OnHotFixLoaded();}private void OnDestroy(){fs.Close();p.Close();}unsafe void InitializeILRuntime(){ #if DEBUG (UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE)//由于Unity的Profiler接口只允许在主线程使用为了避免出异常需要告诉ILRuntime主线程的线程ID才能正确将函数运行耗时报告给Profilerappdomain.UnityMainThreadID System.Threading.Thread.CurrentThread.ManagedThreadId; #endif//这里做一些ILRuntime的注册appdomain.RegisterCrossBindingAdaptor(new MonoBehaviourAdapter());appdomain.RegisterValueTypeBinder(typeof(Vector3), new Vector3Binder());appdomain.DelegateManager.RegisterDelegateConvertorUnityEngine.Events.UnityAction((act) {return new UnityEngine.Events.UnityAction(() {((Action)act)();});});ILRuntime.Runtime.Generated.CLRBindings.Initialize(appdomain);}unsafe void OnHotFixLoaded(){isLoaded true;Debug.Log(AppDomain Loaded);} }首先在Unity中新建一个名叫AppCommon的脚本用于定义一些项目内通用的单例类。这里主要是定义一个叫appdomain的类在Demo中是建议全局只创建一个的。 每个Demo都会有加载这个appdpmain的方法将代码复制到AppCommon类中在其InitializeILRuntime方法中注册好全部需要用到的事件等。 using UnityEngine; using System.Collections; using System.Collections.Generic; using ILRuntime.CLR.TypeSystem; using ILRuntime.CLR.Method; using ILRuntime.Runtime.Intepreter; using ILRuntime.Runtime.Stack;public class MyView1 : MonoBehaviour {static MyView1 instance;public static MyView1 Instance{get { return instance; }}void Start(){instance this;StartCoroutine(LoadHotFixAssembly());}IEnumerator LoadHotFixAssembly(){while(!AppCommon.Instance.isLoaded){yield return 0;}OnHotFixLoaded();}void OnHotFixLoaded(){SetupCLRRedirection();SetupCLRRedirection2();AppCommon.Instance.appdomain.Invoke(HotFix_Project.TestMyView1, ShowView, null, gameObject);}unsafe void SetupCLRRedirection(){//这里面的通常应该写在InitializeILRuntime这里为了演示写这里var arr typeof(GameObject).GetMethods();foreach (var i in arr){if (i.Name AddComponent i.GetGenericArguments().Length 1){AppCommon.Instance.appdomain.RegisterCLRMethodRedirection(i, AddComponent);}}}unsafe void SetupCLRRedirection2(){//这里面的通常应该写在InitializeILRuntime这里为了演示写这里var arr typeof(GameObject).GetMethods();foreach (var i in arr){if (i.Name GetComponent i.GetGenericArguments().Length 1){AppCommon.Instance.appdomain.RegisterCLRMethodRedirection(i, GetComponent);}}}unsafe static StackObject* AddComponent(ILIntepreter __intp, StackObject* __esp, IListobject __mStack, CLRMethod __method, bool isNewObj){//CLR重定向的说明请看相关文档和教程这里不多做解释ILRuntime.Runtime.Enviorment.AppDomain __domain __intp.AppDomain;var ptr __esp - 1;//成员方法的第一个参数为thisGameObject instance StackObject.ToObject(ptr, __domain, __mStack) as GameObject;if (instance null)throw new System.NullReferenceException();__intp.Free(ptr);var genericArgument __method.GenericArguments;//AddComponent应该有且只有1个泛型参数if (genericArgument ! null genericArgument.Length 1){var type genericArgument[0];object res;if (type is CLRType){//Unity主工程的类不需要任何特殊处理直接调用Unity接口res instance.AddComponent(type.TypeForCLR);}else{//热更DLL内的类型比较麻烦。首先我们得自己手动创建实例var ilInstance new ILTypeInstance(type as ILType, false);//手动创建实例是因为默认方式会new MonoBehaviour这在Unity里不允许//接下来创建Adapter实例var clrInstance instance.AddComponentMonoBehaviourAdapter.Adaptor();//unity创建的实例并没有热更DLL里面的实例所以需要手动赋值clrInstance.ILInstance ilInstance;clrInstance.AppDomain __domain;//这个实例默认创建的CLRInstance不是通过AddComponent出来的有效实例所以得手动替换ilInstance.CLRInstance clrInstance;res clrInstance.ILInstance;//交给ILRuntime的实例应该为ILInstanceclrInstance.Awake();//因为Unity调用这个方法时还没准备好所以这里补调一次}return ILIntepreter.PushObject(ptr, __mStack, res);}return __esp;}unsafe static StackObject* GetComponent(ILIntepreter __intp, StackObject* __esp, IListobject __mStack, CLRMethod __method, bool isNewObj){//CLR重定向的说明请看相关文档和教程这里不多做解释ILRuntime.Runtime.Enviorment.AppDomain __domain __intp.AppDomain;var ptr __esp - 1;//成员方法的第一个参数为thisGameObject instance StackObject.ToObject(ptr, __domain, __mStack) as GameObject;if (instance null)throw new System.NullReferenceException();__intp.Free(ptr);var genericArgument __method.GenericArguments;//AddComponent应该有且只有1个泛型参数if (genericArgument ! null genericArgument.Length 1){var type genericArgument[0];object res null;if (type is CLRType){//Unity主工程的类不需要任何特殊处理直接调用Unity接口res instance.GetComponent(type.TypeForCLR);}else{//因为所有DLL里面的MonoBehaviour实际都是这个Component所以我们只能全取出来遍历查找var clrInstances instance.GetComponentsMonoBehaviourAdapter.Adaptor();for (int i 0; i clrInstances.Length; i){var clrInstance clrInstances[i];if (clrInstance.ILInstance ! null)//ILInstance为null, 表示是无效的MonoBehaviour要略过{if (clrInstance.ILInstance.Type type){res clrInstance.ILInstance;//交给ILRuntime的实例应该为ILInstancebreak;}}}}return ILIntepreter.PushObject(ptr, __mStack, res);}return __esp;} }然后还是在Unity中新建一个名叫MyView1的脚本这个脚本的功能其实只是等待上述的appdomain类加载完之后然后调用加载的dll内部的类以及方法即可。实际逻辑是写到HotFix_Project里的也只有HotFix_Project里的代码能热更。 这里写的比较简单大部分代码是抄MonoBehaviourDemo的实际上就是需要使appdomain注册自定义实现AddComponent方法和GetComponent方法。这样在热更的代码中就可以通过AddComponent方法来把C#代码以组件的形式挂载到对应的GameObject中。 最后就是热更代码部分。 因为要用到Unity UI部分的方法所以需要将UnityEngine.UI的dll复制过来并加入到HotFix_Project的引用中。先把dll复制到UnityDlls目录下然后再在VS上右键添加引用在打开的窗口中点击右下角浏览然后选择复制的dll文件即可 using UnityEngine; using UnityEngine.UI;namespace HotFix_Project {class TestMyView1 : MonoBehaviour{private Button btn1;private Button btn2;private Text text;void Start(){btn1 gameObject.transform.Find(btn1).GetComponentButton();btn2 gameObject.transform.Find(btn2).GetComponentButton();btn1.onClick.AddListener(OnClickBtn1);btn2.onClick.AddListener(OnClickBtn2);text gameObject.transform.Find(Text).GetComponentText();}void OnClickBtn1(){text.text 点击了左边的按钮;}void OnClickBtn2(){text.text 点击了右边的按钮;}public static void ShowView(GameObject go){go.AddComponentTestMyView1();}} }接着在HotFix_Project新建一个名叫TestMyView1的C#脚本实现上面描述的功能就可以了。这一部分代码就是可热更的。 写好代码后点击HotFix_Project菜单栏的生成-重新生成解决方案按钮即可运行Unity。 效果如下 最后如果需要实现热更就是在AppCommon加载appdomain的协程中修改一下加载的文件位置如上图框住的部分这里我没试。 所以实际上C#代码热更就是将代码编译成dll然后在加载后以反射调用或者委托等方式来调用写在dll内部的类和方法。因此每次热更只需要重新编译生成dll就可以了。 此外该框架还能生成一些CLR绑定的代码用于减少反射调用的消耗。实际上这里的做法有点类似于tolua框架 代码仓库地址https://gitee.com/chj–project/CSharpHotUpdate
http://www.w-s-a.com/news/429451/

相关文章:

  • 广州市车管所网站建设全国做网站公司前十名
  • 太原手手工网站建设公司视频直播服务
  • 雷达图 做图网站wordpress首页怎么美化
  • 四川做网站设计公司价格vip解析网站怎么做的
  • 网站建设流程域名申请做化工的 有那些网站
  • 软件开发设计流程图seo搜索引擎官网
  • 外国小孩和大人做网站东富龙科技股份有限公司
  • 上线倒计时单页网站模板做网站的资金来源
  • 泸州市建设厅网站中小企业网络需求分析
  • asp网站版权做网页价格
  • 长春网站建设路关键词优化公司哪家好
  • 河南省建设银行网站年报天津设计师网站
  • 沙洋网站定制如果自己建立网站
  • 凡科网站怎么做建站关键字搜索网站怎么做
  • 小说网站建站程序企业邮箱地址
  • 福州市住房和城乡建设网站网站开发方案论文
  • 在线教育网站开发网站推广常用方法包括
  • 东莞高端品牌网站建设软件开发模型及特点
  • 个人网站的设计与实现的主要内容网站开发公司架构
  • 浏览器收录网站什么是新媒体营销
  • 上海营销网站建设公司下面哪个不是网页制作工具
  • 有哪些网站可以做设计比赛苏州设计公司排名前十
  • 公益网站建设需求车陂手机网站开发
  • 高端网站建设专业营销团队宁德网站建设51yunsou
  • 网站如何做cdn购物网站建设app开发
  • 简单的手机网站模板好看大方的企业网站源码.net
  • 沈阳住房和城乡建设厅网站网站个人备案做论坛
  • 企业建网站的目的开家网站建设培训班
  • 做怎么网站网站优化和推广
  • 建站工具 风铃网站每年空间域名费用及维护费