forked from QianMo/Unity-Design-Pattern
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加数据局部性模式 | add Data Locality Pattern
增加数据局部性模式 | add Data Locality Pattern
- Loading branch information
Showing
6 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
Assets/Game Programming Patterns/Data Locality Pattern/Example.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
140 changes: 140 additions & 0 deletions
140
Assets/Game Programming Patterns/Data Locality Pattern/Example/DataLocalityPatternExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
//------------------------------------------------------------------------------------- | ||
// DataLocalityPatternExample.cs | ||
//------------------------------------------------------------------------------------- | ||
|
||
using UnityEngine; | ||
using System.Collections; | ||
using System; | ||
|
||
namespace DataLocalityPatternExample | ||
{ | ||
public class DataLocalityPatternExample : MonoBehaviour | ||
{ | ||
GameX gameProject; | ||
|
||
void Start() | ||
{ | ||
gameProject = new GameX(); | ||
gameProject.Start(); | ||
} | ||
|
||
void Update() | ||
{ | ||
if (gameProject!=null) | ||
{ | ||
gameProject.Update(); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 游戏应用程序类 | ||
/// </summary> | ||
public class GameX | ||
{ | ||
const int MAX_ENTITIES = 10000; | ||
|
||
int numEntities; | ||
|
||
/// <summary> | ||
/// 基于大数组存储保证数据连续性 | ||
/// </summary> | ||
AIComponent[] aiComponents = new AIComponent[MAX_ENTITIES]; | ||
PhysicsComponent[] physicsComponents = new PhysicsComponent[MAX_ENTITIES]; | ||
RenderComponent[] renderComponents = new RenderComponent[MAX_ENTITIES]; | ||
|
||
public void Start() | ||
{ | ||
numEntities = 10; | ||
for (int i = 0; i < numEntities; i++) | ||
{ | ||
aiComponents[i] = new AIComponent(); | ||
physicsComponents[i] = new PhysicsComponent(); | ||
renderComponents[i] = new RenderComponent(); | ||
} | ||
|
||
} | ||
|
||
|
||
public void Update() | ||
{ | ||
// Process AI. | ||
for (int i = 0; i < numEntities; i++) | ||
{ | ||
if (aiComponents!=null && aiComponents.Length>i && aiComponents[i]!= null) | ||
{ | ||
aiComponents[i].Update(); | ||
} | ||
} | ||
|
||
// Update physics. | ||
for (int i = 0; i < numEntities; i++) | ||
{ | ||
if (physicsComponents != null && physicsComponents.Length > i && physicsComponents[i] != null) | ||
{ | ||
physicsComponents[i].Update(); | ||
} | ||
|
||
} | ||
|
||
// Draw to screen. | ||
for (int i = 0; i < numEntities; i++) | ||
{ | ||
if (renderComponents != null && renderComponents.Length > i && renderComponents[i] != null) | ||
{ | ||
renderComponents[i].Render(); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
/// <summary> | ||
/// 组件接口 | ||
/// </summary> | ||
public interface IComponent | ||
{ | ||
void Update(); | ||
} | ||
|
||
/// <summary> | ||
/// AI组件 | ||
/// </summary> | ||
public class AIComponent : IComponent | ||
{ | ||
public void Update() | ||
{ | ||
Debug.Log("AIComponent Update!"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 物理组件 | ||
/// </summary> | ||
public class PhysicsComponent : IComponent | ||
{ | ||
public void Update() | ||
{ | ||
Debug.Log("PhysicsComponent Update!"); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 渲染组件 | ||
/// </summary> | ||
public class RenderComponent : IComponent | ||
{ | ||
public void Update() | ||
{ | ||
Debug.Log("RenderComponent Update!"); | ||
} | ||
|
||
|
||
public void Render() | ||
{ | ||
Debug.Log("RenderComponent Render!"); | ||
} | ||
} | ||
|
||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...ame Programming Patterns/Data Locality Pattern/Example/DataLocalityPatternExample.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+12.7 KB
.../Game Programming Patterns/Data Locality Pattern/Example/DataLocalityPatternExample.unity
Binary file not shown.
8 changes: 8 additions & 0 deletions
8
... Programming Patterns/Data Locality Pattern/Example/DataLocalityPatternExample.unity.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.