Skip to content

Commit ffe1fe1

Browse files
committed
add Game Loop Pattern
add Game Loop Pattern
1 parent d379fe6 commit ffe1fe1

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

Assets/Game Programming Patterns/Event Queue Pattern/README.md.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game Programming Patterns/Game Loop Pattern.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Game Programming Patterns/Game Loop Pattern/Example.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//-------------------------------------------------------------------------------------
2+
// GameLoopPatternExample.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
9+
10+
namespace GameLoopPatternExample
11+
{
12+
13+
public class GameLoopPatternExample : MonoBehaviour
14+
{
15+
GameLoopManager GameLoop = new GameLoopManager();
16+
17+
void Start()
18+
{
19+
//进行游戏循环
20+
//DoGameLoop();
21+
Debug.Log("Unity已经内建了游戏循环模式,即Update( ),自己实现会导致卡死。这边仅保留代码框架,不作调用");
22+
}
23+
24+
void Update()
25+
{
26+
27+
}
28+
29+
/// <summary>
30+
/// 进行游戏循环
31+
/// </summary>
32+
void DoGameLoop()
33+
{
34+
if (GameLoop==null)
35+
{
36+
GameLoop = new GameLoopManager();
37+
}
38+
39+
GameLoop.DoGameLoop();
40+
}
41+
}
42+
43+
44+
/// <summary>
45+
/// 游戏循环manager
46+
/// </summary>
47+
public class GameLoopManager
48+
{
49+
50+
/// <summary>
51+
/// 游戏更新的粒度
52+
/// </summary>
53+
public const float MS_PER_UPDATE = 0.06F;
54+
55+
/// <summary>
56+
/// 进行游戏循环
57+
/// </summary>
58+
public void DoGameLoop()
59+
{
60+
double previous = Time.realtimeSinceStartup;
61+
double lag = 0.0;
62+
if (Time.realtimeSinceStartup==0f)
63+
{
64+
return;
65+
}
66+
while (true)
67+
{
68+
//当前时间
69+
double current = Time.realtimeSinceStartup;
70+
//消逝的时间
71+
double elapsed = current - previous;
72+
previous = current;
73+
lag += elapsed;
74+
//ProcessInput();
75+
76+
while (lag >= MS_PER_UPDATE)
77+
{
78+
//Update();
79+
lag -= MS_PER_UPDATE;
80+
}
81+
82+
//Render();
83+
}
84+
}
85+
86+
87+
/// <summary>
88+
/// 处理按键消息
89+
/// </summary>
90+
void ProcessInput()
91+
{
92+
if (Input.GetKeyDown(KeyCode.Alpha1))
93+
{
94+
Debug.Log("[GameLoopManager]你按下了键盘1键!");
95+
}
96+
}
97+
98+
/// <summary>
99+
/// 进行渲染
100+
/// </summary>
101+
void Render()
102+
{
103+
//do render
104+
}
105+
106+
/// <summary>
107+
/// 处理更新
108+
/// </summary>
109+
void Update()
110+
{
111+
//do update
112+
}
113+
114+
}
115+
116+
117+
118+
}

Assets/Game Programming Patterns/Game Loop Pattern/Example/GameLoopPatternExample.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Game Programming Patterns/Game Loop Pattern/Example/GameLoopPatternExample.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)