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

 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. Calling convention-调用约定

    In computer science, a calling convention is an implementation-level (low-level) scheme for how subr ...

  2. [转载]vim常用命令总结

    内容出处https://www.jianshu.com/p/a8ab13cff1ea 如有侵权请告知 移动.跳转 h.j.k.l:分别对应左.下.上.右.按键盘分布,从左到右,逆时针. w:移动到下一 ...

  3. C语言的常用printf打印占位符%d, %u, %f, %s, %c, %o, %x

    占位符含义及用法 代码: #include <stdio.h> int main(int argc, char const *argv[]) { , b = -; // 默认10进制赋值 ...

  4. vue mint-ui swipe 不显示或显示空白

    vue mint-ui swipe 不显示或显示空白? 解决需要在mt-swipe上层元素设置高度 <div> <div> <mt-header title=" ...

  5. spring中IOC的简单使用

    spring的ioc主要就是依赖注入,有基于构造器的依赖注入还有通过设值注入,这里我只简单的实现设值注入的方法,通过spring的依赖管理,我们可以很方便的了解各层之间的依赖关系,降低了各层之间的耦合 ...

  6. 洛谷 P1417 烹调方案 (01背包拓展)

    一看到这道题就是01背包 但是我注意到价值和当前的时间有关. 没有想太多,直接写,0分 然后发现输入方式不对-- 改了之后只有25分 我知道wa是因为时间会影响价值,但不知道怎么做. 后来看了题解,发 ...

  7. 笔记本win2008 r2的hyper-v安装centos

    一.i5以上cpu支持虚拟化,不过默认是关闭的,先到bios设置里把虚拟功能打开: 二.“服务器管理器”->“角色”里安装hyper-v并重启: 三.设置无线网络桥接,有线就不需要了,具体如下: ...

  8. android自己定义圆盘时钟

    自己定义圆盘时钟的大概流程:由于圆盘时钟的圆盘是不须要动的,所以不必要加在自己定义的view里面,在view里面仅仅须要绘制秒针和分针时针并控其转动就可以. 下面就是自己定义view的主要代码: pa ...

  9. Cocos2dx 小技巧(十五)话说ScrollView的delegate实现过程

    附:本文參加了CSDN博客大赛.亲假设认为这篇文章不错,就大胆的来投上一票吧! !!http://vote.blog.csdn.net/Article/Details? articleid=34140 ...

  10. MyEclipse完好提示配置

    MyEclipse完好提示配置 一般的,MyEclipse中的提示以"."后进行提示,不是非常完好.如今.改动提示配置,让提示更完好. 详细操作例如以下: 1.打开MyEclips ...