Unity 框架

Posted Jason_c

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity 框架相关的知识,希望对你有一定的参考价值。

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

 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using System;
 5 
 6 public abstract class TInputBase : MonoBehaviour{
 7     
 8     public event InputSchemeEventHandler InputSchemeEvent;
 9 
10     public abstract InputSchemeEventArgs HandleInput();
11 
12     public delegate void InputSchemeEventHandler(TInputBase sender, TInputBase.InputSchemeEventArgs inputArgs);
13 
14     //Packaging Input data
15     protected InputSchemeEventArgs GetInputData(GameObject currentObject,EnumList.InputResultTypes inputStyle)
16     {
17         return new InputSchemeEventArgs
18         {
19             CurrentGazeObject = currentObject,
20 
21             Style = inputStyle,
22         };
23     }
24 
25 
26     //Notify delegates during the update method
27     protected void NotifyDelegates(InputSchemeEventArgs data)
28     {
29         if (InputSchemeEvent != null)
30         {
31             this.InputSchemeEvent(this, data);
32         }
33     }
34 
35 
36     void Update()
37     {
38         if (this.HandleInput() != null)
39         {
40             this.NotifyDelegates(this.HandleInput());
41         }
42     }
43 
44     //Args (input args)
45     public class InputSchemeEventArgs : EventArgs
46     {
47         public GameObject CurrentGazeObject;
48 
49         public EnumList.InputResultTypes Style;
50     }
51 }
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 using System;
 5 
 6 public abstract class TInputBase : MonoBehaviour{
 7     
 8     public event InputSchemeEventHandler InputSchemeEvent;
 9 
10     public abstract InputSchemeEventArgs HandleInput();
11 
12     public delegate void InputSchemeEventHandler(TInputBase sender, TInputBase.InputSchemeEventArgs inputArgs);
13 
14     //Packaging Input data
15     protected InputSchemeEventArgs GetInputData(GameObject currentObject,EnumList.InputResultTypes inputStyle)
16     {
17         return new InputSchemeEventArgs
18         {
19             CurrentGazeObject = currentObject,
20 
21             Style = inputStyle,
22         };
23     }
24 
25 
26     //Notify delegates during the update method
27     protected void NotifyDelegates(InputSchemeEventArgs data)
28     {
29         if (InputSchemeEvent != null)
30         {
31             this.InputSchemeEvent(this, data);
32         }
33     }
34 
35 
36     void Update()
37     {
38         if (this.HandleInput() != null)
39         {
40             this.NotifyDelegates(this.HandleInput());
41         }
42     }
43 
44     //Args (input args)
45     public class InputSchemeEventArgs : EventArgs
46     {
47         public GameObject CurrentGazeObject;
48 
49         public EnumList.InputResultTypes Style;
50     }
51 }
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class TKeyboardInput : TInputBase {
 6     
 7     InputSchemeEventArgs inputNoop;
 8     public GameObject rayLaunchPoint;
 9 
10     public override InputSchemeEventArgs HandleInput ()
11     {
12         inputNoop = null;
13         Ray ray = new Ray(rayLaunchPoint.transform.position, rayLaunchPoint.transform.forward);
14         Debug.DrawLine(ray.origin, ray.origin + (ray.direction * 800f), Color.yellow);
15         RaycastHit hit;
16         if (Physics.Raycast(ray,out hit,800f)) {
17 
18             if (Input.GetKeyDown(KeyCode.Space)){
19                 inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.ClickDown);
20             }
21             if (Input.GetKey(KeyCode.Space)){
22                 inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.Clicking);
23             }
24             if (Input.GetKeyUp(KeyCode.Space)){
25                 inputNoop = base.GetInputData(hit.collider.gameObject,EnumList.InputResultTypes.ClickUp);
26             }
27         } 
28         return inputNoop;
29     }
30 }
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 public abstract class TClickBase : MonoBehaviour {
6 
7     public abstract void HandleClick (TInputBase.InputSchemeEventArgs inputArgs);
8 }
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class TDaydreamInput : TInputBase {
 6 
 7     public override InputSchemeEventArgs HandleInput ()
 8     {
 9         //Debug.Log ("Daydream 手柄输入");
10         return null;
11     }
12 }
 1 using System.Collections;
 2 using System.Collections.Generic;
 3 using UnityEngine;
 4 
 5 public class TestFocusEvents : TClickBase {
 6 
 7     public override void HandleClick (TInputBase.InputSchemeEventArgs inputArgs)
 8     {
 9         if (inputArgs.Style.Equals(EnumList.InputResultTypes.ClickDown)) {
10             if (inputArgs.CurrentGazeObject.name=="Cube") {
11                 Debug.Log ("click down cube");
12             }
13         }
14         if (inputArgs.Style.Equals(EnumList.InputResultTypes.Clicking)) {
15             if (inputArgs.CurrentGazeObject.name=="Cube") {
16                 Debug.Log ("clicking cube");
17             }
18         }
19         if (inputArgs.Style.Equals(EnumList.InputResultTypes.ClickUp)) {
20             if (inputArgs.CurrentGazeObject.name=="Cube") {
21                 Debug.Log ("click up cube");
22             }
23         }
24     }
25     void Start()
26     {
27         TController.register = this;
28     }
29 }

 

以上是关于Unity 框架的主要内容,如果未能解决你的问题,请参考以下文章

Unity报错:Read only asset Packages/com.xxxxx.xxx.xxxx/Editor/VSCodeDiscovery.cs.IPGSD has no meta(代码片段

Unity中获取Animator中动画片段的时长

text 来自Codyhouse框架的Browserlist片段源代码

unity动画一个片段播放完怎么让它不会到初始状态

csharp Unity C#片段用于将变换旋转到新的相对旋转数秒

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段