Unity 之 编辑器模拟手机大退重连工具类
发布日期:2021-06-29 19:10:01
浏览次数:2
分类:技术文章
本文共 2187 字,大约阅读时间需要 7 分钟。
分享一个曾经用过的在Unity Editor 上模拟小断线的重连的工具类(手机大退再回来),复制代码到你的工程中,然后挂载到任意物体;运行后,即可模拟断线重连了。
源码如下:
using UnityEditor;using UnityEngine;public class SwitchToBackground : MonoBehaviour{ public void sendApplicationPauseMessage(bool isPause) { Transform[] transList = GameObject.FindObjectsOfType(); for (int i = 0; i < transList.Length; i++) { Transform trans = transList[i]; //Note that messages will not be sent to inactive objects trans.SendMessage("OnApplicationPause", isPause, SendMessageOptions.DontRequireReceiver); } } public void sendApplicationFocusMessage(bool isFocus) { Transform[] transList = GameObject.FindObjectsOfType (); for (int i = 0; i < transList.Length; i++) { Transform trans = transList[i]; //Note that messages will not be sent to inactive objects trans.SendMessage("OnApplicationFocus", isFocus, SendMessageOptions.DontRequireReceiver); } } public void sendEnterBackgroundMessage() { sendApplicationPauseMessage(true); sendApplicationFocusMessage(false); } public void sendEnterFoegroundMessage() { sendApplicationFocusMessage(true); sendApplicationPauseMessage(false); }}[CustomEditor(typeof(SwitchToBackground))]public class simulateSwitchToBackgroundEditor : Editor{ void OnEnable() { } public override void OnInspectorGUI() { DrawDefaultInspector(); serializedObject.Update(); serializedObject.ApplyModifiedProperties();//now varibles in script have been updated if (GUILayout.Button("send enter background message")) { if (Application.isPlaying) { ((SwitchToBackground)target).sendEnterBackgroundMessage(); } } if (GUILayout.Button("send enter foeground message")) { if (Application.isPlaying) { ((SwitchToBackground)target).sendEnterFoegroundMessage(); } } }}
挂载带场景任意物体上即可。。。
PS:一个需要注意的点,打包时需要将代码全部注释,重新编译后重新尝试打包即可,否则会报错,转载地址:https://czhenya.blog.csdn.net/article/details/110188563 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月05日 22时51分43秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
新的开始
2019-04-30
dos下常用网络命令
2019-04-30
powerpc汇编指令分析
2019-04-30
欢迎使用CSDN-markdown编辑器
2019-04-30
文章标题
2019-04-30
算法-动态规划
2019-04-30
算法-3个水桶8升水
2019-04-30
算法-稳定匹配
2019-04-30
管道堵住问题的定位
2019-04-30
ssh问题
2019-04-30
struct union and endian
2019-04-30
a bug related with extern
2019-04-30
cbwfq移植的思考
2019-04-30
TCE1保序功能的开发
2019-04-30
TCE1保序功能开发-----续
2019-04-30
低温故障处理
2019-04-30
联想G455 XP/MAC 双系统安装
2019-04-30
学习cocoa编程-.Cocoa.Programming
2019-04-30
第3章 Object c
2019-04-30
第4章 memory management
2019-04-30