본문으로 건너뛰기

· 약 1분
karais89

유니티에서 가비지 컬렉터 문제로 성능 저하가 생길 수 있습니다.

자주 삭제 되고 생성되는 오브젝트의 경우 메모리풀의 사용으로 성능을 향상 시킬 수 있습니다.

· 약 7분
karais89

01. 싱글톤 패턴

싱글톤 패턴이란?

싱글톤 패턴은 가장 많이 알려진 패턴 중 하나입니다.

본질적으로 싱글톤은 자신의 단일 인스턴스만 생성될 수 있게 해주는 클래스이며 일반적으로 해당 인스턴스에 대한 간단한 액세스를 제공합니다.

일반적으로 유니티에서 매니저 클래스나 컨트롤러 클래스를 싱글톤으로 만들어서 관리해 주는 편입니다.

유니티에서의 싱글톤은 2가지 버전으로 구분할 수 있습니다.

  1. C# 싱글톤 버전
  2. Monobehaviour 버전

기존에는 싱글톤 패턴을 사용할 때 내가 편한 방식으로 사용을 했었고 가장 간단한 방식을 선호 했었는데. 조금은 안전한 싱글톤 패턴을 사용하기 위해서 고민이 필요할 것 같다.

1. C# 싱글톤 버전

스레드 세이프 하지 않은 버전

// Bad code! Do not use!
public sealed class Singleton
{
private static Singleton instance=null;

private Singleton()
{
}

public static Singleton Instance
{
get
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}

간단히 구현된 스레드 세이프 한 버전

public sealed class Singleton
{
private static Singleton instance = null;
private static readonly object padlock = new object();

private Singleton()
{
}

public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
}

스레드 세이프한 방법을 사용하자.

제네릭을 사용한 방법

using System;  
using System.Reflection;

public class Singleton<T> where T : class
{
private static object _syncobj = new object();
private static volatile T _instance = null;

public static T Instance
{
get
{
if (_instance == null)
{
CreateInstance();
}
return _instance;
}
}

private static void CreateInstance()
{
lock (_syncobj)
{
if (_instance == null)
{
Type t = typeof(T);

// Ensure there are no public constructors...
ConstructorInfo[] ctors = t.GetConstructors();
if (ctors.Length > 0)
{
throw new InvalidOperationException(String.Format("{0} has at least one accesible ctor making it impossible to enforce singleton behaviour", t.Name));
}

// Create an instance via the private constructor
_instance = (T)Activator.CreateInstance(t, true);
}
}
}
}

리플렉션 기능을 사용하여 생성된 인스턴스 수를 체크 후 1개 이상이면 익셉션 에러를 띄운다.

2. Monobehaivour 버전

간단한 방법

using UnityEngine;
using System.Collections;

public class Singleton: MonoBehaviour
{
public static Singleton instance = null; //Static instance of GameManager which allows it to be accessed by any other script.

//Awake is always called before any Start functions
void Awake()
{
//Check if instance already exists
if (instance == null)
{
//if not, set instance to this
instance = this;
}
//If instance already exists and it's not this:
else if (instance != this)
{
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject);
}

//Sets this to not be destroyed when reloading scene
DontDestroyOnLoad(gameObject);
}
}

Generic을 사용한 방법

using UnityEngine;
using System.Collections;

/// <summary>
/// Be aware this will not prevent a non singleton constructor
/// such as `T myT = new T();`
/// To prevent that, add `protected T () {}` to your singleton class.
/// As a note, this is made as MonoBehaviour because we need Coroutines.
/// </summary>
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T _instance = null;
private static object _syncobj = new object();
private static bool appIsClosing = false;

public static T Instance
{
get
{
if (appIsClosing)
return null;

lock (_syncobj)
{
if (_instance == null)
{
T[] objs = FindObjectsOfType<T>();

if (objs.Length > 0)
_instance = objs[0];

if (objs.Length > 1)
Debug.LogError("There is more than one " + typeof(T).Name + " in the scene.");

if (_instance == null)
{
string goName = typeof(T).ToString();
GameObject go = GameObject.Find(goName);
if (go == null)
go = new GameObject(goName);
_instance = go.AddComponent<T>();
}
}
return _instance;
}
}
}

/// <summary>
/// When Unity quits, it destroys objects in a random order.
/// In principle, a Singleton is only destroyed when application quits.
/// If any script calls Instance after it have been destroyed,
/// it will create a buggy ghost object that will stay on the Editor scene
/// even after stopping playing the Application. Really bad!
/// So, this was made to be sure we're not creating that buggy ghost object.
/// </summary>
protected virtual void OnApplicationQuit()
{
// release reference on exit
appIsClosing = true;
}
}

사용 방법

public class Manager : Singleton<Manager> {
protected Manager () {} // guarantee this will be always a singleton only - can't use the constructor!

public string myGlobalVar = "whatever";
}

Generic을 사용한 방법에서는 Singleton을 상속받은 클래스에서는 생성자를 반드시 protected로 선언을 해서 외부에서는 생성이 되지 않게 막는다.

그리고 Singleton 클래스의 applicationIsQuitting 변수의 경우 별로 깨끗하지 않은 방법인것 같지만.. 유니티 에디터 상에서 갑자기 나가버리는 경우에 에러가 발생하는 경우라 반드시 필요한 변수이다. 이렇게 로직을 처리할 시에는 instance가 null이 나오는 경우가 생기므로 null 처리를 따로 처리해줘야 된다.

만약에 씬이 변환 되도 파괴되지 않은 싱글톤을 만들고 싶을 시에는 상속받은 클래스의 awake 함수에 아래와 같이 선언한다.

DontDestroyOnLoad(this.gameObject); 의 함수를 실행시켜 주자.

참조

· 약 6분
karais89

프로그래밍 실력 향상 방법

관련 링크들 요약 및 정리

추후 관련 자료들이 더 있으면 추가할 예정.

프로그래밍 스킬을 향상시키는 10가지 방법

  1. 새로운 프로그래밍언어를 배워라.
    • Lisp, Forth, PostScript or Factor, J, Haskell, Prolog, Erlang 등
  2. 좋은 프로그래밍 책을 읽어라.
    • The Art of Computer Programming
    • Structure and Interpretation of Computer Programs (SICP)
    • A discipline of Programming or the famous dragon book.
  3. 오픈소스 프로젝트에 참여하라
    • GitHub, Sourceforge, gitorious, BitBucket or Ohloh.
  4. 프로그래밍 퍼즐(문제)을 풀어라.
  5. 프로그램을 작성하라.
  6. 코드를 읽고 공부하라.
    • Linux Kernel (리눅스 커널)
    • MINIX3는 학습하기에 아주 좋은 운영체제 입니다.
  7. 프로그래밍 관련 웹 사이트 혹은 블로그를 방문해라.
  8. 프로그래밍에 대한 블로그를 작성하라.
    • Q&A같은 것을 통해 질문을 받고 답을 해주기 바랍니다.
    • 튜토리얼을 작성해보세요.
  9. 로우레벨 프로그래밍을 배워라.
    • C
    • assembler
    • 컴퓨터의 기원
    • 운영체제
    • 임베디드 시스템
    • 운영체제 드라이버 개발
  10. 프로그램이 작동하지 않는가? 도움을 받으려 하지 말고 스스로 생각하여라.

동기에게 보내는 편지 : 프로그래밍 실력을 향상 시키는 방법

초보에서 고수로 가는 길.

  1. Code Complete 2/E
  2. Refactoring
  3. 프로그래밍 수련법
  4. 실용주의 프로그래머
  5. 생각하는 프로그래밍

위의 책은 순서에 상관없이 읽어야 되는 책 입니다. 3번부터 보고 다른 것을 읽는 것을 추천 합니다.

게임 프로그래머 실력향상법 자료조사

  1. GitHub와 포트폴리오
    • 오픈 소스 프로젝트 참여 및 자신의 프로젝트 제작
  2. 세미나
  3. 게임 개발자 커뮤니티
  4. 기초부터 단단히
    • 알고리즘
    • 자료구조
    • C#
    • Unity
    • 컴퓨터 그래픽스
  5. 게임 개발 서적 추천
    • C++ 기초 플러스
    • 뇌를 자극하는 C# 5.0 프로그래밍
    • 유티티 4 게임 개발의 정석
    • 따라 하면서 배우는 NGUI 유니티 2D 게임 프로그래밍
    • 유니티 네트워크 프로그래밍
    • 좋은 프로그램을 만드는 핵심 원리 25가지
    • 성공과 실패를 결정하는 1%의 프로그래밍 원리
    • 게임 프로그래밍의 정석
    • 리팩토링
    • Head First Design Pattern
    • Game Programming Gems 시리즈
    • 실용주의 프로그래머
    • 위대한 게임의 탄생 시리즈
    • CODE COMPLETE
    • Debug It! 실용주의 디버깅
    • Effect C++
    • 셰이더 프로그래밍 입문
    • DirectX9를 이용한 3D Game 프로그래밍 입문
    • 프로그래밍 면접 이렇게 준비한다
    • 익스트림 프로그래밍
    • 열혈 C 프로그래밍
    • 3D 게임 프로그래밍
    • Programming Game AI by Example
    • 게임 프로그래머를 위한 기초 수학과 물리
    • STL 튜토리얼 레퍼런스 가이드
    • 나는 프로그래머다.

관련 글

· 약 1분
karais89

현재까지 구현한 목록

  • 페이지네이션 기능
  • 태그로 검색 기능
  • bigfoot 각주 이쁘게 하는 기능
  • Disqus 코멘트 기능
  • Google Search Engine
  • sitemap, robots 추가.

구현 할 목록

  • seo 최적화
  • 전체 글 목록(아카이브)
  • 태그 클라우드
  • 외부 링크를 새탭에서 열기
  • 목차 기능
  • 검색 창 달기
  • 도메인 연결

대략적인 뼈대는 갖춘 상태이고, 구현 할 목록들만 빠르게 구현 한 후 포스팅을 할 예정이다.

· 약 1분
karais89

jekyll에서 댓글을 달 수 있는 기능을 추가 하려고 함.

  1. disqus 회원 가입 후 인증 메일로 인증까지 완료
  2. 현재 jekyll의 기본 테마인 minima를 사용하고 있어서 config 파일 수정

간단하게 코멘트를 달 수 있다.

처음에 disqus를 달았을때 에러가 발생함.

config.yml의 url을 입력해주니 에러가 해결 됨.

· 약 1분
karais89

각주 테스트1

You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve, which launches a web server and auto-regenerates your site when a file is updated.

To add new posts, simply add a file in the _posts directory that follows the convention YYYY-MM-DD-name-of-post.ext and includes the necessary front matter. Take a look at the source for this post to get an idea about how it works.

Jekyll also offers powerful support for code snippets:

{% highlight ruby %} def print_hi(name) puts "Hi, #{name}" end print_hi('Tom') #=> prints 'Hi, Tom' to STDOUT. {% endhighlight %}

Check out the Jekyll docs for more info on how to get the most out of Jekyll. File all bugs/feature requests at Jekyll’s GitHub repo. If you have questions, you can ask them on Jekyll Talk.


  1. 각주 테스트 용