unity下2d和3d混合使用教程,外掛為OrtHello (一)
我是從cocos2d 轉移陣營過來的 unity下開發2d遊戲真是不習慣。。。特別是2d中有許多特性是非常必要的,但是unity本身又支援得非常爛的...所以這時候就要用過第3方外掛,我試過EZGUI, sm2,gm2,還有一個忘了名字的2d外掛,但是都不符合我的要求,在我百無聊賴中終於看見了它——OrtHello ——>_<我要一個能很好的結合itweens(動作引擎),又能和3d場景混合,還要和cocos2d有那麼丁點相像,最後還能利用一些cocos2d專案中的資源(其實OrtHello 要用的話要把plist轉成xml,這本身也是蛋碎的事情)
好吧,我搜了一遍幾乎沒有OrtHello 的中文教材,連英文的教材也十分稀有,我甚至懷疑沒有人用過它來做遊戲——但是這並不妨礙它成為一個遊戲的引擎
因為我要做一個類似東方的彈幕遊戲,所以可能不會研究除了彈幕遊戲之外的一些特性,於是寫下幾篇我的心得作為拋磚引玉之用=>
http://www.wyrmtale.com/products/unity3d-components/orthello
官方網站,本體是免費的,還有一個收費版的,但是我看了一下對我貌似沒啥用所以就忽略了,如果是獨立遊戲開發者的話買之可能還是有必要的。
下完之後匯入,可看到Orthello目錄下有三個資料夾Examples,Objects 和 Standard Assets
如果你的專案非常依賴這個引擎的話建議把 Standard Assets 放到根目錄的unity最先編譯的三個目錄中-,-這你都不懂別問我了,我是建議和iTweens一起到Plugins中
Examples是例子可以刪去的...Objects 是預設Prefabs物體,是十分重要的。
先開啟第一個例子
執行後可以看到效果是一個填充的圖片填滿了整個螢幕,有明顯層次感,然後滑鼠點哪就去哪
首先我們理解一個orthello的執行方式,它必須要在場景中有一個OT的東西,我們還可以看到OT下有4個子節點,分別是Animations,Containers,Prototypes,View
Animations 是對Containers中的spritesheet (- -姑且讓我這麼叫,其實應該是Texture)進行動作管理,就是定義從哪一幀到哪一幀是什麼動作
Containers 是放置Texture的地方,其實你不用對材質有過多要求的話,transparent這個材質應該已經夠你用了
Prototypes 是設定的預設的OT物體而使用的
View 是控制2d場景中的攝像機用的
順便貼一下原始碼
public class CExample1 : MonoBehaviour
{
bool initialized = false; // initialization notifier
bool scrolling = false; // scrolling notifier
// This method will resize the a FilledSprite ( provided by name )
// to match the current view (resolution).
void Resize(string spriteName)
{
// Lookup the FilledSprite using its name.
OTObject sprite = OT.ObjectByName(spriteName);
if (sprite != null)
{
// We found the sprite so lets size it to match the screen's resolution
// We will assume the OTView.zoom factor is set to zero (no zooming)
sprite.size = new Vector2(Screen.width, Screen.height);
}
}
// application initialization.
// This method is called from the Update() function so we can be sure that
// all Orthello objects have been started.
void Initialize()
{
// resize filled sprites to match screen size
Resize("filled 1");
Resize("filled 2");
Resize("filled 3");
Resize("filled 4");
// set initialized notifier to true so we only initialize once.
initialized = true;
}
// Set scroll speed for a specific FilledSprite providing its name
void SetScroll(string spriteName, Vector2 scrollSpeed)
{
// lookup sprite
OTObject sprite = OT.ObjectByName(spriteName);
// set scroll speed
if (sprite != null)
(sprite as OTFilledSprite).scrollSpeed = scrollSpeed;
}
// Update is called once per frame
void Update () {
// Only go on if Orthello is valid.
if (!OT.isValid) return;
// check if we have to initialize
if (!initialized)
Initialize();
// only scroll when left mouse button pressed
if (Input.GetMouseButton(0))
{
// get delta position relative to screen center
Vector2 delta = (Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0));
delta = new Vector2(delta.x / Screen.width, delta.y / Screen.height);
// set scroll speed of layers - the more backwards the less scroll
SetScroll("filled 1", delta * 60);
SetScroll("filled 2", delta * 50);
SetScroll("filled 3", delta * 40);
SetScroll("filled 4", delta * 30);
scrolling = true;
}
else
{
// check if we are scrolling
if (scrolling)
{
// stop scrolling
SetScroll("filled 1", Vector2.zero);
SetScroll("filled 2", Vector2.zero);
SetScroll("filled 3", Vector2.zero);
SetScroll("filled 4", Vector2.zero);
scrolling = false;
}
}
}
}
首先理解一下OT的初始化方式,它並不是通過放在start中進行初始化的,而是放在update中,通過一個bool來判斷是否已經初始化,如果你放到了start中肯定會導致物體找不到OT的-,- ,ok ,那麼在此之前還要判斷一下
OT.isValid
如果要尋找一個ot object請使用ot。objectbyname的方式,這樣會搜尋得更快
----------------------------------------------------------------------------我是傳說中的分割線------------------------------------------------------------
說完了上半節該說下半節了,首先推薦大家不要用Unity自帶的GUI,因為那不僅不好用而且容易造成各種排列錯誤orz
推薦大家還是用投影攝像機,照一個面,最後將2d攝像機和3d攝像機的合成
2DLayer->放置2D物體,是main carema,並且受到OT的view的影響。<-投影攝像機
3DLayer->放置3D物體,可以自己控制,可用來做場景的背景什麼的 <- 3d攝像機
通常來說如果不需要3d和2d混搭,那麼只需OT的view控制就可以了<-它會自動找到場景的main carema,然後進行一些設定,這些設定基本上已經定死了沒什麼要改的,
唯一可以改的地方就是找到OTView
然後找到piexl prefect resolution
將x和y設定成你想要的大小,然後去掉勾ALways piexl prefect 這樣子在任何尺寸下都會進行縮放的比例適應螢幕,
如果勾上ALways piexl prefect 則是按照真實的畫素投影到螢幕上,並且中心點是螢幕的正中心(0,0)
上面那個我的例子就是這樣做的,我打算放到iphone4中,所以螢幕解析度大小為-320~~320,豎著是-480~~480,中心點是0,0,這樣在3d中物體比較好擺放,不好的地方就是從觸控座標系到實際面座標系要進行轉化,