Entity LoadJellyfish() { Entity entity = new Entity("jellyfish"); Transform transform = entity.AddComponent <Transform>(); transform.Position = Screen.Center; SpriteAtlas atlas = SpriteAtlasLoader.ParseSpriteAtlas("Content/Mobs/Jellyfish/atlas/jellyfish.atlas", true); SpriteAnimator animator = new SpriteAnimator(atlas); entity.AddComponent(animator); transform.Origin = new Vector2(animator.TargetRectangle.Width * 0.5f, animator.TargetRectangle.Height * 0.5f); entity.AddComponent <Mover>(); CircleCollider circleCollider = new CircleCollider(); circleCollider.CollisionLayer = CollisionLayer.Jellyfish; entity.AddComponent(circleCollider); entity.AddComponent <Jellyfish>(); return(entity); }
public const int TYPE_SELECTABLE_BIT = 4; // 按钮 public static void RemoveOtherCs(GameObject go, int type) { SpriteBigLoader bigLoader = go.GetComponent <SpriteBigLoader>(); SpriteAtlasLoader altasLoader = go.GetComponent <SpriteAtlasLoader>(); SpriteSelectableAltasLoader selectableLoader = go.GetComponent <SpriteSelectableAltasLoader>(); SpriteSelectableBigLoader selectableBigLoader = go.GetComponent <SpriteSelectableBigLoader>(); if (bigLoader != null && type != TYPE_BIG_SPRITE) { UnityEngine.Object.DestroyImmediate(bigLoader); } if (altasLoader != null && type != TYPE_SPRITE) { UnityEngine.Object.DestroyImmediate(altasLoader); } if (selectableLoader != null && type != TYPE_SELECTABLE_SPRITE) { UnityEngine.Object.DestroyImmediate(selectableLoader); } if (selectableBigLoader != null && type != TYPE_SELECTABLE_BIT) { UnityEngine.Object.DestroyImmediate(selectableBigLoader); } }
Entity LoadPlayer() { Entity player = new Entity("player"); Transform transform = new Transform(); transform.Position = Screen.Center; player.AddComponent(transform); SpriteAtlas atlas = SpriteAtlasLoader.ParseSpriteAtlas("Content/Character/atlas/character.atlas", true); SpriteAnimator animator = new SpriteAnimator(atlas); animator.Play("idle"); player.AddComponent(animator); transform.Origin = animator.TargetRectangle.Size * 0.5f; player.AddComponent <Mover>(); PolygonCollider polygonCollider = new PolygonCollider(); polygonCollider.CollisionLayer = CollisionLayer.Player; player.AddComponent <PolygonCollider>(); player.AddComponent <Player>(); return(player); }
// 处理Sprite中的图集 private static void ExcuteAltas(Image img, string imgPath) { // 图集的处理方式 SpriteAtlasLoader loader = GameObjectHelper.AddComponent <SpriteAtlasLoader>(img.gameObject); RemoveOtherCs(img.gameObject, TYPE_SPRITE); img.sprite = null; loader._initComplete = false; loader._resPath = imgPath; loader._spriteTag = GetSpriteTag(imgPath); }
void Awake() { instance = this; //Load atlas sprites = Resources.LoadAll <Sprite>("EventAtlas"); //Initialize lookup foreach (Sprite s in sprites) { spritesDict.Add(s.name, s); } }
/// <summary> /// Loads a SpriteAtlas created with the Sprite Atlas Packer tool /// </summary> public SpriteAtlas LoadSpriteAtlas(string name, bool premultiplyAlpha = false) { if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is SpriteAtlas spriteAtlas) { return(spriteAtlas); } } var atlas = SpriteAtlasLoader.ParseSpriteAtlas(name, premultiplyAlpha); LoadedAssets.Add(name, atlas); DisposableAssets.Add(atlas); return(atlas); }
/// <summary> /// Loads a SpriteAtlas created with the Sprite Atlas Packer tool /// </summary> public SpriteAtlas LoadSpriteAtlas(string name, bool premultiplyAlpha = false) { if (LoadedAssets.TryGetValue(name, out var asset)) { if (asset is SpriteAtlas spriteAtlas) { return(spriteAtlas); } } var atlasData = SpriteAtlasLoader.ParseSpriteAtlasData($"Content/{name}.atlas"); var atlas = atlasData.AsSpriteAtlas(ReadAsset <Texture2D>(name, null)); LoadedAssets.Add(name, atlas); DisposableAssets.Add(atlas); return(atlas); }
Entity LoadSquid() { Entity entity = new Entity("squid"); SpriteAtlas spriteAtlas = SpriteAtlasLoader.ParseSpriteAtlas("Content/Mobs/Squid/atlas/squid.atlas", true); SpriteAnimator spriteAnimator = new SpriteAnimator(spriteAtlas); entity.AddComponent(spriteAnimator); Transform transform = entity.AddComponent <Transform>(); transform.Origin = spriteAnimator.TargetRectangle.Size * 0.5f; transform.Position = new Vector2(Screen.Width * 0.5f, Screen.Height - 0.5f * spriteAnimator.TargetRectangle.Height); entity.AddComponent <Mover>(); entity.AddComponent <Squid>(); return(entity); }
void LoadTextureAndAtlasFiles() { if (_texturePtr != IntPtr.Zero) { Core.GetGlobalManager <ImGuiManager>().UnbindTexture(_texturePtr); } _spriteAtlasData.Clear(); _nonEditableAnimations.Clear(); _atlasAllowsAnimationEditing = true; _hasSlicedContent = false; var _atlasTexture = Texture2D.FromStream(Core.GraphicsDevice, File.OpenRead(_sourceImageFile)); _textureSize = new Num.Vector2(_atlasTexture.Width, _atlasTexture.Height); _textureAspectRatio = _textureSize.X / _textureSize.Y; _texturePtr = Core.GetGlobalManager <ImGuiManager>().BindTexture(_atlasTexture); _textureLoadedThisFrame = true; if (File.Exists(_sourceAtlasFile)) { _hasSlicedContent = true; _spriteAtlasData = SpriteAtlasLoader.ParseSpriteAtlasData(_sourceAtlasFile, true); // ensure animations are contiguous, since that is all we support. // First check that all frames are in order in the animations for (var j = 0; j < _spriteAtlasData.AnimationFrames.Count; j++) { var animation = _spriteAtlasData.AnimationFrames[j]; for (var i = 0; i < animation.Count; i++) { if (i == 0) { continue; } if (animation[i] != animation[i - 1] + 1) { _atlasAllowsAnimationEditing = false; _nonEditableAnimations.Add(j); } } } // Next check that all frames are in order, ghetto style. We check that all the y-values of the rects // always increase or stay the same. Not perfect by any means, but we dont know if this is padded in some // odd way or contains sprites of odd sizes so this is a quick and dirty solution. var lastRectY = -1; for (var i = 0; i < _spriteAtlasData.SourceRects.Count; i++) { if (i == 0 || lastRectY <= _spriteAtlasData.SourceRects[i].Y) { lastRectY = _spriteAtlasData.SourceRects[i].Y; continue; } _atlasAllowsAnimationEditing = false; return; } } }