第四步,今天开始昨天中断的项目,控制相机的远近,它的实质Y轴上的变化,从60变化到-20,用到了transform.translate函数控制相机的运动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovieCamera : MonoBehaviour {
public float Speed = 10f;
public float endZ = -20;
void Start () {
}
void Update () {
if (transform.position.z < endZ) {
transform.Translate (Vector3.forward * Speed * Time.deltaTime);
}
}
}
我用的是Unity2017版本,所以要给场景添加雾的状态,需要通过window-lighting-setting中,找到Fog选项将其勾选。
第五步,下面我们要使用到NGUI插件,用白色图片做渐显的效果,NGUI-open-prefabbar ,Create-simppleTexture,我们使用TweenAlpha动画,这个动画中,from,to调整的就是图片的Alpha值,图片透明度,将to 设为0,增加延迟2秒,持续时间3秒,这个可以根据不同电脑的性能自己决定时间。
第六步,设置开始界面和按钮,有些图片我们需要他在开始不显示,我们可以在ColorTint中将Alph值设为0,加入第五步中用到的动画,如果要图片显示闪烁状,可以将playtype设置为Pingpong,container是同时显示的按键,按钮需要添加Boxcollider和ButtonScript组建。
第七步,处理按下任何键的事件,它的实质是在父节点找到组建,激活需要的组建,使不需要的组建沉睡。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PressAnyKey : MonoBehaviour {
private bool isAnyKeyDown = false;
private GameObject ButtonContainer;
void Start () {
ButtonContainer = this.transform.parent.Find ("ButtonContainer").gameObject;
}
void Update () {
if(isAnyKeyDown == false) {
if (Input.anyKey) {
ShowButton ();
}
}
}
private void ShowButton ()
{
ButtonContainer.SetActive (true);
this.gameObject.SetActive (false);
isAnyKeyDown = true;
}
}
第八步,处理开始按钮和加载按钮,创建OnNewGame和OnloadGame两个类,在按钮上将这两个类添加上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonContainer : MonoBehaviour {
public void OnNewGame ()
{
PlayerPrefs.SetInt ("DataFromSave", 0);
}
public void OnLoadGame ()
{
PlayerPrefs.SetInt ("DataFromSave", 1);
}
}
第九步,场景背景声音与按钮点击声音添加,MainCamera使用AudioSoure组建,勾选Loop,调整声音大小,按钮则要添加UI提供的PlaySound脚本。
今天就先到这里,休息一下。