ngui中 代码调用按钮事件

好烦人啊这个问题, 我弄完发上来

这个问题解决了一半 发现可以用 按钮绑定来解决这个问题,并且更安全方便快速

直接在按钮上添加一个 key binding 指定按键 搞定

不过看了一下源代码,好像这个binding实际上就是一个委托,哈哈,这样实际上两个问题都解决了@.@

using UnityEngine;/// <summary>
/// This class makes it possible to activate or select something by pressing a key (such as space bar for example).
/// </summary>

[AddComponentMenu("Game/UI/Key Binding")]
public class UIKeyBinding : MonoBehaviour
{public enum Action{PressAndClick,Select,}public enum Modifier{None,Shift,Control,Alt,}/// <summary>/// Key that will trigger the binding./// </summary>public KeyCode keyCode = KeyCode.None;/// <summary>/// Modifier key that must be active in order for the binding to trigger./// </summary>public Modifier modifier = Modifier.None;/// <summary>/// Action to take with the specified key./// </summary>public Action action = Action.PressAndClick;bool mIgnoreUp = false;bool mIsInput = false;/// <summary>/// If we're bound to an input field, subscribe to its Submit notification./// </summary>void Start (){UIInput input = GetComponent<UIInput>();mIsInput = (input != null);if (input != null) EventDelegate.Add(input.onSubmit, OnSubmit);}/// <summary>/// Ignore the KeyUp message if the input field "ate" it./// </summary>void OnSubmit () { if (UICamera.currentKey == keyCode && IsModifierActive()) mIgnoreUp = true; }/// <summary>/// Convenience function that checks whether the required modifier key is active./// </summary>bool IsModifierActive (){if (modifier == Modifier.None) return true;if (modifier == Modifier.Alt){if (Input.GetKey(KeyCode.LeftAlt) ||Input.GetKey(KeyCode.RightAlt)) return true;}else if (modifier == Modifier.Control){if (Input.GetKey(KeyCode.LeftControl) ||Input.GetKey(KeyCode.RightControl)) return true;}else if (modifier == Modifier.Shift){if (Input.GetKey(KeyCode.LeftShift) ||Input.GetKey(KeyCode.RightShift)) return true;}return false;}/// <summary>/// Process the key binding./// </summary>void Update (){if (keyCode == KeyCode.None || !IsModifierActive()) return;if (action == Action.PressAndClick){if (UICamera.inputHasFocus) return;UICamera.currentTouch = UICamera.controller;UICamera.currentScheme = UICamera.ControlScheme.Mouse;UICamera.currentTouch.current = gameObject;if (Input.GetKeyDown(keyCode)){UICamera.Notify(gameObject, "OnPress", true);}if (Input.GetKeyUp(keyCode)){UICamera.Notify(gameObject, "OnPress", false);UICamera.Notify(gameObject, "OnClick", null);}UICamera.currentTouch.current = null;}else if (action == Action.Select){if (Input.GetKeyUp(keyCode)){if (mIsInput){if (!mIgnoreUp && !UICamera.inputHasFocus){UICamera.selectedObject = gameObject;}mIgnoreUp = false;}else{UICamera.selectedObject = gameObject;}}}}
}

转载于:https://www.cnblogs.com/vsdog/p/5195113.html

ngui中 代码调用按钮事件(后来改成了按钮绑定键盘..)相关推荐

  1. mysql中如何将默认用户名root改成其他?

    大家好,我是雄雄,欢迎关注公众号[雄雄的小课堂]. 最近,我的个人站上线啦,大家可以直接在浏览器的地址栏中输入:穆雄雄.com,轻轻敲击回车,即可直接进入-- 欢迎大家多多关注,多多留言,多多点赞,多 ...

  2. python代码转成php代码的工具 或者go转成php的代码,想把odoo改成成php swoole当成web服务+go的架构

    目前市场上有一些可以将Python代码转换为PHP代码的工具,例如: Transcrypt:Transcrypt是一个将Python代码转换为JavaScript和PHP的工具.它可以将Python代 ...

  3. html 图片 把绝对路径改为相对路径,html中想把图片绝对路径 改成相对路径怎么操作?...

    首先要我们要弄懂相对路径修改成绝对路径那么必须要知道路径的基本概念. html中的路径:指文件存放的位置,在网页中利用路径可以引用文件,完成:插入图像.视频等功能.表示在html中路径的使用方式有两种 ...

  4. android软键盘回车键改成完成按钮,Android修改软键盘回车键并监听

    首先在XML中修改想把回车键改变为什么键 android:imeOptions="actionSend" android:inputType="text" 其中 ...

  5. HTML返回上一级目录图片,html中想把图片绝对路径 改成相对路径怎么操作?

    首先要我们要弄懂相对路径修改成绝对路径那么必须要知道路径的基本概念. html中的路径:指文件存放的位置,在网页中利用路径可以引用文件,完成:插入图像.视频等功能.表示在html中路径的使用方式有两种 ...

  6. element-ui单选框按钮的样式改成勾勾

    默认的单选框的样式是这样的 我想换成图标是勾勾的. // 全局单选框图标换成勾勾     #app /deep/ .el-radio__input.is-checked .el-radio__inne ...

  7. 计算机改成音乐键位,让键盘变成你想要的样子:改键利器 Karabiner-Elements

    随着年度作者颁奖典礼的结,几位最佳作者已经陆陆续续拿到了我派定制键盘

  8. webview中的图片相应事件相应 坑

    需求: 点击webview中的图片相应事件,点击里面按钮相应事件.网页源代码中没有提供响应的javascript的方法,所以需要重新写方法 来响应时间 ,实现android和html代码互调 代码 红 ...

  9. 逆向破解之易语言按钮事件特征码

    什么是按钮事件特征码 破解的一个用易语言编写的软件,通常先要搜索到按钮事件特征码,但是并不是说你按钮事件特征码处下断点就一定能精准的捕获按钮事件.例如易语言时钟控件或启动窗口事件都会通过消息派遣至按钮 ...

最新文章

  1. 手写 Spring 事务、IOC、DI 和 MVC
  2. Leetcode 292. Nim 游戏 解题思路及C++实现
  3. 读写xml节点的数据总结
  4. CreateDirectory GetCurrentDirectory 和SetCurrentDirectory
  5. Tarjan点的双联通(寻找割点)
  6. 使用DataTables合并行
  7. 微信小程序毕业设计 就餐预约点餐小程序毕业设计
  8. 带你深入了解GPU、FPGA和ASIC
  9. 滴水石穿,奇迹是一点点实现的
  10. 乔治城大学计算机科学专业,乔治城大学计算机系
  11. 计算机网络课后习题概略
  12. passenger 部署
  13. 极米Z6X Pro值得购买吗?这篇评测告诉你
  14. 如何区别计算机体系结构与计算机组成这两个概念?
  15. 在PYNQ-Z2上移植RISC-V
  16. Linux常用打包命令
  17. 目标检测2022最新进展
  18. 单身9年,有天猫双11的陪伴不再寂寞
  19. 计算机仿真模拟论文,计算机仿真论文
  20. 【转】很全的英语短语

热门文章

  1. python selenium安装
  2. 模型预测控制的缺点_【电子技术】【2018.01】模型预测控制FPGA实现的协同设计...
  3. np中meshgrid生成二维矩阵matplotlib中imshow生成图形
  4. matlab抓取股票数据,Matlab通过sina web接口获取个数即时股票数据函数实现代码
  5. Cardano链上首个流动性解决方案商Occam宣布与Changelly合作孵化新项目
  6. aelf宣布推出集中式资产管理系统CAM
  7. 少数以太坊矿池正在联合抵制EIP-1559提案的实施
  8. SAP License:ERP系统管理软件该有的“魅力”
  9. SAP License:SAP的2021关键词:协作
  10. C++并发编程之std::future