一、初始化项目开发环境
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)可以将三维向量转换为四元数。
修改摄像机参数值,正交模式,活用Transform组件右上角的copy component和pasteprivate 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);//设置父物体
}
}
}
3)等腰直角三角形求底边长实现单排菱形平铺
等腰直角三角形求底边:2的平方根 乘以 直角边长
unity代码实现:Mathf.Sqrt(2)*n;
地板转了45°,两块地板之间距离变为斜边,调整SIZEprivate 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);
}
}
}
网友评论