声音
如果需要在游戏中播放声音,需要在某个节点添加Audio Source
组件,然后在拖一个音频文件到其AudioClip
上,如果勾选了Play On Awake
,该节点激活的时候,就开始播放声音,如果去掉够选,在在代码中播放:
// panel为添加Audio Source组件的对象
this.panel.GetComponent<AudioSource> ().Stop ();
this.panel.GetComponent<AudioSource> ().Play ();
如果需要修改音频文件则:
// "sounds/test.mp3"是相对于Resource目录的音频路径
this.panel.GetComponent<AudioSource> ().clip = Resources.Load ("sounds/test.mp3", typeof(AudioClip)) as AudioClip;
动画
要播放动画,首先需要给该节点添加一个Animation
组件,然后再在Animation窗口编辑一个动画,拖到该组件的Animations
中,当然Animations
中可以保存多个动画文件,默认播放的是Animation
中的动画,如果勾选Play Automatically
,则在节点激活时,就会自动播放Animation
中指定的动画;不勾选可以在代码中播放:
this.panel.GetComponent<Animation> ().Stop ();
// 不传AnimationName默认播放Aniamtion中的
this.panel.GetComponent<Animation> ().Play ("AnimationName");
另外注意,如果在修改当前节点的透明度,不会影响到其子节点透明度,这个更cocos不一样,如果我们需要修改的同时也修改子节点,就需要添加CavasGroup
组件,修改其alpha值
补充一个动画倒播和正放:
Animation ani = this.gameObject.GetComponent<Animation> ();
ani [aniName].speed = isReverse ? -1 : 1;
ani [aniName].time = isReverse ? ani [aniName].clip.length : 0;
ani.Play (aniName);