Skip to content

Instantly share code, notes, and snippets.

@dasannikov
Last active April 8, 2023 17:07
Show Gist options
  • Save dasannikov/9fbffd8e6965e005f34390a3572abe20 to your computer and use it in GitHub Desktop.
Save dasannikov/9fbffd8e6965e005f34390a3572abe20 to your computer and use it in GitHub Desktop.
Unity Coding Guidelines
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.
}
}
}
@dasannikov
Copy link
Author

dasannikov commented Mar 16, 2021

Real code/class example without guidelines comments.

using UnityEngine;
using System.Collections;

namespace Company {

    public class TheRealThing : MonoBehaviour {

        [SerializeField] Transform _otherTransform = default;
        [SerializeField] float _someValue = default;

        int _someValue2;
        Vector3 _someDirection = Vector3.up;
        const float SOME_VALUE = 0f;

        public int Value { get; private set; }
        public float Value2 => _someValue + SOME_VALUE + CalculateSomeValue();

        float CalculateSomeValue() {
            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);
            }
        }

        void Start() {
            var a = 10; // Use var!
            for(var i = 0; i < a; i++) {
                Log.Message("i = {0}", i);
            }
        }

    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment