Coding conventions for C# in Unity
Follow these naming conventions: http://www.dofactory.com/reference/csharp-coding-standards
- Use
[SerializeField](with a public property if necessary) instead of public fields.[SerializeField] private int someInt = 0; ... public int SomeInt { get {return someInt; } }
- Use
[Tooltip("")]for commenting fields[Tooltip("Changes the horizontal speed of the player")] [SerializeField] private float horizontalSpeed = 2f;
- Use
[Unit("")]to give extra information about the unit of a field[Unit("meters/second")] [SerializeField] private float startVelocity = 5f;
- Use
[HideInNormalInspector]instead of[HideInInspector]in order to still see the values in the debug inspector[Tooltip("The precalculated squared maximum speed for faster comparison")] [HideInNormalInspector] [SerializeField] private float squaredMaxSpeed = 5f;
- If not explicitly required to have a component use Reset() instead of [RequireComponent()]
- Use OnValidate() to precalculate properties
- Pack all editor scripts like Reset(), OnValidate() or other methods only used in the editor at the end of the class and encapsulate them with #if UNITY_EDITOR #endif
#if UNITY_EDITOR [Button] private void SortArray() { Array.Sort(sortedArray); } private void Reset() { if(player == null) { player = GetComponent<Player>(); } } // Called whenever the user changes a value in the inspector private void OnValidate() { reciprocalValue = 1f/value; squaredMaxSpeed = maxSpeed * maxSpeed; } #endif