public void Copy(SPRITE_FRAME f) { this.uvs = f.uvs; this.scaleFactor = f.scaleFactor; this.topLeftOffset = f.topLeftOffset; this.bottomRightOffset = f.bottomRightOffset; }
public static void SetImageRaster(byte[] _byteFriendImage, GameObject _objImage) { Texture2D wwwFriendImage = new Texture2D((int)ScrollData.sizeMessageImage.x, (int)ScrollData.sizeMessageImage.y); MeshRenderer meshRenderer = (MeshRenderer)_objImage.GetComponent(typeof(MeshRenderer)); if (_byteFriendImage != null) { wwwFriendImage.LoadImage(_byteFriendImage); meshRenderer.material.mainTexture = wwwFriendImage; CSpriteFrame[] frames = new CSpriteFrame[4]; // 4 arrays of frames, with 1 frame each _objImage.GetComponent <UIButton>().SetUVs(new Rect(0, 0, 1, 1)); for (int i = 0; i < frames.Length; ++i) { frames[i] = new CSpriteFrame(); frames[i].uvs = new Rect(0, 0, 1, 1); SPRITE_FRAME [] anim = new SPRITE_FRAME[1]; anim[0] = frames[i].ToStruct(); _objImage.GetComponent <UIButton>().animations[i].SetAnim(anim); } _objImage.GetComponent <UIButton>().UpdateUVs(); } }
// Wraps all processing of a frame in one method // (Trimming, offset calculation, etc) void ProcessFrame(Texture2D tex, bool dontTrim, ref Texture2D newTex, ref SPRITE_FRAME frameInfo) { frameInfo = new SPRITE_FRAME(0); Rect occupiedArea; if (trimImages && !dontTrim) { occupiedArea = GetOccupiedRect(tex); } else { occupiedArea = new Rect(0, 0, tex.width, tex.height); } newTex = SnipArea(tex, occupiedArea); GetOffsets(tex, occupiedArea, ref frameInfo.topLeftOffset, ref frameInfo.bottomRightOffset); // Calculate the scale factor // (a value that when multiplied by the actual dimensions // of the sprite will yield a value that is half the size // that the sprite would be were the empty area not trimmed // out): frameInfo.scaleFactor.x = (tex.width / occupiedArea.width) * 0.5f; frameInfo.scaleFactor.y = (tex.height / occupiedArea.height) * 0.5f; }
public void SetFrameInfo(SPRITE_FRAME fInfo) { this.frameInfo = fInfo; this.uvRect = fInfo.uvs; this.SetBleedCompensation(); if (this.autoResize || this.pixelPerfect) { this.CalcSize(); } }
protected override void Awake() { if (textureAnimations == null) textureAnimations = new TextureAnim[0]; staticFrameInfo = _ser_stat_frame_info.ToStruct(); base.Awake(); Init(); }
public void SetFrameInfo(SPRITE_FRAME fInfo) { frameInfo = fInfo; uvRect = fInfo.uvs; SetBleedCompensation(); if (autoResize || pixelPerfect) { CalcSize(); } }
protected override void Awake() { if (textureAnimations == null) { textureAnimations = new TextureAnim[0]; } staticFrameInfo = _ser_stat_frame_info.ToStruct(); base.Awake(); Init(); }
/// <summary> /// Copies all the attributes of another sprite. /// </summary> /// <param name="s">A reference to the sprite to be copied.</param> public override void Copy(SpriteRoot s) { base.Copy(s); PackedSprite sp; // Check the type: if (!(s is PackedSprite)) { return; } sp = (PackedSprite)s; if (!sp.m_started) { staticFrameInfo = sp._ser_stat_frame_info.ToStruct(); } else { staticFrameInfo = sp.staticFrameInfo; } if (curAnim != null) { if (curAnim.index == -1) { PlayAnim(curAnim); } else { SetState(curAnim.index); } } else { frameInfo = staticFrameInfo; uvRect = frameInfo.uvs; if (autoResize || pixelPerfect) { CalcSize(); } else { SetSize(s.width, s.height); } } SetBleedCompensation(); }
// Finds all textures which are to be part // of a given atlas: private AtlasTextureList BuildTextureList(MaterialSpriteList msMap) { ISpriteAggregator spr; Texture2D newTex = null; SPRITE_FRAME frameInfo = new SPRITE_FRAME(0); AtlasTextureList texList = new AtlasTextureList(); texList.material = msMap.material; // Get all the textures pointed to by the sprites: for (int i = 0; i < msMap.sprites.Count; ++i) { spr = (ISpriteAggregator)msMap.sprites[i]; spr.Aggregate(AssetDatabase.GUIDToAssetPath, AssetDatabase.LoadAssetAtPath, AssetDatabase.AssetPathToGUID); // Get the sprite ready to give us its textures if (spr.SourceTextures.Length <= 0) { Debug.LogError("Null texture reference detected on sprite \"" + ((Component)spr).name + "\"! Please verify that this was intentional."); return(null); } for (int j = 0; j < spr.SourceTextures.Length; ++j) { if (spr.SourceTextures[j] != null) { // See if the texture is already in our list: if (-1 >= texList.textures.IndexOf(spr.SourceTextures[j])) { VerifyImportSettings(spr.SourceTextures[j]); texList.textures.Add(spr.SourceTextures[j]); ProcessFrame(spr.SourceTextures[j], spr.DoNotTrimImages, ref newTex, ref frameInfo); texList.trimmedTextures.Add(newTex); texList.frames.Add(frameInfo); } } else { Debug.LogError("Null texture reference detected on sprite \"" + ((Component)spr).name + "\"! Please verify that this was intentional."); #if WARN_ON_NULL_TEXTURE Debug.LogWarning("Null texture reference detected on sprite \"" + ((Component)spr).name + "\"! Please verify that this was intentional."); #endif } } } return(texList); }
public bool GetNextFrame(ref SPRITE_FRAME nextFrame) { if (this.frames.Length < 1) { return(false); } if (this.curFrame + this.stepDir >= this.frames.Length || this.curFrame + this.stepDir < 0) { if (this.stepDir > 0 && this.loopReverse) { this.stepDir = -1; this.curFrame += this.stepDir; this.curFrame = Mathf.Clamp(this.curFrame, 0, this.frames.Length - 1); nextFrame = this.frames[this.curFrame]; } else { if (this.numLoops + 1 > this.loopCycles && this.loopCycles != -1) { return(false); } this.numLoops++; if (this.loopReverse) { this.stepDir *= -1; this.curFrame += this.stepDir; this.curFrame = Mathf.Clamp(this.curFrame, 0, this.frames.Length - 1); } else if (this.playInReverse) { this.curFrame = this.frames.Length - 1; } else { this.curFrame = 0; } nextFrame = this.frames[this.curFrame]; } } else { this.curFrame += this.stepDir; nextFrame = this.frames[this.curFrame]; } return(true); }
protected virtual void OnEnable() { if (this.managed && this.manager != null && this.m_started) { SPRITE_FRAME sPRITE_FRAME = this.frameInfo; this.manager.AddSprite(this); this.frameInfo = sPRITE_FRAME; this.uvRect = this.frameInfo.uvs; this.SetBleedCompensation(); } else if (this.savedManager != null) { SPRITE_FRAME sPRITE_FRAME2 = this.frameInfo; this.savedManager.AddSprite(this); this.frameInfo = sPRITE_FRAME2; this.uvRect = this.frameInfo.uvs; this.SetBleedCompensation(); } }
/// <summary> /// Appends UV animation to the clip specified by index. /// </summary> /// <param name="index">The animation clip to append to.</param> /// <param name="anim">Array of ANIM_FRAMEs that define the animation to be appended.</param> public void AppendAnim(int index, SPRITE_FRAME[] anim) { if (index >= clips.Length) return; clips[index].AppendAnim(anim); CalcDuration(); }
public bool GetNextFrame(ref SPRITE_FRAME nextFrame) { if (clips.Length < 1) return false; ret = clips[curClip].GetNextFrame(ref nextFrame); if (!ret) { // See if we have another clip in the queue: if ((curClip + stepDir) >= clips.Length || (curClip + stepDir) < 0) { // See if we need to loop (if we're reversing, we don't loop until we get back to the beginning): if (stepDir > 0 && loopReverse) { stepDir = -1; // Reverse playback direction curClip += stepDir; curClip = Mathf.Clamp(curClip, 0, clips.Length - 1); // Make the newly selected clip ready for playing: clips[curClip].Reset(); clips[curClip].PlayInReverse(); // Go ahead and step one since it will start // at the end, and that's where we are already: clips[curClip].GetNextFrame(ref nextFrame); } else { // See if we can loop: if (numLoops + 1 > loopCycles && loopCycles != -1) return false; // We've reached the end of the last clip else { // Loop the animation: ++numLoops; if (loopReverse) { stepDir *= -1; curClip += stepDir; curClip = Mathf.Clamp(curClip, 0, clips.Length - 1); // Make the newly selected clip ready for playing: clips[curClip].Reset(); if (stepDir < 0) clips[curClip].PlayInReverse(); // Go ahead and step one since it will start // where we are already: clips[curClip].GetNextFrame(ref nextFrame); } else { curClip = 0; framePos = -1; // Make the newly selected clip ready for playing: clips[curClip].Reset(); } } } } else { curClip += stepDir; // Make the newly selected clip ready for playing: clips[curClip].Reset(); if (stepDir < 0) { clips[curClip].PlayInReverse(); // Go ahead and step one since it will start // at the end, and that's where we are already: clips[curClip].GetNextFrame(ref nextFrame); } } framePos += stepDir; // Get the next frame since our previous // attempt failed, and all that we just // did was to be able to get the next one: clips[curClip].GetNextFrame(ref nextFrame); return true; // Keep playing } framePos += stepDir; /* // Simpler, non-looping logic: if (curClip < clips.Length - 1) { // Go to the next clip: ++curClip; return true; // Keep playing } else return false; // We've reached the end of the last clip */ return true; }
/// <summary> /// Appends the specified array of frames to the /// existing animation. /// </summary> /// <param name="anim">Array of ANIM_FRAMEs which hold the UV /// coordinates defining the animation.</param> public void AppendAnim(SPRITE_FRAME[] anim) { SPRITE_FRAME[] tempFrames = frames; frames = new SPRITE_FRAME[frames.Length + anim.Length]; tempFrames.CopyTo(frames, 0); anim.CopyTo(frames, tempFrames.Length); CalcLength(); }
/// <summary> /// Assigns the specified array of frames to the /// animation, replacing its current contents. /// </summary> /// <param name="anim">Array of ANIM_FRAMEs which hold the UV /// coordinates defining the animation.</param> public void SetAnim(SPRITE_FRAME[] anim) { frames = anim; CalcLength(); }
/// <summary> /// Constructs an array of frames based upon the info /// supplied. NOTE: When the edge of the texture is reached, /// this algorithm will "wrap" to the extreme left edge of the /// atlas. /// </summary> /// <param name="start">The UV of the lower-left corner of the first cell</param> /// <param name="cellSize">width and height, in UV space, of each cell</param> /// <param name="totalCells">Total number of cells in the in the animation.</param> /// <returns>Array of SPRITE_FRAME objects that contain the UVs of each frame of animation.</returns> public SPRITE_FRAME[] BuildWrappedUVAnim(Vector2 start, Vector2 cellSize, int totalCells) { int cellCount = 0; Vector2 curPos; frames = new SPRITE_FRAME[totalCells]; frames[0] = new SPRITE_FRAME(0); frames[0].uvs.x = start.x; frames[0].uvs.y = start.y; frames[0].uvs.xMax = start.x + cellSize.x; frames[0].uvs.yMax = start.y + cellSize.y; curPos = start; for (cellCount = 1; cellCount < totalCells; ++cellCount) { curPos.x += cellSize.x; if (curPos.x + cellSize.x > 1.01f) { curPos.x = 0; curPos.y -= cellSize.y; } frames[cellCount] = new SPRITE_FRAME(0); frames[cellCount].uvs.x = curPos.x; frames[cellCount].uvs.y = curPos.y; frames[cellCount].uvs.xMax = curPos.x + cellSize.x; frames[cellCount].uvs.yMax = curPos.y + cellSize.y; } return frames; }
// Finds all textures which are to be part // of a given atlas: AtlasTextureList BuildTextureList(MaterialSpriteList msMap) { ISpriteAggregator spr; Texture2D newTex = null; SPRITE_FRAME frameInfo = new SPRITE_FRAME(0); AtlasTextureList texList = new AtlasTextureList(); texList.material = msMap.material; // Get all the textures pointed to by the sprites: for (int i = 0; i < msMap.sprites.Count; ++i) { spr = msMap.sprites[i]; spr.Aggregate(AssetDatabase.GUIDToAssetPath, AssetDatabase.LoadAssetAtPath, AssetDatabase.AssetPathToGUID); // Get the sprite ready to give us its textures for (int j = 0; j < spr.SourceTextures.Length; ++j) { if (spr.SourceTextures[j] != null) { // See if the texture is already in our list: if (-1 >= texList.textures.IndexOf(spr.SourceTextures[j])) { VerifyImportSettings(spr.SourceTextures[j]); texList.textures.Add(spr.SourceTextures[j]); ProcessFrame(spr.SourceTextures[j], spr.DoNotTrimImages, ref newTex, ref frameInfo); texList.trimmedTextures.Add(newTex); texList.frames.Add(frameInfo); } } else { #if WARN_ON_NULL_TEXTURE Debug.LogWarning("Null texture reference detected on sprite \"" + ((Component)spr).name + "\"! Please verify that this was intentional."); #endif } } } return texList; }
public bool GetNextFrame(ref SPRITE_FRAME nextFrame) { if (this.clips.Length < 1) { return(false); } this.ret = this.clips[this.curClip].GetNextFrame(ref nextFrame); if (!this.ret) { if (this.curClip + this.stepDir >= this.clips.Length || this.curClip + this.stepDir < 0) { if (this.stepDir > 0 && this.loopReverse) { this.stepDir = -1; this.curClip += this.stepDir; this.curClip = Mathf.Clamp(this.curClip, 0, this.clips.Length - 1); this.clips[this.curClip].Reset(); this.clips[this.curClip].PlayInReverse(); this.clips[this.curClip].GetNextFrame(ref nextFrame); } else { if (this.numLoops + 1 > this.loopCycles && this.loopCycles != -1) { return(false); } this.numLoops++; if (this.loopReverse) { this.stepDir *= -1; this.curClip += this.stepDir; this.curClip = Mathf.Clamp(this.curClip, 0, this.clips.Length - 1); this.clips[this.curClip].Reset(); if (this.stepDir < 0) { this.clips[this.curClip].PlayInReverse(); } this.clips[this.curClip].GetNextFrame(ref nextFrame); } else { this.curClip = 0; this.framePos = -1; this.clips[this.curClip].Reset(); } } } else { this.curClip += this.stepDir; this.clips[this.curClip].Reset(); if (this.stepDir < 0) { this.clips[this.curClip].PlayInReverse(); this.clips[this.curClip].GetNextFrame(ref nextFrame); } } this.framePos += this.stepDir; this.clips[this.curClip].GetNextFrame(ref nextFrame); return(true); } this.framePos += this.stepDir; return(true); }
/// <summary> /// Copies all the attributes of another sprite. /// </summary> /// <param name="s">A reference to the sprite to be copied.</param> public override void Copy(SpriteRoot s) { base.Copy(s); PackedSprite sp; // Check the type: if (!(s is PackedSprite)) return; sp = (PackedSprite)s; staticFrameInfo = sp.staticFrameInfo; if (autoResize || pixelPerfect) CalcSize(); else SetSize(s.width, s.height); SetBleedCompensation(); }
/// <summary> /// Replaces the contents of the specified clip. /// </summary> /// <param name="index">Index of the clip the contents of which you wish to replace.</param> /// <param name="frames">Array of ANIM_FRAMEs that define the content of an animation clip.</param> public void SetAnim(int index, SPRITE_FRAME[] frames) { if (index >= clips.Length) return; clips[index].SetAnim(frames); CalcDuration(); }
// Stores the UV and other info of the next frame in 'nextFrame', // returns false if we've reached the end of the animation (this // will never happen if it is set to loop infinitely) public bool GetNextFrame(ref SPRITE_FRAME nextFrame) { if (frames.Length < 1) return false; // See if we can advance to the next frame: if ((curFrame + stepDir) >= frames.Length || (curFrame + stepDir) < 0) { // See if we need to loop (if we're reversing, we don't loop until we get back to the beginning): if (stepDir > 0 && loopReverse) { stepDir = -1; // Reverse playback direction curFrame += stepDir; curFrame = Mathf.Clamp(curFrame, 0, frames.Length - 1); nextFrame = frames[curFrame]; } else { // See if we can loop: if (numLoops + 1 > loopCycles && loopCycles != -1) return false; else { // Loop the animation: ++numLoops; if (loopReverse) { stepDir *= -1; curFrame += stepDir; curFrame = Mathf.Clamp(curFrame, 0, frames.Length - 1); } else { if (playInReverse) curFrame = frames.Length - 1; else curFrame = 0; } nextFrame = frames[curFrame]; } } } else { curFrame += stepDir; nextFrame = frames[curFrame]; } return true; }
/// <summary> /// Copies all the attributes of another sprite. /// </summary> /// <param name="s">A reference to the sprite to be copied.</param> public override void Copy(SpriteRoot s) { base.Copy(s); PackedSprite sp; // Check the type: if (!(s is PackedSprite)) return; sp = (PackedSprite)s; if (!sp.m_started) staticFrameInfo = sp._ser_stat_frame_info.ToStruct(); else staticFrameInfo = sp.staticFrameInfo; if (curAnim != null) { if (curAnim.index == -1) PlayAnim(curAnim); else SetState(curAnim.index); } else { frameInfo = staticFrameInfo; uvRect = frameInfo.uvs; if (autoResize || pixelPerfect) CalcSize(); else SetSize(s.width, s.height); } SetBleedCompensation(); }
/// <summary> /// Constructs an array of frames based upon the info /// supplied. NOTE: When the edge of the texture is reached, /// this algorithm will "wrap" to the next row down, starting /// directly below the position of the first animation cell /// on the row above. /// </summary> /// <param name="start">The UV of the lower-left corner of the first cell</param> /// <param name="cellSize">width and height, in UV space, of each cell</param> /// <param name="cols">Number of columns in the grid</param> /// <param name="rows">Number of rows in the grid</param> /// <param name="totalCells">Total number of cells in the grid (left-to-right, top-to-bottom ordering is assumed, just like reading English).</param> /// <returns>Array of SPRITE_FRAME objects that contain the UVs of each frame of animation.</returns> public SPRITE_FRAME[] BuildUVAnim(Vector2 start, Vector2 cellSize, int cols, int rows, int totalCells) { int cellCount = 0; frames = new SPRITE_FRAME[totalCells]; frames[0] = new SPRITE_FRAME(0); frames[0].uvs.x = start.x; frames[0].uvs.y = start.y; frames[0].uvs.xMax = start.x + cellSize.x; frames[0].uvs.yMax = start.y + cellSize.y; for (int row = 0; row < rows; ++row) { for (int col = 0; col < cols && cellCount < totalCells; ++col) { frames[cellCount] = new SPRITE_FRAME(0); frames[cellCount].uvs.x = start.x + cellSize.x * ((float)col); frames[cellCount].uvs.y = start.y - cellSize.y * ((float)row); frames[cellCount].uvs.xMax = frames[cellCount].uvs.x + cellSize.x; frames[cellCount].uvs.yMax = frames[cellCount].uvs.y + cellSize.y; ++cellCount; } } CalcLength(); return frames; }
// Wraps all processing of a frame in one method // (Trimming, offset calculation, etc) void ProcessFrame(Texture2D tex, bool dontTrim, ref Texture2D newTex, ref SPRITE_FRAME frameInfo) { frameInfo = new SPRITE_FRAME(0); Rect occupiedArea; if (trimImages && !dontTrim) occupiedArea = GetOccupiedRect(tex); else occupiedArea = new Rect(0, 0, tex.width, tex.height); newTex = SnipArea(tex, occupiedArea); GetOffsets(tex, occupiedArea, ref frameInfo.topLeftOffset, ref frameInfo.bottomRightOffset); // Calculate the scale factor // (a value that when multiplied by the actual dimensions // of the sprite will yield a value that is half the size // that the sprite would be were the empty area not trimmed // out): frameInfo.scaleFactor.x = (tex.width / occupiedArea.width) * 0.5f; frameInfo.scaleFactor.y = (tex.height / occupiedArea.height) * 0.5f; }
public CSpriteFrame(SPRITE_FRAME f) { this.Copy(f); }