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 Using11 using System.Collections;12 using System.Collections.Generic;13 using UnityEngine;14 #endregion15 16 // types are defined within the scope of namespace17 // space name starts with Upper case letter18 // each following word starts with upper Case letter19 // sub-spaces are divided by '.' (dot)20 // here "MyNameSpace" is main space and "Scripts" is sub-space21 namespace MyNameSpace.Scripts22 {23 /// <summary>24 /// type name starts with Upper case letter25 /// each following word starts with Upper case letter26 /// <remarks>27 /// type = class, enum, struct, delegate. interface, etc28 /// </remarks>29 /// </summary>30 public class MyScript : MonoBehaviour31 {32 /// <summary>33 /// private variable name starts with "_" (underscore) character34 /// and with lower case letter afterwards35 /// each following word starts with Upper case letter36 /// </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 variables43 /// </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 type53 /// naming convention -- similar to public properties54 /// </summary>55 public Color MyColor => _myColor;56 57 /// <summary>58 /// method name starts with Upper case letter59 /// each following word starts with Upper case letter60 /// <remarks>61 /// unity event methods are 'private' by default62 /// </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 line69 StartCoroutine(MyCoroutine(new List<GameObject>{gameObject}));70 71 // opt-2: capture list in local variable for debugging purposes72 var gameObjects = new List<GameObject>{gameObject};73 // start 'MyCoroutine' and capture 'StartCoroutine' result in class variable74 _myCoroutine = StartCoroutine(MyCoroutine(gameObjects));75 }76 77 /// <summary>78 /// method name starts with Upper case letter79 /// 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 null84 if (_myCoroutine == null)85 {86 // this instruction is between parenthesis in order to allow easy breakpoint87 return;88 }89 StopCoroutine(_myCoroutine);90 _myCoroutine = null;91 }92 93 /// <summary>94 /// method name starts with Upper case letter95 /// each following word starts with Upper case letter96 /// </summary>97 /// <param name="gameObjects">98 /// parameter name starts with lower case letter99 /// each following word starts with Upper case letter100 /// </param>101 /// <remarks>102 /// this function is 'static', since it does not need to access to any members of this class103 /// </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 list107 foreach (var gameObj in gameObjects)108 {109 // TODO do something with game object and wait one frame110 yield return null;111 }112 }113 }114 }