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

 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. 适配器模式(Adapter):类适配器、对象适配器

    适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口.A d a p t e r 模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 适用场景: 1.已经存在的类的接口 ...

  2. lua_pcall与lua_call之间的区别

    lua_pcall与lua_call之间的区别 定义: void lua_call (lua_State *L, int nargs, int nresults); int lua_pcall (lu ...

  3. Dropout 上

    From <白话深度学习与TensorFlow> Dropout 顾名思义是“丢弃”,在一轮训练阶段丢弃一部分网络节点,比如可以在其中的某些层上临时关闭一些节点,让他们既不输入也不输出,这 ...

  4. javaweb集成swagger

    一.添加依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...

  5. ocrsearch的横屏转竖屏的解决方案

    //这是其中解决预览图的一部分(坑了好久的)@Override public void onPreviewFrame(byte[] data, Camera camera) { Size previe ...

  6. 【codeforces 131E】Yet Another Task with Queens

    [题目链接]:http://codeforces.com/problemset/problem/131/E [题意] 给你n*n坐标上的m个皇后的位置; 然后让你求出,能够攻击到0,1,2-8个其他皇 ...

  7. Login.hbm.xml

    <?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLI ...

  8. maven的setting设置

    maven的setting设置,settings.xml文件,多写了几个仓库的地址: <?xml version="1.0" encoding="UTF-8&quo ...

  9. USACO 1.2 Palindromic Squares (进制转换,回文)

    /* ID:twd30651 PROG:palsquare LANG:C++ */ #include<iostream> #include<fstream> #include& ...

  10. angularjs 自定义服务

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...