unity下2d和3d混合使⽤教程,插件为OrtHello(⼀)
我是从cocos2d 转移阵营过来的 unity下开发2d游戏真是不习惯。。。特别是2d中有许多特性是⾮常必要的,但是unity本⾝⼜⽀持得⾮常烂的...所以这时候就要⽤过第3⽅插件,我试过EZGUI, sm2,gm2,还有⼀个忘了名字的2d插件,但是都不符合我的要求,在我百⽆聊赖中终于看见了它——OrtHello ——>_<;我要⼀个能很好的结合itweens(动作引擎),⼜能和3d场景混合,还要和cocos2d有那么丁点相像,最后还能利⽤⼀些cocos2d项⽬中的资源(其实OrtHello 要⽤的话要把plist转成xml,这本⾝也是蛋碎的事情)
好吧,我搜了⼀遍⼏乎没有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.
/
/ 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 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
unity 教程if (!initialized)
Initialize();
// only scroll when left mouse button pressed
if (Input.GetMouseButton(0))
{
// get delta position relative to screen center
Vector2 delta = (usePosition - 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", );
SetScroll("filled 2", );
SetScroll("filled 3", );
SetScroll("filled 4", );
SetScroll("filled 4", );
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中物体⽐较好摆放,不好的地⽅就是从触摸坐标系到实际⾯坐标系要进⾏转化,