調整 OSX Yosemite 的 ulimit

OSX 每次改版,改 ulimit 的方式都會改一次… 以前是 launchd.conf、sysctl 想不到到了 Yosemite ,要改 ulimit 竟然要寫 plist… 而且這方法還是在 Riak 文件找到的。Open Files Limit。要改的有兩個檔案:

/Library/LaunchDaemons/limit.maxfiles.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
    <dict>
      <key>Label</key>
        <string>limit.maxfiles</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxfiles</string>
          <string>65536</string>
          <string>65536</string>
        </array>
      <key>RunAtLoad</key>
        <true/>
      <key>ServiceIPC</key>
        <false/>
    </dict>
  </plist>

/Library/LaunchDaemons/limit.maxproc.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple/DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  <plist version="1.0">
    <dict>
      <key>Label</key>
        <string>limit.maxproc</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxproc</string>
          <string>2048</string>
          <string>2048</string>
        </array>
      <key>RunAtLoad</key>
        <true />
      <key>ServiceIPC</key>
        <false />
    </dict>
  </plist>

然後重開機就可以了…

Operational Transformation 在 JavaScript 上的實作

最近因緣際會下接觸到 Operational Transformation 這東西… OT 是 Google Wave、Hackpad、Etherpad 等等即時協作工具背後的基礎。Wikipedia 的說明依然是有些抽象,想了解一些基本觀念的話,這篇會是不錯的起點。

根據前 Google Wave 工程師的說法:實作 OT 並不是一件容易的事情。當初他們花了兩年寫出 Google Wave,如果搬到今天,可能也還是需要兩年。幸好,已經有一些現成的 OT 實作可以參考:

ShareJS

http://sharejs.org/

ShareJS 就是前面那位前 Google Wave 工程師的作品。相較於其他函式庫,ShareJS 走的是全端全包的路線。從跟前端編輯器的整合各式 OT 演算法的定義與實作、直接就可使用的整套同步系統、甚至還有自己自製的 Database Frontend

不過,寫這篇文章的時候,ShareJS 正在進行 0.7 版的改版,文件跟程式碼有些混亂,不太容易理解…

ot.js

https://github.com/Operational-Transformation/ot.js/

ot.js 是另一套 OT 在 js 上的實作,包含 OT type 與基本的 server/client 的實作。跟 ShareJS 比起來較單純,也比較好懂。缺點是文件很少,範例似乎也有些 bug。

btw, firepad 是基於這套做出來的。

changesets

https://github.com/marcelklehr/changesets

我目前使用的是這套實作,它只實作了針對文字的 OT ,並不包含 server/client 等等其他元件。不過,他的文件應該是最清楚的,也支援從 diff 建立 OT 。

 

我想 OT 在這幾年應該會有不少有趣的應用?反正現在玩玩也不吃虧 :P

 

用 vim 開發 Unity3d (C#)

vim-unity3d

 

感謝 YouCompleteMe 這個超神 vim plugin,終於可以不用忍受 monodevelop 的龜速,改用 vim 啦。設定步驟如下:

  1. 用你喜歡的 vim plugin 安裝方式安裝 YouCompleteMe
  2. 至 YouCompleteMe 安裝目錄下執行 ./install.sh –omnisharp-completer
  3. 將你的 Unity3d Project 目錄底下的 PROJECT_NAME.sln 刪除,只留下 PROJECT_NAME-csharp.sln
  4. 完成!

gotcha:

  • 剛開啟 vim 後的第一次補完會失敗,第二次才會開始運作。

Platform Specific Sprite Collection with Unity 2dToolkit

When working on platforms with different resolutions, it’s common to have assets prepared for them with different sizes. For example, you might have a 320×480(1x) background image for iPhone 3GS, 640×960(2x) for iPhone 4G retina display and 1280*1920 for iPad3. With 2dToolkit, managing these assets become a breeze. In this tutorial, we’re going to use 2dToolkit’s Platform Specific Sprite Collection and Camera Override to solve this problem.

Setup your scene with 1x assets

When swapping sprites with Platform Specific Sprite Collection, 2dToolkit will maintain the size of sprite as the same. So it’s better to setup your scene with 1x sprites.

Import your high-quality assets with correct asset path

Make sure all assets in the sprite collection have corresponding high-quality version(2x, 4x) and correct file path. Check documentation for more information.

Enable multiple platform support of sprite collection

Just follow the “Ideal Workflow" in the documentation

Setup a empty scene before other scenes in your game

In order to make 2dToolkit swap the sprite collection to the one with better quality assets, you have to tell it to do so BEFORE any sprite being loaded into memory. Therefore, the easiest way to do so is prepare an empty scene before your game and put the following script into it:

using UnityEngine;
using System.Collections;

public class ResolutionSetting : MonoBehaviour {
	void Awake () {
		tk2dSystem.CurrentPlatform = "2x"; // Or 4x, based on the resolution of your target device
		Application.LoadLevel("YOUR STARTING SCENE");
	}
}

Setup the tk2dCamera resolution override

tk2dCamera can automatically scale the game to fit the resolution of target device. You can set it up like mine or trying out different settings by yourself:

LittleSnapper

Since we’re setting up the game with assets having the lowest quality, we set the Native Resolution to the target resolution of your lowest quality assets. In this case, It’s 320×480.

The most important setting is the “Pixel Perfect Fit" of Auto Scale. Combine with platform specific sprite collection, it will maintain the pixel perfect of your game and stretch the game’s resolution to fit the screen, leaving some letterbox space if not possible.

Bonus: enable Force Editor Resolution to show the correct resolution in unity’s game view

tk2dCamera comes with a setting called “Force Editor Resolution“. Unity’s game view won’t display in the correct resolution if you don’t enable it.

在Unity3D中實作Diablo3的單位白色閃爍

Blizzard最近雖然在Game Design上的水準讓人不敢恭維,他們的工程能力倒是依然強大。在Diablo3裡,他們用了招簡單有效的方式,讓攻擊怪物的打擊感大幅提高:讓怪物在被打到的瞬間閃一下白色。

要怎樣在Unity3D中實作這種效果呢?其實只要透過一個簡單的Shader就可以了:

Shader "Custom/White" {
   Properties {
	    _Color ("Color", Color) = (1,1,1)
	}
 
 	SubShader {
	    Color [_Color]
	    Pass {}
	} 
	FallBack "Diffuse"
}

然後在怪物被打到的時候執行:

renderer.material.shader = Shader.Find("Custom/White");
yield return new WaitForSeconds(0.1f);
renderer.material.shader = Shader.Find("Diffuse");

就可以了