-
-
Save unitycoder/50ecd385d3d8c642df70 to your computer and use it in GitHub Desktop.
Example Unity Class - How to format your code.
This file contains hidden or 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
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class ExampleClass : MonoBehaviour // Try to name classes so other developers can infer their functionality. Abstract classes are prefixed with 'Base' | |
{ | |
private enum WidgetType // Enums should end in 'Type' and can be inlined with the class if private, but generally should be relocated if public | |
{ | |
None, | |
Circle, | |
Sphere | |
} | |
private struct SomeWidgetData // Custom structs used by the class can use public fields, if custom classes or structs need to be public, break them out | |
{ | |
public int WidgetId; | |
public string WidgetName; | |
} | |
private const int WIDGET_OFFSET = 10; // Consts come before serialized fields, but after any enums, classes or structs used by the class | |
[Header("Widget Properties")] // Use the Header attribute to organize your serializable fields | |
[SerializeField] private Transform m_TargetTransform; // Serialized fields should be marked private, and start with 'm_' | |
private string m_SomeString; // Non-serialized fields come after serialized fields, and start with 'm_' | |
protected bool m_SomeBool; // Use the protected access modifier to allow inherited classes to access the field | |
public bool IsWidgetReady { get; private set; } // Properties should have a private or protected set where possible, and follow normal Pascal case | |
public Transform Transform { get; private set; } // Note that it is OK to have public properties named the same as their type - the compiler can infer what you want to use | |
private Dictionary<int,Transform> m_WidgetTransforms; // Use dictionaries where possible, retrieval via key is fast | |
private void Awake() // Use awake to initialize variables that are used for the duration of the class lifecycle | |
{ | |
m_WidgetTransforms = new Dictionary<int, Transform>(); // Initialize in Awake rather than in the fields declaration | |
} | |
private void Start() // Unity Monobehaviour methods should come first | |
{ | |
// Comments should have a space after the '//' and be written as proper sentences | |
int someValue = 3; // Local variables should be camel case | |
Transform someTransform = this.GetComponent<Transform>(); // When using Monobehaviour derived methods, prefix with 'this.' | |
} | |
public string GetWidgetName(int widgetId) // Public methods are Pascal case and parameters are camel case - note that 'ID' is written 'Id' - try to avoid consecutive uppercase | |
{ | |
Debug.LogFormat("Output:{0}, {1}, {2}", "hello", widgetId, "world"); // Use Debug.LogFormat where possible | |
Vector3 position = this.transform.position; // As of Unity 5, there's no point caching transforms as it's done internally | |
return "hello world"; | |
} | |
public void CheckSomething() // Note that CheckSomething calls the coroutine StartCheckingSomething - we can easily infer the intended functionality | |
{ | |
StartCoroutine(StartCheckingSomething()); | |
} | |
private IEnumerator StartCheckingSomething() // Coroutine names should begin with "Start" and if called by another method, should be easily associated | |
{ | |
while(!this.IsWidgetReady) | |
{ | |
yield return new WaitForEndOfFrame(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment