Unity 框架(一)
当项目需求中,后期可能接入多种输入设备的时候,可以借鉴一下以下代码
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 框架(一)的更多相关文章
- Unity框架入门
介绍Unity框架之前,先要说几个概念DIP依赖倒置原则.IOC控制反转.DI依赖注入 DIP是设计原则之一,定义:上层不应该依赖于底层,两者都依赖于抽象: 抽象不依赖于细节,细节应该依赖于抽象. 像 ...
- 深入理解IOC模式及Unity框架
研究了下,有几篇博客确实已经说得很清楚了 1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html 这篇博客是通过一个 ...
- [Solution] AOP原理解析及Castle、Autofac、Unity框架使用
本节目录: AOP介绍 AOP基本原理 AOP框架 Castle Core Castle Windsor Autofac Unity AOP介绍 面向切面编程(Aspect Oriented Prog ...
- [Solution] DI原理解析及Castle、Unity框架使用
本节目录 DI介绍 DI基本原理 DI框架 Castle Windsor Unity Autofac Spring.Net DI介绍 控制反转(Inversion of Control,英文缩写为Io ...
- 基于xlua和mvvm的unity框架
1.框架简介 这两天在Github上发现了xlua的作者车雄生前辈开源的一个框架—XUUI,于是下载下来学习了一下.XUUI基于xlua,又借鉴了mvvm的设计概念.xlua是目前很火的unity热更 ...
- AOP原理解析及Castle、Autofac、Unity框架使用
转自:https://www.cnblogs.com/neverc/p/5241466.html AOP介绍 面向切面编程(Aspect Oriented Programming,英文缩写为AOP), ...
- IOC模式及Unity框架文章收藏
1.IoC模式:http://www.cnblogs.com/qqlin/archive/2012/10/09/2707075.html 通过Unity实现IOC容器. 2.深入理解DIP.IoC.D ...
- 【Unity--Apwork框架】AOP编程--拦截,用于缓存和异常处理(Unity框架的拦截注入-Interception)
第一步:定义拦截行为:CachingBehavior 和 ExceptionLoggingBehavior 他们都继承接口:IInterceptionBehavior (程序集 Microsoft.P ...
- IAR搭建unity框架
1. 新建工程 2. 增加组 unit,并加入相应源码 3. 增加需要测试的API源文件 4. 编写相应的测试用例 编译,download and debug view->TerminalIO ...
- 依赖注入Unity框架
依赖注入和控制反转是对同一件事情的不同描述,从某个方面讲,就是它们描述的角度不同.依赖注入是从应用程序的角度在描述,可以把依赖注入描述完整点:应用程序依赖容器创建并注入它所需要的外部资源:而控制反转是 ...
随机推荐
- 脱离node自己使用普通的requirejs管理js资源
首先,工程目录: 现在主页面(web框架写法.html): <!DOCTYPE html> <html lang="en"> <head> &l ...
- [HNOI2008]越狱 快速幂 逆推
考虑越狱的情况有些复杂,不如考虑总情况减去不越狱的情况. 显然,总情况为 $m^n$ 种,不越狱的情况为 $m*(m-1)*(m-1)*(m-1)....$ 即为 $m*(m-1)^(n-1)$. 做 ...
- wordpress 后台登录增加访问效验,优化退出效果
之前记录了 wordpress 后台登录增加访问效验, 记录了增加后台访问地址被直接访问的困难性的修改步骤. 还有一个地方需要补充一下,就是退出. 退出的时候不做调整会直接跳到首页,这样体验很不好. ...
- UI布局【转】
转载自: https://www.cnblogs.com/wangdaijun/p/5519459.html https://www.jianshu.com/p/f781c40df57c Good U ...
- Linux文件属性(属主属组权限)
Linux文件属性 Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规定. ...
- 【codeforces 29B】Traffic Lights
[题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...
- 理解Swift中map 和 flatMap对集合的作用
map和flatMap是函数式编程中常见的概念,python等语言中都有.借助于 map和flapMap 函数可以非常轻易地将数组转换成另外一个新数组. map函数可以被数组调用,它接受一个闭包作为參 ...
- JAVA:从public static void main(String args[])開始
我们都知道当你要执行一个JAVA文件的时候必需要有一个main函数. 这是为什么呢? 跟C语言的道理一样,当你执行一个文件的时候.你必需要有一个入口函数或者入口地址,在C里面是main函数.相同的在J ...
- MySQl Study学习之--MySQl二进制日志管理
MySQl Study学习之--MySQl二进制日志管理 MySQL二进制日志(Binary Log) a.它包括的内容及作用例如以下: 包括了全部更新了数据或者已经潜在更新了数据(比方没 ...
- Lambert/Diffuse 光照模型
Lambert/Diffuse光照模型的特点:各向同性,即与观察的方向无关,反射光只与入射光和入射角度相关. 1.光源垂直照射平面 如图,设入射光量为Ф, 平面面积为A, 则可以认为平面上每一点获取的 ...