Last active
April 8, 2023 17:07
-
-
Save dasannikov/9fbffd8e6965e005f34390a3572abe20 to your computer and use it in GitHub Desktop.
Unity Coding Guidelines
This file contains 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; | |
namespace CompanyName { | |
public class TheThing : MonoBehaviour { | |
// 1 | |
// Public fields section. Try to avoid public fields. | |
public int SomeValue; // Avoid public fields. Use public properties instead. | |
public static int SomeGlobalValue; // Avoid static fields. | |
// 2 | |
// Unity inspector section | |
[SerializeField] Transform _otherTransform = default; | |
[SerializeField] float _someValue = default; | |
// 3 | |
// Protected section | |
protected int _someValue1; | |
protected const float SOME_VALUE = 0f; | |
// 4 | |
// Private section | |
int _someValue2; | |
Vector3 _someDirection = Vector3.up; | |
static int _someGlobalValue2; // Try to avoid using static fields. | |
// 5 | |
// Properties section | |
public int Value { get; private set; } // Use public auto properties if needed. | |
public float Value2 => _someValue + SOME_VALUE + CalculateSomeValue(); | |
// 6 | |
// Class methods. Use public only if you can't | |
float CalculateSomeValue() { | |
// Bad example of method (In this particular code property works better) | |
var retValue = _someDirection.magnitude; | |
return retValue; | |
} | |
public void MoveUp() => StartCoroutine(_MoveUp()); | |
IEnumerator _MoveUp() { | |
for(var i = 0; i < 10; i++) { | |
_someDirection += Vector3.up * 0.1f; | |
yield return new WaitForSeconds(0.1f); | |
} | |
} | |
// 7 | |
// Unity stuff. | |
void Start() { | |
var a = 10; // Use var! | |
for(var i = 0; i < a; i++) { | |
Debug.Log($"i = {i}"); // Use string interpolation. | |
Log.Message("i = {0}", i); // Custom log without interpolations and allocations is better. | |
} | |
} | |
void Update() { | |
// Don't write/leave empty methods. Leads to performance lack. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Real code/class example without guidelines comments.