/// <summary> /// 将一个精灵层放入动画中 /// </summary> /// <param name="layer">精灵层</param> public void PutLayer(SpriteLayer layer) { if (layers == null || layers.Length == 0) { layers = new SpriteLayer[1]; layers[0] = layer; } else { // 判断是替换还是追加 bool replace = false; for (int i = 0; i < layers.Length; i++) { if (layers[i].id == layer.id) { // 替换原有的层 layers[i] = layer; replace = true; break; } } if (!replace) { // 追加 System.Array.Resize <SpriteLayer>(ref layers, layers.Length + 1); layers[layers.Length - 1] = layer; } } InitLayer(layer); }
/// <summary> /// 清除指定层的精灵 /// </summary> /// <param name="layerId">层ID</param> public void Clear(int layerId) { SpriteLayer layer = GetLayer(layerId); if (layer != null) { Clear(layer); } }
private void InitLayerBySpriteInstance(SpriteLayer layer) { if (layer.spriteInstance != null) { Sprite sprite = layer.spriteInstance; layer.maxIndex = sprite.maxIndex + layer.frameDelay; sprite.flipHorizontal = flipHorizontal; sprite.flipVertical = flipVertical; } }
private void InitLayerBySprite(SpriteLayer layer) { if (layer.spritePrefab != null) { Sprite sprite = Instantiate(layer.spritePrefab) as Sprite; layer.spriteInstance = sprite; sprite.transform.parent = layer.gameObject.transform; layer.maxIndex = sprite.maxIndex + layer.frameDelay; sprite.flipHorizontal = flipHorizontal; sprite.flipVertical = flipVertical; } }
/// <summary> /// 清除指定层的精灵 /// </summary> /// <param name="layer">层ID</param> public void Clear(SpriteLayer layer) { if (layer.gameObject != null) { DestroyGobj(layer.gameObject); layer.gameObject = null; } layer.hidden = true; layer.maxIndex = 0; layer.spritePrefab = null; layer.spriteInstance = null; }
private void HideLayer(SpriteLayer layer, bool yes) { if (yes) { layer.hidden = true; layer.gameObject.transform.localScale = Vector3.zero; } else { layer.hidden = false; layer.gameObject.transform.localScale = Vector3.one; } }
private void InitLayer(SpriteLayer layer) { // 确保层 gameObject 存在,没有则生成 Transform layerTransform = transform.FindChild(layer.name); if (layerTransform == null) { // game object of layer GameObject layerGo = new GameObject(); layerGo.transform.parent = transform; layerGo.name = layer.name; layer.gameObject = layerGo; } else { layer.gameObject = layerTransform.gameObject; // 获取精灵实例 if (layerTransform.childCount > 0) { Sprite sprite = null; foreach (Transform child in layerTransform) { sprite = child.GetComponent <Sprite>(); if (sprite != null) { layer.spriteInstance = sprite; break; } } } } if (layer.spritePrefab != null) { if (layer.spriteInstance != null) { InitLayerBySpriteInstance(layer); } else { // 初始化 sprite 部分 InitLayerBySprite(layer); } // 计算动画最大索引 if (maxIndex < layer.maxIndex) { maxIndex = layer.maxIndex; } // 该层是否需要隐藏 if (layer.hiddenBeforeBegin) { HideLayer(layer, true); } } else { HideLayer(layer, true); } }