美文网首页
Unity实战之方块跑酷(一)

Unity实战之方块跑酷(一)

作者: ElfACCC | 来源:发表于2018-09-16 18:59 被阅读105次

一、初始化项目开发环境

1)项目创建

2)导入NGUI插件

3)相关资源导入

4)Atlas制作(由UI中的图片导入)

二、初始屏幕自适应

原因:Android手机屏幕尺寸不一样,屏幕分辨率千奇百怪

该实战用16:9的分辨率自适应 1920*1080   1280*720  960*540  720*405

三、NGUI初步实现UI自适应

Game面板创建先使用的分辨率尺寸

创建多个分辨率 图标缩小

UIRoot组件相关设置:1)缩放类型,手机自适应 内容宽高度 填写数值,勾选fit

四、开始界面UI

初始界面UI 商城界面UI 游戏界面UI

五、地图生成算法之菱形布局

1)Resources动态加载资源

①新建资源目录“Resources”存放预制体文件。!一定要叫这个名字!

②使用Resources.Load("资源名")进行动态资源加载。

prefabs文件中的地板砖预制体拖到Resources文件夹中,创建一个空物体,讲c#脚本挂载其上

private GameObject m_prefab_tile;

void Start () {

        m_prefab_tile = Resources.Load("tile_white") as GameObject;

}

2)代码生成地图菱形布局

两层for循环嵌套生成长方形的地面效果。

Quaternion.Euler(Vector3)可以将三维向量转换为四元数。

private void CreateMapItem()

    {

        for (int i = 0; i < 10; i++)

        {

            for (int j = 0; j < 5; j++)

            {

                Vector3 pos = new Vector3(j*0.254f, 0, i*0.254f);

                Vector3 rot = new Vector3(-90, 0, 0);

                GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//实例化生成,默认位置,无旋转

                tile.GetComponent<Transform>().SetParent(m_Transform);//设置父物体

            }

        }

    }

修改摄像机参数值,正交模式,活用Transform组件右上角的copy component和paste

3)等腰直角三角形求底边长实现单排菱形平铺

等腰直角三角形求底边:2的平方根 乘以 直角边长

unity代码实现:Mathf.Sqrt(2)*n;

地板转了45°,两块地板之间距离变为斜边,调整SIZE

private void CreateMapItem()

    {

        for (int i = 0; i < 10; i++)

        {

            for (int j = 0; j < 5; j++)

            {

                Vector3 pos = new Vector3(j * Mathf.Sqrt(2) * 0.254f, 0, i * Mathf.Sqrt(2) * 0.254f);

                Vector3 rot = new Vector3(-90, 45, 0);//地板变成斜的了

                GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//实例化生成,默认位置,无旋转

                tile.GetComponent<Transform>().SetParent(m_Transform);//设置父物体

            }

        }

    }

4)菱形地图双排布局

第二排菱形位置需要偏移,偏移量为半个底边长度

//第二排

        for (int i = 0; i < 10; i++)

        {

            for (int j = 0; j < 4; j++)

            {

                Vector3 pos = new Vector3(j * bottomLength + bottomLength / 2, 0, i * bottomLength + bottomLength / 2);

                Vector3 rot = new Vector3(-90, 45, 0);

                GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//实例化生成,默认位置,无旋转

                tile.GetComponent<Transform>().SetParent(m_Transform);//设置父物体

            }

        }

调颜色

当出现高光时,把预制体中的金属度调为0

private Color color1 = new Color(124/255f, 155/255f, 230/255f);

tile.GetComponent<MeshRenderer>().material.color = color1;

 tile.GetComponent<Transform>().FindChild("normal_a2").GetComponent<MeshRenderer>().material.color = color1;//找到子物体,再找他的组件meshrender改颜色,颜色rbg要除以255加个f

两种颜色

将预制体wall,修改金属度,拖入resources文件夹中,在脚本中找到该预制体

因为墙壁没有子物体,所以FindChild这句要放在else里面。↓↓↓↓

墙壁生成完成

六、地图生成算法之数据管理

1)美化瓷砖地图

①摄像机位置与角度调整

②灯光位置与角度一集颜色调整

③墙壁模型重新定义颜色

2)地图数据存储

3)地图数据测试

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

/// <summary>

/// 地图管理器

/// </summary>

public class MapManager : MonoBehaviour {

    private GameObject m_prefab_tile;

    private GameObject m_prefab_wall;

    //地图数据存储

    private List<GameObject[]> mapList = new List<GameObject[]>();

    private Transform m_Transform;

    private float bottomLength = Mathf.Sqrt(2) * 0.254f;

    private Color color1 = new Color(124/255f, 155/255f, 230/255f);

    private Color color2 = new Color(125/255f,169/255f,233/255f);

    private Color color3 = new Color(87 / 255f, 93 / 255f, 169 / 255f);

void Start () {

        m_prefab_tile = Resources.Load("tile_white") as GameObject;

        m_prefab_wall = Resources.Load("wall2") as GameObject;

        m_Transform = gameObject.GetComponent<Transform>();

        CreateMapItem();

}

    /// <summary>

    /// 创建地图元素(段)

    /// </summary>

    private void CreateMapItem()

    {

        //单排

        for (int i = 0; i < 10; i++)

        {

            GameObject[] item = new GameObject[6];

            for (int j = 0; j < 6; j++)

            {

                Vector3 pos = new Vector3(j * bottomLength, 0, i * bottomLength);

                Vector3 rot = new Vector3(-90, 45, 0);

                GameObject tile = null;

                if (j == 0 || j == 5)

                {

                    tile = GameObject.Instantiate(m_prefab_wall, pos, Quaternion.Euler(rot)) as GameObject;//实例化生成,默认位置,无旋转

                    tile.GetComponent<MeshRenderer>().material.color = color3;

                }

                else

                {

                    tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//实例化生成,默认位置,无旋转

                    tile.GetComponent<Transform>().FindChild("normal_a2").GetComponent<MeshRenderer>().material.color = color1;

                    tile.GetComponent<MeshRenderer>().material.color = color1;

                }

                tile.GetComponent<Transform>().SetParent(m_Transform);//设置父物体

                item[j] = tile;

            }

            mapList.Add(item);

            //第二排

            GameObject[] item2 = new GameObject[5];

            for (int j = 0; j < 5; j++)

            {

                Vector3 pos = new Vector3(j * bottomLength + bottomLength / 2, 0, i * bottomLength + bottomLength / 2);

                Vector3 rot = new Vector3(-90, 45, 0);

                GameObject tile = GameObject.Instantiate(m_prefab_tile, pos, Quaternion.Euler(rot)) as GameObject;//实例化生成,默认位置,无旋转

                tile.GetComponent<MeshRenderer>().material.color = color2;

                tile.GetComponent<Transform>().FindChild("normal_a2").GetComponent<MeshRenderer>().material.color = color2;

                tile.GetComponent<Transform>().SetParent(m_Transform);//设置父物体

                item2[j] = tile;

            }

            mapList.Add(item2);

        }

    }

void Update () {

        if (Input.GetKeyDown(KeyCode.Space))

        {

            string str = "";

            for (int i = 0; i < mapList.Count; i++)

            {

                for (int j = 0; j < mapList[i].Length; j++)

                {

                    str += mapList[i][j].name;

                }

                str += "\n";

            }

            Debug.Log(str);

        }

}

}

相关文章

  • Unity实战之方块跑酷(一)

    一、初始化项目开发环境 1)项目创建 2)导入NGUI插件 3)相关资源导入 4)Atlas制作(由UI中的图片导...

  • Unity实战之方块跑酷(三)

    一、游戏奖励物品生成与交互 1)奖励物品生成 奖励物品要在正常的瓷砖物体的上方生成,奖励物品的生成概率是一个固定值...

  • Unity实战之方块跑酷(二)

    一、角色出生与基本控制移动 1)角色出生 读取地图集合中的数据,指定主角模型的出生点和旋转状态。 2)地图显示数据...

  • Java项目实战之天天酷跑(一):登录界面

    首先,写一个需求文档:一、项目名称:《天天酷跑》(RunDay) 二、功能介绍:闯关类游戏,玩家登录后,选择进入游...

  • 写一个发射子弹打方块的demo

    2019-01-19 今天写了一个发射子弹打方块的Unity demo 学习地址Unity零基础入门 - 打砖块 ...

  • unity实战之StreetRacer

    说明 这是我自学unity开发出来的赛车游戏Demo。关键的几个点,比如说车的移动,障碍物的生成,以及车的控制都已...

  • unity实战之CandyCrush

    前言 这次带来的是CandyCrush,一个三消类游戏。对于消除类游戏的起源,普遍公认的是,《俄罗斯方块》是这一游...

  • 破界新生|孙洁:墙上舞者,为逐梦而跑

    “从体操教练,到跑酷冠军梦想的脚步,从未停止!” 有人说跑酷是一项运动, 有人说跑酷是一种信仰, 有人说跑酷是一种...

  • 实战----射击方块

    在左手手柄和开始按钮上都添加碰撞盒(注意把trigger勾选上,防止与其他物体发生碰撞),在手柄上添加刚体(关闭重...

  • [unity3d源码] Unity3D 跑酷类游戏《逃离地球》源

    1、Unity3D 跑酷类游戏《逃离地球》源码下载 2、非捡代码论坛研发项目,所以请不要商用,仅限学习使用,请24...

网友评论

      本文标题:Unity实战之方块跑酷(一)

      本文链接:https://www.haomeiwen.com/subject/myutnftx.html