Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Forked from shaunsales/ExampleClass.cs
Created December 2, 2015 23:19
Show Gist options
  • Select an option

  • Save unitycoder/50ecd385d3d8c642df70 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/50ecd385d3d8c642df70 to your computer and use it in GitHub Desktop.

Revisions

  1. @shaunsales shaunsales revised this gist Nov 26, 2015. No changes.
  2. @shaunsales shaunsales revised this gist Nov 26, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ExampleClass.cs
    Original file line number Diff line number Diff line change
    @@ -56,12 +56,12 @@ private void Start() // Unity Monobehaviour methods should come first
    return "hello world";
    }

    public void CheckSomething()
    public void CheckSomething() // Note that CheckSomething calls the coroutine StartCheckingSomething - we can easily infer the intended functionality
    {
    StartCoroutine(StartCheckingSomething());
    }

    private IEnumerator StartCheckingSomething()
    private IEnumerator StartCheckingSomething() // Coroutine names should begin with "Start" and if called by another method, should be easily associated
    {
    while(!this.IsWidgetReady)
    {
  3. @shaunsales shaunsales revised this gist Nov 26, 2015. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions ExampleClass.cs
    Original file line number Diff line number Diff line change
    @@ -55,4 +55,18 @@ private void Start() // Unity Monobehaviour methods should come first

    return "hello world";
    }

    public void CheckSomething()
    {
    StartCoroutine(StartCheckingSomething());
    }

    private IEnumerator StartCheckingSomething()
    {
    while(!this.IsWidgetReady)
    {
    yield return new WaitForEndOfFrame();
    }
    }
    }
    }
  4. @shaunsales shaunsales created this gist Nov 23, 2015.
    58 changes: 58 additions & 0 deletions ExampleClass.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    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";
    }
    }