1 /******************************************************************************
2 Style Guidelines:
3 * Readability - code is easy to read and to understand
4 * Standardisation - all team members are aligned with same code style
5 * Self-descriptive code - code is easy to understand without comments
6 * Debuggable - code is easy to debug (IDE friendly)
7 ******************************************************************************/
8
9 // using (imports) of other namespaces will be wrapped with region "Using"
10 #region Using
11 using System.Collections;
12 using System.Collections.Generic;
13 using UnityEngine;
14 #endregion
15
16 // types are defined within the scope of namespace
17 // space name starts with Upper case letter
18 // each following word starts with upper Case letter
19 // sub-spaces are divided by '.' (dot)
20 // here "MyNameSpace" is main space and "Scripts" is sub-space
21 namespace MyNameSpace.Scripts
22 {
23 /// <summary>
24 /// type name starts with Upper case letter
25 /// each following word starts with Upper case letter
26 /// <remarks>
27 /// type = class, enum, struct, delegate. interface, etc
28 /// </remarks>
29 /// </summary>
30 public class MyScript : MonoBehaviour
31 {
32 /// <summary>
33 /// private variable name starts with "_" (underscore) character
34 /// and with lower case letter afterwards
35 /// each following word starts with Upper case letter
36 /// </summary>
37 private Coroutine _myCoroutine;
38
39 /// <summary>
40 /// serializable variable signed with attribute <see cref="SerializeField"/>
41 /// if variable is not required for access in child class, it can remain 'private'
42 /// naming convention -- similar to private variables
43 /// </summary>
44 /// <remarks>
45 /// public property 'MyColor' will allow access to '_myColor' value from other types
46 /// </remarks>
47 [SerializeField]
48 private Color _myColor;
49
50 /// <summary>
51 /// 'MyColor' returns value of '_myColor'
52 /// no setter == not possible to change '_myColor' from another type
53 /// naming convention -- similar to public properties
54 /// </summary>
55 public Color MyColor => _myColor;
56
57 /// <summary>
58 /// method name starts with Upper case letter
59 /// each following word starts with Upper case letter
60 /// <remarks>
61 /// unity event methods are 'private' by default
62 /// </remarks>
63 /// </summary>
64 private void Start()
65 {
66 Debug.LogFormat("{0} started.", nameof(MyScript));
67
68 // opt-1: start 'MyCoroutine' directly and instantiate list in same line
69 StartCoroutine(MyCoroutine(new List<GameObject>{gameObject}));
70
71 // opt-2: capture list in local variable for debugging purposes
72 var gameObjects = new List<GameObject>{gameObject};
73 // start 'MyCoroutine' and capture 'StartCoroutine' result in class variable
74 _myCoroutine = StartCoroutine(MyCoroutine(gameObjects));
75 }
76
77 /// <summary>
78 /// method name starts with Upper case letter
79 /// each following word starts with Upper case letter
80 /// </summary>
81 public void StopMyCoroutine()
82 {
83 // do not execute function's code in case '_myCoroutine' is null
84 if (_myCoroutine == null)
85 {
86 // this instruction is between parenthesis in order to allow easy breakpoint
87 return;
88 }
89 StopCoroutine(_myCoroutine);
90 _myCoroutine = null;
91 }
92
93 /// <summary>
94 /// method name starts with Upper case letter
95 /// each following word starts with Upper case letter
96 /// </summary>
97 /// <param name="gameObjects">
98 /// parameter name starts with lower case letter
99 /// each following word starts with Upper case letter
100 /// </param>
101 /// <remarks>
102 /// this function is 'static', since it does not need to access to any members of this class
103 /// </remarks>
104 private static IEnumerator MyCoroutine(IEnumerable<GameObject> gameObjects)
105 {
106 // 'var' here can help in future refactoring, if we'll pass other type of list
107 foreach (var gameObj in gameObjects)
108 {
109 // TODO do something with game object and wait one frame
110 yield return null;
111 }
112 }
113 }
114 }