1.新建项目文件夹,新建3D项目选择该文件夹,然后在项目视图创建好需要的目录结构(比如Scripts、Materials、Audios、Textures、Scenes)


2.导入资源,将资源从文件夹拖入到项目视图


在AudioSource的SpatialBlend属性,0表示2D,1表示3D)


  导入图片到Textures目录,将texture type都设置为gui的,将format改为true color(优化的时候设置compressed)


3.保存场景到Scenes目录,叫Start。



4.因为是用3d开发2d游戏,所以将main camera的projection改为正交视角(orthographic)。


  创建一个方向光


  创建quad,将其位置reset。


  新建一个Material bg,选择shader Unlit/Transparent(这是显示透明图片用的),然后在texture里选择背景图片。


  然后把材质拖到quad上,就可以显示图片了。



5.在项目设置里可设置平台,比如选android,然后设置运行的游戏屏幕为横屏landscape 1280x800


  缩放bg x10, y15,它不需要碰撞,所以把collder组件删掉


  拷贝bg,重新搞一份back,把back作为bg的儿子,reset一下(Ctrl+D快捷键 复制)


  同理创建2个pipe放到一个空gameobject pipe去


  ...



6.同理创建bird,但是bird是帧图,需要修改材质:tiling表示显示的部分(0无,1表100%),offset表示偏移


  给小鸟加个脚本组件,用脚本来控制文理偏移:



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<Renderer>().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<Rigidbody>().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. 
 
 
 
)
 
 
 
  
 
 

    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.声音 
 
 
 
sfx_swooshing,勾选play on awake,则只播放一次。
 
 
  给bird添加一个audio source - wing,去掉play on awake,在左键按下的地方添加代码:
 
 

    GetComponent<AudioSource>().Play(); 
  
 
 

     在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方法: 
 
 

   public void OnClick() { 
 
 
 

           Application.LoadLevel(0); 
 
 
 

       }