今天主要来说下,数据绑定。
之前第一章,我说到 把 资源图 画成格子,你们应该还有印象吧。
那么,当我 知道 格子数据,能否拿到 资源对应的图片呢?
大家先复习一下 第一章,发现很多格子数据 是相同的,有好多个1,好多个2 等等。
所以这里必须要定义一个方向,比如 左上角的1 ,右上角的1,左下角的1,右下角的1.
好,先把资源加载类,继续优化下
using UnityEngine;
using System.Collections; public class ResourcesSprite : MonoBehaviour
{ void Start()
{
OnAutoTileSave("", "sea", , );
OnAutoTileSave("", "sea", , );
OnAutoTileSave("", "sea", , );
OnSortAutoTile("sea", );
} void Update()
{ } //对资源图 排序
void OnSortAutoTile(string key, int total = )
{
Sprite[, ,] dict = GameModel.getInstance().dictSprite[key];
for (int j = ; j < ; j++)
{
Sprite[] sp = new Sprite[total];
for (int i = ; i < total; i++)
{
sp[i] = dict[i, j / , j % ];
}
int x1 = j / ;
int y1 = j % ;
GameModel.getInstance().autoTileSprite.Add(x1 + "," + y1 + "," + key, sp);
}
} //根据 图片名字,键值名字,当前帧 和 总共帧 保存单例中
void OnAutoTileSave(string pathName, string key, int current, int total)
{
object[] objs = Resources.LoadAll(pathName);
Sprite[, ,] dict;
//是否存在 键值
if (GameModel.getInstance().dictSprite.ContainsKey(key))
{
dict = GameModel.getInstance().dictSprite[key];
}
else
{
dict = new Sprite[total, , ];
GameModel.getInstance().dictSprite.Add(key, dict);
} int i = ;
foreach (object obj in objs)
{
Sprite sp = obj as Sprite;
if (sp != null)
{ dict[current, i / , i % ] = sp;
i++;
}
}
GameModel.getInstance().dictSprite[key] = dict;
} }
大家可以看到,我增加了一个 资源排序的方法,因为 资源图 是 固定的 6*4 格子,应该不可能变,所以我也就写死了。
好,下面看看如何调用这个类。
同样,在昨天 那四个 sprite脚本中增加
using UnityEngine;
using System.Collections; public class AutoTileShow : MonoBehaviour
{
public int directions;
int i = ;
private Sprite m_Image; bool open = true;
Sprite[] sp; void Start()
{
if (!gameObject.GetComponent<SpriteRenderer>())
{
m_Image = gameObject.AddComponent<SpriteRenderer>().sprite;
}
sp = new Sprite[];//图片数据临时存放
} float timebattle; void Update()
{
timebattle -= Time.deltaTime;
if (timebattle <= )
{
timebattle = .2f;//闪烁频率 if (i == )
{
i = ;
} if (open)
{
sp = OnGetAutoTile(directions, );
open = false;
} m_Image = sp[i];
gameObject.GetComponent<SpriteRenderer>().sprite = m_Image;
i++;
}
} //通过 方向 权值 获取 对应图像
public Sprite[] OnGetAutoTile(int direction, int weightNumber, string key = "sea")
{
Sprite[] map = { };
ArrayList arr = new ArrayList();
switch (weightNumber)
{
case :
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
map = OnDirectionAutoTile(direction, key, arr);
break;
case :
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
map = OnDirectionAutoTile(direction, key, arr);
break;
case :
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
map = OnDirectionAutoTile(direction, key, arr);
break;
case :
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
map = OnDirectionAutoTile(direction, key, arr);
break;
case :
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
map = OnDirectionAutoTile(direction, key, arr);
break;
case :
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
map = OnDirectionAutoTile(direction, key, arr);
break;
case :
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
arr.Add( + "," + + "," + "sea");
map = OnDirectionAutoTile(direction, key, arr);
break;
default: break;
}
return map;
} public Sprite[] OnDirectionAutoTile(int direction, string key, ArrayList arr)
{
Sprite[] mapTile = new Sprite[];
mapTile = GameModel.getInstance().autoTileSprite[arr[direction - ].ToString()];
return mapTile;
/***
switch (direction)
{
case 1://左上
mapTile = GameModel.getInstance().autoTileSprite[arr[0].ToString()];
break;
case 2://左下
mapTile = GameModel.getInstance().autoTileSprite[1 + "," + 0 + "," + key];
break;
case 3://右上
mapTile = GameModel.getInstance().autoTileSprite[0 + "," + 1 + "," + key];
break;
case 4://右下
mapTile = GameModel.getInstance().autoTileSprite[1 + "," + 1 + "," + key];
break;
default: break;
}
* **/
}
}
增加后,我们在u3d界面中,看到一个方向变量。
我们把 四个图,按照左上左下右上右下 1234,对应起来,大家也可以看看这个脚本,有部分注释的,也是这么定义的。
在
void Update() 中,我们运行 sp = OnGetAutoTile(directions, 1);
注意,这个1 就是 权值,也就是 绑定格子的数据值。
我们可以看到 动画状态的 图,如下。因为 是1 的格子,要求四个方向。
链接:http://pan.baidu.com/s/1c1dOojm 密码:esis
注:OnGetAutoTile方法 写的不是很好,因为我还没找到 规律来优化它,不过反正 只有1-7 这几种,先讲究下吧。
好了,今天先说道这里,下一节是 权值检测,很重要的一章哦