一、脚本的生命周期回调
Cocos Creator 为组件脚本提供了生命周期的回调函数。用户只要定义特定的回调函数,Creator 就会在特定的时期自动执行相关脚本,用户不需要手工调用它们。
- onLoad(Unity是Awake)
组件脚本的初始化阶段,我们提供了 onLoad 回调函数。onLoad 回调会在节点首次激活时触发,比如所在的场景被载入,或者所在节点被激活的情况下。 在 onLoad 阶段,保证了你可以获取到场景中的其他节点,以及节点关联的资源数据。 onLoad 总是会在任何 start 方法调用前执行,这能用于安排脚本的初始化顺序。 通常我们会在 onLoad 阶段去做一些初始化相关的操作。
- start(同Unity)
start 回调函数会在组件第一次激活前,也就是第一次执行update之前触发。 start 通常用于初始化一些中间状态的数据,这些数据可能在 update 时会发生改变,并且被频繁的 enable 和 disable。
- update(同Unity)
游戏开发的一个关键点是在每一帧渲染前更新物体的行为,状态和方位。这些更新操作通常都放在 update回调中。
- lateUpdate(同Unity)
update 会在所有动画更新前执行,但如果我们要在动效(如动画、粒子、物理等)更新之后才进行一些额外操作,或者希望在所有组件的update都执行完之后才进行其它操作,那就需要用到 lateUpdate 回调。
- onDestroy(同Unity)
当组件的 enabled 属性从 false 变为 true 时,或者所在节点的 active 属性从 false 变为 true时,会激活 onEnable 回调。倘若节点第一次被创建且 enabled 为 true,则会在 onLoad 之后,start 之前被调用。
- onEnable(同Unity)
当组件的 enabled 属性从 true 变为 false 时,或者所在节点的 active 属性从 true 变为 false时,会激活 onDisable 回调。
- onDisable(同Unity)
当组件或者所在节点调用了 destroy(),则会调用 onDestroy 回调,并在当帧结束时统一回收组件。
二、API对比
Unity | CocosCreator | ||
访问节点和组件 | 获得组件所在的节点 | transform | this.node |
获得其它组件 | transform.getcomponent<>(); | this.getComponent(); | |
查找子节点 | transform.Find | this.node.getChildByName | |
全局名字查找 | gameObject.Find | cc.find | |
激活/关闭节点 |
gameObject.setActive(false); | this.node.active = false; |
|
更改节点的父节点 |
transform.setParent | this.node.parent = parentNode; | |
添加子节点 | this.node.addChild(); |
||
索引节点的子节点 |
this.node.children //返回节点的子节点数组 his.node.childrenCount |
||
Transform | 更改节点位置 |
transform.localPosition= | this.node.setPosition(100, 50); this.node.x=100; this.node.position = cc.v2(100, 50); |
更改节点旋转 |
transform.rotation= | this.node.rotation = 90; this.node.setRotation(90); |
|
更改节点缩放 |
this.node.scaleX = 2; this.node.setScale(2); |
||
Rect Transform | 更改节点尺寸 |
rectTransform.width | this.node.setContentSize(100, 100); this.node.width = 100; |
更改节点锚点位置 |
this.node.anchorX = 1; this.node.setAnchorPoint(1, 0); |
||
数学 | 向量 | Vector2/3/4 | cc.v2; |
颜色和不透明度 | 颜色和不透明度 |
spriteRenderer.color=Color.red; text.alpha=1; |
mySprite.node.color = cc.Color.RED; mySprite.node.opacity = 128; |
节点生命 | 创建新节点 |
new cc.Node() | |
克隆已有节点/创建预制节点 |
Instantiate | cc.instantiate |
|
销毁节点 |
Destroy(gameObject); | node.destroy() cc.isValid |
|
Scene Management |
加载和切换场景 |
SceneManager.LoadScene(); | cc.director.loadScene("MyScene"); |
常驻节点 |
DonotDestroy(); | cc.game.addPersistRootNode(myNode); cc.game.removePersistRootNode(myNode); |
|
场景加载回调 |
cc.director.loadScene("MyScene", onSceneLaunched);//由于回调函数只能写在本脚本中,所以场景加载回调通常用来配合常驻节点,在常驻节点上挂载的脚本中使用。 |
||
预加载场景 |
cc.director.preloadScene("table", function () { cc.log("Next scene preloaded"); }); cc.director.loadScene("table"); |
||
获取和加载资源 |
资源属性的声明 |
public Sprite mySprite=null; | mySprite: cc.Texture2D =null; |
资源属性的加载-动态加载. |
Resources.Load<Sprite>(" "); | cc.loader.loadRes(" ",cc.SpriteFrame); //.必须放置在 resources 文件夹或它的子文件夹下。resources 需要在 assets 文件夹中手工创建,并且必须位于 assets 的根目录(二者相同). //Creator 资源动态加载的时候都是异步的,需要在回调函数中获得载入的资源。 |
|
资源释放 |
cc.loader.releaseRes("test assets/image", cc.SpriteFrame); cc.loader.releaseRes("test assets/anim"); cc.loader.releaseAsset(spriteFrame); |
||
加载远程资源和设备资源 |
cc.loader.load(remoteUrl); | ||
监听和发射事件 |
监听事件 | this.node.on('mousedown',function(event)){ console.log('Hello!'); } |
|
关闭监听 | this.node.off('foobar',this._sayHello,this); | ||
发射事件 | this.node.emit('say-hello','Hello,this is Cocos Creator'); | ||
派送事件 | this.node.dispatchEvent(); event.stopPropagation(); |
||
系统内置事件 | Input.getKeyDown() Input.getMouseButtonDown() Input.getAxis() |
鼠标、触摸、键盘、重力传感 cc.Node.EventType.MOUSE_DOWN cc.Node.EventType.TOUCH_START cc.SystemEvent.EventType.KEY_DOWN cc.SystemEvent.EventType.DEVICEMOTION |
|
动作系统 | 简介 | Rigidbody | Action //.制作简单的形变和位移动画 |
移动 | rigidbody.AddForce() rigidbody.velocity CharcterController.SimpleMove() //.transform.Translate() |
var action = cc.moveTo(2,100,100);//先创建再播放 node.runAction(action);//有点像动画 node.stopAction(action); node.stopAllAction(); |
|
Tag | gameObject.FindWithTag(); if(collider.tag==" "){} |
var ACTION_TAG=1; //给动作添加tag,并借此控制 action.setTag(ACTION_TAG); node.getActionByTag(ACTION_TAG); node.stopActionByTag(ACTION_TAG); |
|
动作分类 | 时间间隔动作:cc.moveTo / cc.rotateBy / cc.scaleTo / cc.skewTo / cc.jumpBy / cc.follow /cc.bezierTo / cc.blink / cc.fadeTo / cc.tintTo / cc.delayTime / cc.reverseTime 即时动作:cc.callFunc / cc.hide / cc.show /cc.flipX / cc.place 【容器动作】 顺序动作:var seq= cc.sequence(cc.moveBy(),cc.moveBy()); 同步动作:var spawn =cc.spawn(cc.moveBy(),cc.scaleTo()); 重复动作:var seq=cc.repeat(cc.sequence(),5); 永远重复动作: var seq=cc.repeatForever(cc.sequence()); 速度动作:var action = cc.speed(cc.spawn(),0.5); 动作回调:var finished =cc.callFunc(function (){ },this, opt); |
||
计时器 | 开始一个计时器 | IEnumerator MyFunc(){ yield return new WaitForSeconds(5f); } Invoke(" ",1f); InvokeRepeat(" ", 5f); |
component.schedule( function() { } ,5 );//.每隔5s执行一次 var interval=5; var repeat=3; var delay=10; componet.schedule(function(){ }, interval , repeat , delay); //.10s后开始计时,每5s执行一次回调,重复3次 componet.scheduleOnce( function(){},2);//.只执行一次 |
取消计时器 | this.callback=function() { if(this.count===5){ this.unschedule(this.callback); } this.count++; }//.每次执行count++,判断count是多少 component.schedule(this.callback,1); |
||