当项目需求中,后期可能接入多种输入设备的时候,可以借鉴一下以下代码

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System; public abstract class TInputBase : MonoBehaviour{ public event InputSchemeEventHandler InputSchemeEvent; public abstract InputSchemeEventArgs HandleInput(); public delegate void InputSchemeEventHandler(TInputBase sender, TInputBase.InputSchemeEventArgs inputArgs); //Packaging Input data
protected InputSchemeEventArgs GetInputData(GameObject currentObject,EnumList.InputResultTypes inputStyle)
{
return new InputSchemeEventArgs
{
CurrentGazeObject = currentObject, Style = inputStyle,
};
} //Notify delegates during the update method
protected void NotifyDelegates(InputSchemeEventArgs data)
{
if (InputSchemeEvent != null)
{
this.InputSchemeEvent(this, data);
}
} void Update()
{
if (this.HandleInput() != null)
{
this.NotifyDelegates(this.HandleInput());
}
} //Args (input args)
public class InputSchemeEventArgs : EventArgs
{
public GameObject CurrentGazeObject; public EnumList.InputResultTypes Style;
}
}
 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System; public abstract class TInputBase : MonoBehaviour{ public event InputSchemeEventHandler InputSchemeEvent; public abstract InputSchemeEventArgs HandleInput(); public delegate void InputSchemeEventHandler(TInputBase sender, TInputBase.InputSchemeEventArgs inputArgs); //Packaging Input data
protected InputSchemeEventArgs GetInputData(GameObject currentObject,EnumList.InputResultTypes inputStyle)
{
return new InputSchemeEventArgs
{
CurrentGazeObject = currentObject, Style = inputStyle,
};
} //Notify delegates during the update method
protected void NotifyDelegates(InputSchemeEventArgs data)
{
if (InputSchemeEvent != null)
{
this.InputSchemeEvent(this, data);
}
} void Update()
{
if (this.HandleInput() != null)
{
this.NotifyDelegates(this.HandleInput());
}
} //Args (input args)
public class InputSchemeEventArgs : EventArgs
{
public GameObject CurrentGazeObject; public EnumList.InputResultTypes Style;
}
}
 using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class TKeyboardInput : TInputBase { InputSchemeEventArgs inputNoop;
public GameObject rayLaunchPoint; public override InputSchemeEventArgs HandleInput ()
{
inputNoop = null;
Ray ray = new Ray(rayLaunchPoint.transform.position, rayLaunchPoint.transform.forward);
Debug.DrawLine(ray.origin, ray.origin + (ray.direction * 800f), Color.yellow);
RaycastHit hit;
if (Physics.Raycast(ray,out hit,800f)) { if (Input.GetKeyDown(KeyCode.Space)){
inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.ClickDown);
}
if (Input.GetKey(KeyCode.Space)){
inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.Clicking);
}
if (Input.GetKeyUp(KeyCode.Space)){
inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.ClickUp);
}
}
return inputNoop;
}
}
 using System.Collections;
using System.Collections.Generic;
using UnityEngine; public abstract class TClickBase : MonoBehaviour { public abstract void HandleClick (TInputBase.InputSchemeEventArgs inputArgs);
}
 using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class TDaydreamInput : TInputBase { public override InputSchemeEventArgs HandleInput ()
{
//Debug.Log ("Daydream 手柄输入");
return null;
}
}
 using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class TestFocusEvents : TClickBase { public override void HandleClick (TInputBase.InputSchemeEventArgs inputArgs)
{
if (inputArgs.Style.Equals(EnumList.InputResultTypes.ClickDown)) {
if (inputArgs.CurrentGazeObject.name=="Cube") {
Debug.Log ("click down cube");
}
}
if (inputArgs.Style.Equals(EnumList.InputResultTypes.Clicking)) {
if (inputArgs.CurrentGazeObject.name=="Cube") {
Debug.Log ("clicking cube");
}
}
if (inputArgs.Style.Equals(EnumList.InputResultTypes.ClickUp)) {
if (inputArgs.CurrentGazeObject.name=="Cube") {
Debug.Log ("click up cube");
}
}
}
void Start()
{
TController.register = this;
}
}

Unity 框架(一)的更多相关文章

  1. Unity框架入门

    介绍Unity框架之前,先要说几个概念DIP依赖倒置原则.IOC控制反转.DI依赖注入 DIP是设计原则之一,定义:上层不应该依赖于底层,两者都依赖于抽象: 抽象不依赖于细节,细节应该依赖于抽象. 像 ...

  2. 深入理解IOC模式及Unity框架

    研究了下,有几篇博客确实已经说得很清楚了 1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html  这篇博客是通过一个 ...

  3. [Solution] AOP原理解析及Castle、Autofac、Unity框架使用

    本节目录: AOP介绍 AOP基本原理 AOP框架 Castle Core Castle Windsor Autofac Unity AOP介绍 面向切面编程(Aspect Oriented Prog ...

  4. [Solution] DI原理解析及Castle、Unity框架使用

    本节目录 DI介绍 DI基本原理 DI框架 Castle Windsor Unity Autofac Spring.Net DI介绍 控制反转(Inversion of Control,英文缩写为Io ...

  5. 基于xlua和mvvm的unity框架

    1.框架简介 这两天在Github上发现了xlua的作者车雄生前辈开源的一个框架—XUUI,于是下载下来学习了一下.XUUI基于xlua,又借鉴了mvvm的设计概念.xlua是目前很火的unity热更 ...

  6. AOP原理解析及Castle、Autofac、Unity框架使用

    转自:https://www.cnblogs.com/neverc/p/5241466.html AOP介绍 面向切面编程(Aspect Oriented Programming,英文缩写为AOP), ...

  7. IOC模式及Unity框架文章收藏

    1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html 通过Unity实现IOC容器. 2.深入理解DIP.IoC.D ...

  8. 【Unity--Apwork框架】AOP编程--拦截,用于缓存和异常处理(Unity框架的拦截注入-Interception)

    第一步:定义拦截行为:CachingBehavior 和 ExceptionLoggingBehavior 他们都继承接口:IInterceptionBehavior (程序集 Microsoft.P ...

  9. IAR搭建unity框架

    1. 新建工程 2. 增加组 unit,并加入相应源码 3. 增加需要测试的API源文件 4. 编写相应的测试用例 编译,download and debug view->TerminalIO ...

  10. 依赖注入Unity框架

    依赖注入和控制反转是对同一件事情的不同描述,从某个方面讲,就是它们描述的角度不同.依赖注入是从应用程序的角度在描述,可以把依赖注入描述完整点:应用程序依赖容器创建并注入它所需要的外部资源:而控制反转是 ...

随机推荐

  1. jQuery学习(一)——jQuery入门

    1.jQuery基础 Jquery它是一个库(框架),要想使用它,必须先引入! jquery-1.8.3.js:一般用于学习阶段. jquery-1.8.3.min.js:用于项目使用阶段 官网下载后 ...

  2. miniUI打开一个新的画面(ondestroy)

    转自:http://blog.csdn.net/u012934325/article/details/77914691 需求:开发中需求通过点击父界面一个按钮弹出一个子界面,在miniUI中我们可以这 ...

  3. CentOS 6.9 CentOS 7.4 自动安装系统 kickstart

    通过ks文件 实现 CentOS 6.9 & 7.4 自动安装系统 环境: VMware 14.0 Pro版 光盘镜像: CentOS-6.9-x86_64-minimal.iso ks文件生 ...

  4. [zjoi2016]小星星 (容斥+DP)

    我们先用树形DP,求出选取集合S中的点,满足连通性的但是标号可重的方案数,贡献给F(i)(1\(\leq\)i\(\leq\)\(\mid S\mid\)),也就是我们要处理出F(i)代表取至多i个点 ...

  5. mysql 密码的破解

    现在的主流的数据库一般是mysql  ,sql  server ,  oracle. 有的时候我们忘记了数据库密码的时候我们要怎么办,破解别人的数据库的密码的时候我们要怎么搞  忘记密码是一件很头痛的 ...

  6. vue-cli 打包 使用 history模式 的后端配置

    apache的配置 这是windows下的 在httpd-vhosts.conf文件中把目录指向项目index.html文件所在的位置 # Virtual Hosts # <VirtualHos ...

  7. pytorch 2 variable 变量

    import torch from torch.autograd import Variable tensor = torch.FloatTensor([[1, 2], [3, 4]]) variab ...

  8. 时间就是金钱HNCOI2000(最短路)

    时间就是金钱HNCOI2000 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! 人们总是选时间最短或费用最低的路线 例如, ...

  9. ajax动态添加复选框

    function getLands() { $.ajax({ url:"httpserver.do?doPost&userQygs="+$("#userQygs& ...

  10. HDU 5607 graph(矩阵优化+概率DP)

    该题非常easy想到求概率的转移方程:用d[i][j]表示第i步,走到j点的概率. 可是该题的k高达1e9.所以依照套路.要用矩阵相乘来优化. 第一次写矩阵相乘. 大概的意思就是利用矩阵实现递推. 而 ...