1.新建项目文件夹,新建3D项目选择该文件夹,然后在项目视图创建好需要的目录结构(比如Scripts、Materials、Audios、Textures、Scenes)
导入图片到Textures目录,将texture type都设置为gui的,将format改为true color(优化的时候设置compressed)
新建一个Material bg,选择shader Unlit/Transparent(这是显示透明图片用的),然后在texture里选择背景图片。
...
using UnityEngine;
using System.Collections;
public class Bird : MonoBehaviour {
public int frameNumber = 10;
public int frameCount = 0;
public float timer = 0.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer >= 1.0f / frameNumber) {
++frameCount;
timer -= 1.0f / frameNumber;
// update material
int frameIndex = frameCount % 3;
float offset = (float)frameIndex / 3;
this.GetComponent().material.SetTextureOffset(
"_MainTex", new Vector2(offset, 0.0f));
}
}
}
7.管道设置随机位置:
void Start () {
RandomPos();
}
public void RandomPos() {
float y = Random.Range(-0.4f, -0.1f);
Vector3 pos = this.transform.localPosition;
this.transform.localPosition = new Vector3(pos.x, y, pos.z);
}
8.添加物理
添加physics->rigidbody,去掉重力(use gravity),设置速度:
this.GetComponent().velocity = new Vector3(5, 0, 0);
可以把rigidbody中的constraints的freeze position的Z勾上,则z不会改变。
然后添加一个sphere collider,并设置其半径radius到一定大小。
给管道、地面 添加碰撞box collider..
9.创建关卡
将bg做成prefab:新建一个Prefabs文件夹,将bg游戏对象拖到文件夹去就生成prefab了。
然后生成生成物体:bg1 bg2 bg3.
给main camera添加一个脚本:GameMgr(先实现单例,将firstBg设置为bg3
)
public class GameMgr : MonoBehaviour {
public Transform firstBg;
public static GameMgr instance;
void Awake() {
instance = this;
}
}
给bg1添加一个空的game object叫MoveTrigger,给添加一个BoxCollider,其size的x设为0.1,然后移动到bg2的中间,表示小鸟碰撞的那儿触发移动。
然后添加脚本MoveTrigger(将小鸟的tag设置为Player,注意MoveTrigger的collider要设置为is trigger,不然不会触发!):
public class MoveTrigger : MonoBehaviour {
public Transform curBg;
public void OnTriggerEnter(Collider other) {
//print("on trigger enter");
if (other.tag == "Player") {
// move the bg
Vector3 pos = curBg.localPosition;
Vector3 firstPos = GameMgr.instance.firstBg.transform.localPosition;
curBg.localPosition = new Vector3(firstPos.x + 10, pos.y, pos.z);
GameMgr.instance.firstBg = curBg;
}
}
}
然后将bg1拖给curBg,然后prefab apply一下,就3个都改变了。
再让pipe1和pipe2重新设置下位置:
public Pipe pipe1;
public Pipe pipe2;
...
pipe1.RandomPos();
pipe2.RandomPos();
9.控制小鸟
// left mouse
if (Input.GetMouseButton(0)) {
v = rigidBody.velocity;
rigidBody.velocity = new Vector3(v.x, 5, v.z);
}
在Main Camera上添加一个FollowBird脚本:
public class FollowBird : MonoBehaviour {
private GameObject bird;
private Transform birdTransform;
// Use this for initialization
void Start () {
bird = GameObject.FindGameObjectWithTag("Player");
birdTransform = bird.transform;
}
// Update is called once per frame
void Update () {
Vector3 birdPos = birdTransform.position;
this.transform.position = new Vector3(birdPos.x + 6.36f, birdPos.y - 2.53f, -10);
}
}
10.添加得分
在给pipe1添加box collider,大小设置为上下管道中间区域,然后在Pipe脚本里添加:
public void OnTriggerExit(Collider other) { // enter, exit, stay
if (other.tag == "Player") {
// add score
++GameMgr.instance.score;
}
}
// test
void OnGUI() {
GUILayout.Label("Score:" + GameMgr.instance.score);
}
...
11.发布android
file - project settings - player settings要设置bundle id,然后add open scenes,再build and run
12.声音
在Main Camera上添加audio source,选择
sfx_swooshing,勾选play on awake,则只播放一次。
给bird添加一个audio source - wing,去掉play on awake,在左键按下的地方添加代码:
在pipe_up/pipe_down都添加audio source - hit,添加代码。
要区分则添加AudioSource变量来播放。
13.结束界面
选择score图片添加gui texture(unity5我用ui rawimage替代)
添加文字gui text,再添加UIButton表示start按钮
将这些ui的父节点改名为menu,添加GameMenu脚本管理:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameMenu : MonoBehaviour {
public static GameMenu instance;
public Text now;
public Text best;
void Awake() {
instance = this;
// hide
this.gameObject.SetActive(false);
}
public void UpdateScore(float score) {
float highScore = PlayerPrefs.GetFloat("score", 0);
if (score > highScore) {
highScore = score;
PlayerPrefs.SetFloat("score", highScore);
}
now.text = score.ToString();
best.text = highScore.ToString();
}
}
在start按钮的OnClick里调用GameMenu的OnClick方法: