Skip to content

Commit 36a2c54

Browse files
committed
Added game loop and update patterns
1 parent 07b9a77 commit 36a2c54

18 files changed

+1118
-6
lines changed

Assets/Patterns/9. Update.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/Patterns/9. Update/Custom Update method.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.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
%YAML 1.1
2+
%TAG !u! tag:unity3d.com,2011:
3+
--- !u!21 &2100000
4+
Material:
5+
serializedVersion: 6
6+
m_ObjectHideFlags: 0
7+
m_CorrespondingSourceObject: {fileID: 0}
8+
m_PrefabInstance: {fileID: 0}
9+
m_PrefabAsset: {fileID: 0}
10+
m_Name: Ground
11+
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
12+
m_ShaderKeywords:
13+
m_LightmapFlags: 4
14+
m_EnableInstancingVariants: 0
15+
m_DoubleSidedGI: 0
16+
m_CustomRenderQueue: -1
17+
stringTagMap: {}
18+
disabledShaderPasses: []
19+
m_SavedProperties:
20+
serializedVersion: 3
21+
m_TexEnvs:
22+
- _BumpMap:
23+
m_Texture: {fileID: 0}
24+
m_Scale: {x: 1, y: 1}
25+
m_Offset: {x: 0, y: 0}
26+
- _DetailAlbedoMap:
27+
m_Texture: {fileID: 0}
28+
m_Scale: {x: 1, y: 1}
29+
m_Offset: {x: 0, y: 0}
30+
- _DetailMask:
31+
m_Texture: {fileID: 0}
32+
m_Scale: {x: 1, y: 1}
33+
m_Offset: {x: 0, y: 0}
34+
- _DetailNormalMap:
35+
m_Texture: {fileID: 0}
36+
m_Scale: {x: 1, y: 1}
37+
m_Offset: {x: 0, y: 0}
38+
- _EmissionMap:
39+
m_Texture: {fileID: 0}
40+
m_Scale: {x: 1, y: 1}
41+
m_Offset: {x: 0, y: 0}
42+
- _MainTex:
43+
m_Texture: {fileID: 0}
44+
m_Scale: {x: 1, y: 1}
45+
m_Offset: {x: 0, y: 0}
46+
- _MetallicGlossMap:
47+
m_Texture: {fileID: 0}
48+
m_Scale: {x: 1, y: 1}
49+
m_Offset: {x: 0, y: 0}
50+
- _OcclusionMap:
51+
m_Texture: {fileID: 0}
52+
m_Scale: {x: 1, y: 1}
53+
m_Offset: {x: 0, y: 0}
54+
- _ParallaxMap:
55+
m_Texture: {fileID: 0}
56+
m_Scale: {x: 1, y: 1}
57+
m_Offset: {x: 0, y: 0}
58+
m_Floats:
59+
- _BumpScale: 1
60+
- _Cutoff: 0.5
61+
- _DetailNormalMapScale: 1
62+
- _DstBlend: 0
63+
- _GlossMapScale: 1
64+
- _Glossiness: 0.5
65+
- _GlossyReflections: 1
66+
- _Metallic: 0
67+
- _Mode: 0
68+
- _OcclusionStrength: 1
69+
- _Parallax: 0.02
70+
- _SmoothnessTextureChannel: 0
71+
- _SpecularHighlights: 1
72+
- _SrcBlend: 1
73+
- _UVSec: 0
74+
- _ZWrite: 1
75+
m_Colors:
76+
- _Color: {r: 0.6509434, g: 0.6509434, b: 0.6509434, a: 1}
77+
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}

Assets/Patterns/9. Update/Custom Update method/Ground.mat.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/Patterns/9. Update/Custom Update method/Scripts.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.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Update.CustomUpdateMethod
6+
{
7+
//Example of custom update method to illustrate the update method pattern in the book "Game Programming Patterns"
8+
//This idea is based on code from the book "Unity 2017 Game Optimization"
9+
//This class will run all our custom update methods in Unity's own Update method, which will make it easier to pause the game
10+
public class GameController : MonoBehaviour
11+
{
12+
//The list with all objects with a custom update method
13+
//Is static to make it easier to illustrate the example, but you could maybe use the Observer pattern to register methods
14+
private static List<IUpdateable> updateableObjects = new List<IUpdateable>();
15+
16+
private bool isPaused = false;
17+
18+
19+
20+
//This should be the game's only MonoBehaviour Update method
21+
private void Update()
22+
{
23+
//Run all custom update methods
24+
if (!isPaused && updateableObjects != null)
25+
{
26+
float dt = Time.deltaTime;
27+
28+
//Iterate through all objects backwards in case one ojects decides to destroy itself
29+
for (int i = updateableObjects.Count - 1; i >= 0; i--)
30+
{
31+
IUpdateable updateableObj = updateableObjects[i];
32+
33+
updateableObj.OnUpdate(dt);
34+
}
35+
}
36+
37+
//Pause-unpause
38+
if (Input.GetKeyDown(KeyCode.Space))
39+
{
40+
isPaused = !isPaused;
41+
}
42+
}
43+
44+
45+
46+
//Register new object
47+
public static void RegisterUpdateableObject(IUpdateable obj)
48+
{
49+
if (!updateableObjects.Contains(obj))
50+
{
51+
updateableObjects.Add(obj);
52+
}
53+
else
54+
{
55+
MonoBehaviour mb = (MonoBehaviour)obj;
56+
57+
Debug.Log($"{mb.gameObject.name} has already been registered");
58+
}
59+
}
60+
61+
62+
63+
//Unregister
64+
public static void UnregisterUpdateableObject(IUpdateable obj)
65+
{
66+
if (!updateableObjects.Contains(obj))
67+
{
68+
updateableObjects.Remove(obj);
69+
}
70+
}
71+
}
72+
}

Assets/Patterns/9. Update/Custom Update method/Scripts/GameController.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Update.CustomUpdateMethod
6+
{
7+
//Interface for base class for custom Update method
8+
public interface IUpdateable
9+
{
10+
//This is the custom update method
11+
void OnUpdate(float dt);
12+
}
13+
}

Assets/Patterns/9. Update/Custom Update method/Scripts/IUpdateable.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Update.CustomUpdateMethod
6+
{
7+
//Attach to all object that should have a custom Update (and Start) method
8+
//The parent class will handle the registration of the OnUpdate method
9+
public class ObjectWithCustomUpdateMethod : UpdateableComponent
10+
{
11+
private const float SPEED = 10f;
12+
13+
private const float MAP_RADIUS = 10f;
14+
15+
16+
//Custom start, which will be called from the parent which uses Unity's Start() method
17+
protected override void OnStart()
18+
{
19+
Debug.Log($"[{this.name}]Custom Start is working");
20+
21+
//Generate a random direction
22+
transform.rotation = GetRandomDirection();
23+
}
24+
25+
26+
//Custom Update()
27+
//dt is Time.deltaTime
28+
public override void OnUpdate(float dt)
29+
{
30+
//Move forward
31+
Vector3 newPos = transform.position + transform.forward * SPEED * dt;
32+
33+
//Are we outside of the circle?
34+
if ((newPos - Vector3.zero).sqrMagnitude > MAP_RADIUS * MAP_RADIUS)
35+
{
36+
//If so we cant move and have to change directon
37+
transform.rotation = GetRandomDirection();
38+
}
39+
//Move to the new postion
40+
else
41+
{
42+
transform.position = newPos;
43+
}
44+
}
45+
46+
47+
//Generate a quaternion with a random rotation around y axis
48+
private Quaternion GetRandomDirection()
49+
{
50+
Quaternion randomDir = Quaternion.Euler(new Vector3(0f, Random.Range(0f, 360f), 0f));
51+
52+
return randomDir;
53+
}
54+
}
55+
}

Assets/Patterns/9. Update/Custom Update method/Scripts/ObjectWithCustomUpdateMethod.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace Update.CustomUpdateMethod
6+
{
7+
//Base class for objects with a custom Update method
8+
public class UpdateableComponent : MonoBehaviour, IUpdateable
9+
{
10+
//Unity's method which is working fine because the class inherits from MonoBehaviour
11+
private void Start()
12+
{
13+
//Register the object
14+
GameController.RegisterUpdateableObject(this);
15+
16+
OnStart();
17+
}
18+
19+
20+
//This is a custom Start method which the child can override, because we can't use Unity's Start in both parent and child
21+
protected virtual void OnStart()
22+
{
23+
24+
}
25+
26+
27+
//Custom update method, which the child can override
28+
public virtual void OnUpdate(float dt)
29+
{
30+
31+
}
32+
33+
34+
private void OnDestroy()
35+
{
36+
//Unegister the object
37+
//Remember that it's dangerous to call another method in OnDestroy() because the other method might already be destroyed
38+
GameController.UnregisterUpdateableObject(this);
39+
}
40+
}
41+
}

Assets/Patterns/9. Update/Custom Update method/Scripts/UpdateableComponent.cs.meta

Lines changed: 11 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)