# 二叉树算法应用案例

157 篇文章 2 订阅
25 篇文章 0 订阅

public AtlasNode Insert(Texture2D image, int index) {
if (image == null) // Obviously an error!
return null;

if (child != null) {// If this node is not a leaf, try inserting into first child.
AtlasNode newNode = child[0].Insert(image, index);
if (newNode != null)
return newNode;

// No more room in first child, insert into second child!
return child[1].Insert(image, index);
}
else {
// If there is already a lightmap in this node, early out
if (hasImage)
return null;

// If this node is too small for the image, return
if (!ImageFits(image, rc))
return null;

// If the image is perfect, accept!
if (PerfectFit(image, rc)) {
hasImage = true;
imageRef = image;
name = imageRef.name;
sortIndex = index;
return this;
}

// If we made it this far, this node must be split.
child = new AtlasNode[2];
child[0] = new AtlasNode();
child[1] = new AtlasNode();

// Decide which way to split image
float deltaW = rc.width - image.width;
float deltaH = rc.height - image.height;

if (deltaW > deltaH) {
child[0].rc = new Rect(rc.xMin, rc.yMin, image.width, rc.height);
child[1].rc = new Rect(rc.xMin + image.width + TEXTURE_PADDING, rc.yMin, rc.width - (image.width + TEXTURE_PADDING), rc.height);
}
else {
child[0].rc = new Rect(rc.xMin, rc.yMin, rc.width, image.height);
child[1].rc = new Rect(rc.xMin, rc.yMin + image.height + TEXTURE_PADDING, rc.width, rc.height - (image.height + TEXTURE_PADDING));
}

// Lets try inserting into first child, eh?
return child[0].Insert(image, index);
}
}

public static Atlas[] CreateAtlas(string name, Texture2D[] textures, Atlas startWith = null) {
List<Texture2D> toProcess = new List<Texture2D>();
int index = toProcess.Count - 1;
toProcess.Reverse(); // Because we index backwards

List<Atlas> result = new List<Atlas>();

int insertIndex = 0;
if (startWith != null) {
insertIndex = startWith.root.sortIndex;
}

while(index >= 0) {
Atlas _atlas = startWith;
if (_atlas == null) {
_atlas = new Atlas();
_atlas.texture = new Texture2D(AtlasSize, AtlasSize, TextureFormat.RGBA32, false);
_atlas.root = new AtlasNode();
_atlas.root.rc = new Rect(0, 0, AtlasSize, AtlasSize);
}
startWith = null;

while (index >= 0 && (_atlas.root.Contains(toProcess[index].name) || _atlas.root.Insert(toProcess[index], insertIndex++) != null)) {
index -= 1;
}
_atlas.root.sortIndex = insertIndex;
insertIndex = 0;
_atlas = null;
}

foreach(Atlas atlas in result) {
atlas.root.Build(atlas.texture);
List<AtlasNode> nodes = new List<AtlasNode>();
atlas.root.GetBounds(ref nodes);
nodes.Sort(delegate (AtlasNode x, AtlasNode y) {
if (x.sortIndex == y.sortIndex) return 0;
if (y.sortIndex > x.sortIndex) return -1;
return 1;
});

List<Rect> rects = new List<Rect>();
foreach(AtlasNode node in nodes) {
Rect normalized = new Rect(node.rc.xMin / atlas.root.rc.width, node.rc.yMin / atlas.root.rc.height, node.rc.width / atlas.root.rc.width,

node.rc.height / atlas.root.rc.height);
// bunp everything over by half a pixel to avoid floating errors
normalized.x += 0.5f / atlas.root.rc.width;
normalized.width -= 1.0f / atlas.root.rc.width;
normalized.y += 0.5f / atlas.root.rc.height;
normalized.height -= 1.0f / atlas.root.rc.height;
}

atlas.uvRects = new AtlasDescriptor[rects.Count];
for (int i = 0; i < rects.Count; i++) {
atlas.uvRects[i] = new AtlasDescriptor();
atlas.uvRects[i].width = (int)nodes[i].rc.width;
atlas.uvRects[i].height = (int)nodes[i].rc.height;
atlas.uvRects[i].name = nodes[i].name;
atlas.uvRects[i].uvRect = rects[i];
}

atlas.root.Clear();
#if DEBUG_ATLASES
atlas.texture.Apply(false, false);
SaveAtlas(atlas, name);
#else
if (atlas != result[result.Count - 1])
atlas.texture.Apply(false, true);
else
atlas.texture.Apply(false, false);
#endif
}

return result.ToArray();
}

if(d<100) state=嘲笑，单挑;
else if(d<200) state=单挑;
else if(d<300) state=嗜血魔法;
else if(d<400) state=呼唤同伴;
else state=逃跑;


　if(d>=200)&&(d<300) state=嗜血魔法;
else if(d>=300)&&(d<500) state=呼唤同伴;
else if(d>=100)&&(d<200) state=单挑;
else if(d<100) state=嘲笑，单挑;
else state=逃跑;


• 7
点赞
• 24
收藏
觉得还不错? 一键收藏
• 1
评论
06-25 4万+
08-14 549
05-12 209
10-22 693
04-25 3万+
03-20 3万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。