// Draw a sprite within the rect - i.e. starting at the rect public static void DrawSpriteTextureInRect( Rect rect, tk2dSpriteDefinition def, Color tint, Vector2 position, float angle, Vector2 scale ) { Init(); Vector2 pixelSize = new Vector3( 1.0f / (def.texelSize.x), 1.0f / (def.texelSize.y) ); Rect visibleRect = VisibleRect; Vector4 clipRegion = new Vector4(visibleRect.x, visibleRect.y, visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height); if (Event.current.type == EventType.Repaint) { if (def.material != null) { Mesh tmpMesh = new Mesh(); tmpMesh.vertices = def.positions; tmpMesh.uv = def.uvs; tmpMesh.triangles = def.indices; tmpMesh.RecalculateBounds(); tmpMesh.RecalculateNormals(); mat.mainTexture = def.material.mainTexture; mat.SetColor("_Tint", tint); mat.SetVector("_Clip", clipRegion); Matrix4x4 m = new Matrix4x4(); m.SetTRS(new Vector3(rect.x + position.x * scale.y, rect.y + position.y * scale.y, 0), Quaternion.Euler(0, 0, -angle), new Vector3(pixelSize.x * scale.x, -pixelSize.y * scale.y, 1)); mat.SetPass(0); Graphics.DrawMeshNow(tmpMesh, m * GUI.matrix); Object.DestroyImmediate(tmpMesh); } } }
public static void SetSpriteIndices(int[] indices, int offset, int vStart, tk2dSpriteDefinition spriteDef) { for (int i = 0; i < spriteDef.indices.Length; ++i) { indices[offset + i] = vStart + spriteDef.indices[i]; } }
public static void SetSpriteGeom(Vector3[] pos, Vector2[] uv, Vector3[] norm, Vector4[] tang, int offset, tk2dSpriteDefinition spriteDef, Vector3 scale) { for (int i = 0; i < spriteDef.positions.Length; ++i) { pos[offset + i] = Vector3.Scale(spriteDef.positions[i], scale); } for (int i = 0; i < spriteDef.uvs.Length; ++i) { uv[offset + i] = spriteDef.uvs[i]; } if (norm != null && spriteDef.normals != null) { for (int i = 0; i < spriteDef.normals.Length; ++i) { norm[offset + i] = spriteDef.normals[i]; } } if (tang != null && spriteDef.tangents != null) { for (int i = 0; i < spriteDef.tangents.Length; ++i) { tang[offset + i] = spriteDef.tangents[i]; } } }
void UpdateAttachPointTransform( tk2dSpriteDefinition.AttachPoint attachPoint, Transform t ) { t.localPosition = Vector3.Scale( attachPoint.position, sprite.scale ); t.localScale = sprite.scale; float scl = Mathf.Sign(sprite.scale.x) * Mathf.Sign(sprite.scale.y); t.localEulerAngles = new Vector3(0, 0, attachPoint.angle * scl); // handle angle fixup }
public void Draw(Rect r, tk2dSpriteDefinition sprite) { Init(); Event ev = Event.current; switch (ev.type) { case EventType.MouseDown: if (r.Contains(ev.mousePosition)) { dragging = true; ev.Use(); } break; case EventType.MouseDrag: if (dragging && r.Contains(ev.mousePosition)) { translate += ev.delta; ev.Use(); Repaint(); } break; case EventType.MouseUp: dragging = false; break; case EventType.ScrollWheel: if (r.Contains(ev.mousePosition)) { scale = Mathf.Clamp(scale + ev.delta.y * 0.1f, 0.1f, 10.0f); ev.Use(); Repaint(); } break; } tk2dGrid.Draw(r, translate); // Draw axis Vector2 axisPos = new Vector2(r.center.x + translate.x, r.center.y + translate.y); if (axisPos.y > r.yMin && axisPos.y < r.yMax) { Handles.color = new Color(1, 0, 0, 0.5f); Handles.DrawLine(new Vector2(r.x, r.center.y + translate.y), new Vector2(r.x + r.width, r.center.y + translate.y)); } if (axisPos.x > r.xMin && axisPos.x < r.xMax) { Handles.color = new Color(0, 1, 0, 0.5f); Handles.DrawLine(new Vector2(r.center.x + translate.x, r.y), new Vector2(r.center.x + translate.x, r.y + r.height)); } Handles.color = Color.white; // Draw sprite if (sprite != null) { tk2dSpriteThumbnailCache.DrawSpriteTextureCentered(r, sprite, translate, scale, Color.white); } }
// Clipped sprite public static void GetClippedSpriteGeomDesc(out int numVertices, out int numIndices, tk2dSpriteDefinition spriteDef) { if (spriteDef.positions.Length == 4) { numVertices = 4; numIndices = 6; } else { numVertices = 0; numIndices = 0; } }
public static void GetSlicedSpriteGeomDesc(out int numVertices, out int numIndices, tk2dSpriteDefinition spriteDef, bool borderOnly) { if (spriteDef.positions.Length == 4) { numVertices = 0x10; numIndices = !borderOnly ? 0x36 : 0x30; } else { numVertices = 0; numIndices = 0; } }
public static Vector3 ApplySpriteVertexTileFlags(tk2dTileMap tileMap, tk2dSpriteDefinition spriteDef, Vector3 pos, bool flipH, bool flipV, bool rot90) { float cx = tileMap.data.tileOrigin.x + 0.5f * tileMap.data.tileSize.x; float cy = tileMap.data.tileOrigin.y + 0.5f * tileMap.data.tileSize.y; float dx = pos.x - cx; float dy = pos.y - cy; if (rot90) { float tmp = dx; dx = dy; dy = -tmp; } if (flipH) dx *= -1.0f; if (flipV) dy *= -1.0f; pos.x = cx + dx; pos.y = cy + dy; return pos; }
public void Draw(Rect r, tk2dSpriteDefinition sprite) { Init(); Event ev = Event.current; switch (ev.type) { case EventType.MouseDown: if (r.Contains(ev.mousePosition)) { dragging = true; ev.Use(); } break; case EventType.MouseDrag: if (dragging && r.Contains(ev.mousePosition)) { translate += ev.delta; ev.Use(); Repaint(); } break; case EventType.MouseUp: dragging = false; break; case EventType.ScrollWheel: if (r.Contains(ev.mousePosition)) { scale = Mathf.Clamp(scale + ev.delta.y * 0.1f, 0.1f, 10.0f); ev.Use(); Repaint(); } break; } // Draw grid float scl = 16.0f; GUI.DrawTextureWithTexCoords(r, gridTexture, new Rect(-translate.x / scl, translate.y / scl, r.width / scl, r.height / scl), false); // Draw sprite if (sprite != null) { spriteThumbnailRenderer.DrawSpriteTextureCentered(r, sprite, translate, scale, Color.white); } }
private static tk2dSpriteDefinition CreateDefinitionForRegionInTexture(string name, Vector2 textureDimensions, float scale, Rect uvRegion, Rect trimRect, Vector2 anchor, bool rotated) { float height = uvRegion.height; float width = uvRegion.width; float x = textureDimensions.x; float y = textureDimensions.y; tk2dSpriteDefinition definition = new tk2dSpriteDefinition(); definition.flipped = !rotated ? tk2dSpriteDefinition.FlipMode.None : tk2dSpriteDefinition.FlipMode.TPackerCW; definition.extractRegion = false; definition.name = name; definition.colliderType = tk2dSpriteDefinition.ColliderType.Unset; Vector2 vector = new Vector2(0.001f, 0.001f); Vector2 vector2 = new Vector2((uvRegion.x + vector.x) / x, 1f - (((uvRegion.y + uvRegion.height) + vector.y) / y)); Vector2 vector3 = new Vector2(((uvRegion.x + uvRegion.width) - vector.x) / x, 1f - ((uvRegion.y - vector.y) / y)); Vector2 vector4 = new Vector2(trimRect.x - anchor.x, -trimRect.y + anchor.y); if (rotated) { vector4.y -= width; } vector4 = (Vector2) (vector4 * scale); Vector3 vector5 = new Vector3(-anchor.x * scale, anchor.y * scale, 0f); Vector3 vector6 = vector5 + new Vector3(trimRect.width * scale, -trimRect.height * scale, 0f); Vector3 vector7 = new Vector3(0f, -height * scale, 0f); Vector3 vector8 = vector7 + new Vector3(width * scale, height * scale, 0f); if (rotated) { definition.positions = new Vector3[] { new Vector3(-vector8.y + vector4.x, vector7.x + vector4.y, 0f), new Vector3(-vector7.y + vector4.x, vector7.x + vector4.y, 0f), new Vector3(-vector8.y + vector4.x, vector8.x + vector4.y, 0f), new Vector3(-vector7.y + vector4.x, vector8.x + vector4.y, 0f) }; definition.uvs = new Vector2[] { new Vector2(vector2.x, vector3.y), new Vector2(vector2.x, vector2.y), new Vector2(vector3.x, vector3.y), new Vector2(vector3.x, vector2.y) }; } else { definition.positions = new Vector3[] { new Vector3(vector7.x + vector4.x, vector7.y + vector4.y, 0f), new Vector3(vector8.x + vector4.x, vector7.y + vector4.y, 0f), new Vector3(vector7.x + vector4.x, vector8.y + vector4.y, 0f), new Vector3(vector8.x + vector4.x, vector8.y + vector4.y, 0f) }; definition.uvs = new Vector2[] { new Vector2(vector2.x, vector2.y), new Vector2(vector3.x, vector2.y), new Vector2(vector2.x, vector3.y), new Vector2(vector3.x, vector3.y) }; } definition.normals = new Vector3[0]; definition.tangents = new Vector4[0]; definition.indices = new int[] { 0, 3, 1, 2, 3, 0 }; Vector3 vector9 = new Vector3(vector5.x, vector6.y, 0f); Vector3 vector10 = new Vector3(vector6.x, vector5.y, 0f); definition.boundsData = new Vector3[] { (Vector3) ((vector10 + vector9) / 2f), vector10 - vector9 }; definition.untrimmedBoundsData = new Vector3[] { (Vector3) ((vector10 + vector9) / 2f), vector10 - vector9 }; definition.texelSize = new Vector2(scale, scale); return definition; }
public static void AddSpritesToCollection(AssetBundle assetSource, List <string> AssetNames, tk2dSpriteCollectionData collection) { List <Texture> m_Textures = new List <Texture>(); foreach (string AssetName in AssetNames) { m_Textures.Add(assetSource.LoadAsset <Texture2D>(AssetName)); } if (m_Textures.Count > 0) { foreach (Texture2D texture in m_Textures) { tk2dSpriteDefinition tk2dSpriteDefinition = ConstructDefinition(texture); tk2dSpriteDefinition.name = texture.name; AddSpriteToCollection(tk2dSpriteDefinition, collection); } } }
public void Draw(Rect r, tk2dSpriteDefinition sprite) { Init(); Event ev = Event.current; switch (ev.type) { case EventType.MouseDown: if (r.Contains(ev.mousePosition)) { dragging = true; ev.Use(); } break; case EventType.MouseDrag: if (dragging && r.Contains(ev.mousePosition)) { translate += ev.delta; ev.Use(); Repaint(); } break; case EventType.MouseUp: dragging = false; break; case EventType.ScrollWheel: if (r.Contains(ev.mousePosition)) { scale = Mathf.Clamp(scale + ev.delta.y * 0.1f, 0.1f, 10.0f); ev.Use(); Repaint(); } break; } tk2dGrid.Draw(r, translate); // Draw sprite if (sprite != null) { spriteThumbnailRenderer.DrawSpriteTextureCentered(r, sprite, translate, scale, Color.white); } }
public static tk2dSpriteDefinition SetupDefinitionForShellSprite(string name, int id, int pixelWidth, int pixelHeight, tk2dSpriteDefinition overrideToCopyFrom = null) { float thing = 14; float trueWidth = (float)pixelWidth / thing; float trueHeight = (float)pixelHeight / thing; tk2dSpriteDefinition def = overrideToCopyFrom ?? VFXCollection.inst.spriteDefinitions[(PickupObjectDatabase.GetById(202) as Gun).shellCasing.GetComponent <tk2dBaseSprite>().spriteId].CopyDefinitionFrom(); def.boundsDataCenter = new Vector3(trueWidth / 2f, trueHeight / 2f, 0f); def.boundsDataExtents = new Vector3(trueWidth, trueHeight, 0f); def.untrimmedBoundsDataCenter = new Vector3(trueWidth / 2f, trueHeight / 2f, 0f); def.untrimmedBoundsDataExtents = new Vector3(trueWidth, trueHeight, 0f); def.position0 = new Vector3(0f, 0f, 0f); def.position1 = new Vector3(0f + trueWidth, 0f, 0f); def.position2 = new Vector3(0f, 0f + trueHeight, 0f); def.position3 = new Vector3(0f + trueWidth, 0f + trueHeight, 0f); def.name = name; VFXCollection.spriteDefinitions[id] = def; return(def); }
private static tk2dSpriteDefinition SmartCopySpriteDefinition( tk2dSpriteDefinition original, Dictionary <object, object> objectDict ) { var newSpriteDefinition = new tk2dSpriteDefinition { name = original.name, boundsData = SmartCopyArray(original.boundsData, objectDict), untrimmedBoundsData = SmartCopyArray(original.untrimmedBoundsData, objectDict), texelSize = original.texelSize, positions = SmartCopyArray(original.positions, objectDict), normals = SmartCopyArray(original.normals, objectDict), tangents = SmartCopyArray(original.tangents, objectDict), uvs = SmartCopyArray(original.uvs, objectDict), normalizedUvs = SmartCopyArray(original.normalizedUvs, objectDict), indices = SmartCopyArray(original.indices, objectDict), material = SmartCopyMaterial(original.material, objectDict), materialInst = SmartCopyMaterial(original.materialInst, objectDict), materialId = original.materialId, sourceTextureGUID = original.sourceTextureGUID, extractRegion = original.extractRegion, regionX = original.regionX, regionY = original.regionY, regionW = original.regionW, regionH = original.regionH, flipped = original.flipped, complexGeometry = original.complexGeometry, physicsEngine = original.physicsEngine, colliderType = original.colliderType, // We do not deep copy this as we probably want to preserve these colliders customColliders = original.customColliders, colliderVertices = original.colliderVertices, colliderIndicesFwd = original.colliderIndicesFwd, colliderIndicesBack = original.colliderIndicesBack, colliderConvex = original.colliderConvex, colliderSmoothSphereCollisions = original.colliderSmoothSphereCollisions, polygonCollider2D = original.polygonCollider2D, edgeCollider2D = original.edgeCollider2D, attachPoints = original.attachPoints }; return(newSpriteDefinition); }
public void DrawSpriteTexture(Rect rect, tk2dSpriteDefinition def, Color tint) { Vector2 pixelSize = new Vector3(rect.width / def.untrimmedBoundsData[1].x, rect.height / def.untrimmedBoundsData[1].y); Rect visibleRect = VisibleRect; Vector4 clipRegion = new Vector4(visibleRect.x, visibleRect.y, visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height); bool visible = true; if (rect.xMin > visibleRect.xMax || rect.yMin > visibleRect.yMax || rect.xMax < visibleRect.xMin || rect.yMax < visibleRect.yMin) { visible = false; } if (Event.current.type == EventType.Repaint && visible) { Mesh tmpMesh = new Mesh(); tmpMesh.vertices = def.positions; tmpMesh.uv = def.uvs; tmpMesh.triangles = def.indices; tmpMesh.RecalculateBounds(); tmpMesh.RecalculateNormals(); Vector3 t = def.untrimmedBoundsData[1] * 0.5f - def.untrimmedBoundsData[0]; float tq = def.untrimmedBoundsData[1].y; mat.mainTexture = def.material.mainTexture; mat.SetColor("_Tint", tint); mat.SetVector("_Clip", clipRegion); Matrix4x4 m = new Matrix4x4(); m.SetTRS(new Vector3(rect.x + t.x * pixelSize.x, rect.y + (tq - t.y) * pixelSize.y, 0), Quaternion.identity, new Vector3(pixelSize.x, -pixelSize.y, 1)); mat.SetPass(0); Graphics.DrawMeshNow(tmpMesh, m * GUI.matrix); Object.DestroyImmediate(tmpMesh); } }
void Init() { // check if already initialized if (materialInsts != null) { return; } if (spriteDefinitions == null) { spriteDefinitions = new tk2dSpriteDefinition[0]; } if (materials == null) { materials = new Material[0]; } materialInsts = new Material[materials.Length]; if (needMaterialInstance) { for (int i = 0; i < materials.Length; ++i) { materialInsts[i] = Instantiate(materials[i]) as Material; #if UNITY_EDITOR materialInsts[i].hideFlags = HideFlags.DontSave; #endif } for (int i = 0; i < spriteDefinitions.Length; ++i) { tk2dSpriteDefinition def = spriteDefinitions[i]; def.materialInst = materialInsts[def.materialId]; } } else { for (int i = 0; i < spriteDefinitions.Length; ++i) { tk2dSpriteDefinition def = spriteDefinitions[i]; def.materialInst = def.material; } } }
private static tk2dSpriteDefinition ConstructDefinition(YAML.Clip clip, YAML.Frame frame, Texture2D texture, Material material) { var width = frame.W ?? clip.FrameSize.Width; var height = frame.H ?? clip.FrameSize.Height; var spritedef = new tk2dSpriteDefinition { normals = new Vector3[] { new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, 0.0f, -1.0f), }, tangents = new Vector4[] { new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), }, texelSize = new Vector2(0.1f, 0.1f), extractRegion = false, regionX = 0, regionY = 0, regionW = 0, regionH = 0, flipped = tk2dSpriteDefinition.FlipMode.None, complexGeometry = false, physicsEngine = tk2dSpriteDefinition.PhysicsEngine.Physics3D, colliderType = tk2dSpriteDefinition.ColliderType.Box, collisionLayer = CollisionLayer.PlayerHitBox, position0 = new Vector3(16 / frame.Position.X, 16 / frame.Position.Y), position1 = new Vector3(16 / frame.Position.X + width, 16 / frame.Position.Y), position2 = new Vector3(16 / frame.Position.X, 16 / frame.Position.Y + height), position3 = new Vector3(16 / frame.Position.X + width, 16 / frame.Position.Y + height), material = material, materialInst = material, materialId = 0, }; spritedef.uvs = ETGMod.Assets.GenerateUVs(texture, frame.X, frame.Y, width, height); return(spritedef); }
public static void SetProjectileSpriteRight(this Projectile proj, string name, int pixelWidth, int pixelHeight, bool lightened = true, tk2dBaseSprite.Anchor anchor = tk2dBaseSprite.Anchor.LowerLeft, bool anchorChangesCollider = true, bool fixesScale = false, int?overrideColliderPixelWidth = null, int?overrideColliderPixelHeight = null, int?overrideColliderOffsetX = null, int?overrideColliderOffsetY = null, Projectile overrideProjectileToCopyFrom = null) { try { proj.GetAnySprite().spriteId = ETGMod.Databases.Items.ProjectileCollection.inst.GetSpriteIdByName(name); tk2dSpriteDefinition def = SetupDefinitionForProjectileSprite(name, proj.GetAnySprite().spriteId, pixelWidth, pixelHeight, lightened, overrideColliderPixelWidth, overrideColliderPixelHeight, overrideColliderOffsetX, overrideColliderOffsetY, overrideProjectileToCopyFrom); def.ConstructOffsetsFromAnchor(anchor, def.position3, fixesScale, anchorChangesCollider); proj.GetAnySprite().scale = new Vector3(1f, 1f, 1f); proj.transform.localScale = new Vector3(1f, 1f, 1f); proj.GetAnySprite().transform.localScale = new Vector3(1f, 1f, 1f); proj.AdditionalScaleMultiplier = 1f; } catch (Exception ex) { ETGModConsole.Log("Ooops! Seems like something got very, Very, VERY wrong. Here's the exception:"); ETGModConsole.Log(ex.ToString()); } }
void PerformSelection() { tk2dSpriteDefinition def = SelectedDefinition; if (def != null) { int spriteIndex = -1; for (int i = 0; i < spriteCollection.inst.spriteDefinitions.Length; ++i) { if (spriteCollection.inst.spriteDefinitions[i] == def) { spriteIndex = i; } } if (spriteIndex != -1 && callback != null) { callback(spriteCollection, spriteIndex, callbackData); } } }
public static void SetTiledSpriteIndices(int[] indices, int offset, int vStart, tk2dSpriteDefinition spriteDef, Vector2 dimensions) { int numVertices; int numIndices; GetTiledSpriteGeomDesc(out numVertices, out numIndices, spriteDef, dimensions); int baseIndex = 0; for (int i = 0; i < numIndices; i += 6) { indices[offset + i + 0] = vStart + spriteDef.indices[0] + baseIndex; indices[offset + i + 1] = vStart + spriteDef.indices[1] + baseIndex; indices[offset + i + 2] = vStart + spriteDef.indices[2] + baseIndex; indices[offset + i + 3] = vStart + spriteDef.indices[3] + baseIndex; indices[offset + i + 4] = vStart + spriteDef.indices[4] + baseIndex; indices[offset + i + 5] = vStart + spriteDef.indices[5] + baseIndex; baseIndex += 4; } }
public static void SetupDefinitionForClipSprite(string name, int id, int pixelWidth, int pixelHeight) { float thing = 14; float trueWidth = (float)pixelWidth / thing; float trueHeight = (float)pixelHeight / thing; tk2dSpriteDefinition def = VFXCollection.inst.spriteDefinitions[(PickupObjectDatabase.GetById(47) as Gun).clipObject.GetComponent <tk2dBaseSprite>().spriteId].CopyDefinitionFrom(); def.boundsDataCenter = new Vector3(trueWidth / 2f, trueHeight / 2f, 0f); def.boundsDataExtents = new Vector3(trueWidth, trueHeight, 0f); def.untrimmedBoundsDataCenter = new Vector3(trueWidth / 2f, trueHeight / 2f, 0f); def.untrimmedBoundsDataExtents = new Vector3(trueWidth, trueHeight, 0f); def.position0 = new Vector3(0f, 0f, 0f); def.position1 = new Vector3(0f + trueWidth, 0f, 0f); def.position2 = new Vector3(0f, 0f + trueHeight, 0f); def.position3 = new Vector3(0f + trueWidth, 0f + trueHeight, 0f); def.colliderVertices[1].x = trueWidth; def.colliderVertices[1].y = trueHeight; def.name = name; VFXCollection.spriteDefinitions[id] = def; }
public static void HandlePunchoutAnimations(PunchoutPlayerController player, CustomCharacterData data) { Tools.Print("Replacing punchout sprites..."); var orig = player.sprite.Collection; var copyCollection = GameObject.Instantiate(orig); GameObject.DontDestroyOnLoad(copyCollection); tk2dSpriteDefinition[] copyDefinitions = new tk2dSpriteDefinition[orig.spriteDefinitions.Length]; for (int i = 0; i < copyCollection.spriteDefinitions.Length; i++) { copyDefinitions[i] = orig.spriteDefinitions[i].Copy(); } copyCollection.spriteDefinitions = copyDefinitions; foreach (var tex in data.punchoutSprites) { var def = copyCollection.GetSpriteDefinition(tex.name); if (def != null) { def.ReplaceTexture(tex.CropWhiteSpace()); } } player.spriteAnimator.Library = GameObject.Instantiate(player.spriteAnimator.Library); GameObject.DontDestroyOnLoad(player.spriteAnimator.Library); foreach (var clip in player.spriteAnimator.Library.clips) { for (int i = 0; i < clip.frames.Length; i++) { clip.frames[i].spriteCollection = copyCollection; } } copyCollection.name = data.nameShort + " Punchout Collection"; //CharacterBuilder.storedCollections.Add(data.nameInternal, copyCollection); player.sprite.Collection = copyCollection; Tools.Print("Punchout sprites successfully replaced"); }
public static void AddAnimationToObject(this GameObject target, tk2dSpriteCollectionData spriteCollection, string animationName, List <string> spritePaths, int fps, Vector2 colliderDimensions, Vector2 colliderOffsets, tk2dBaseSprite.Anchor anchor, tk2dSpriteAnimationClip.WrapMode wrapMode, bool isDefaultAnimation = false) { tk2dSpriteAnimator animator = target.GetOrAddComponent <tk2dSpriteAnimator>(); tk2dSpriteAnimation animation = target.GetOrAddComponent <tk2dSpriteAnimation>(); animation.clips = new tk2dSpriteAnimationClip[0]; animator.Library = animation; tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = animationName, frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in spritePaths) { tk2dSpriteCollectionData collection = spriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = new Vector3[] { new Vector3(colliderOffsets.x / 16, colliderOffsets.y / 16, 0f), new Vector3((colliderDimensions.x / 16), (colliderDimensions.y / 16), 0f) }; frameDef.ConstructOffsetsFromAnchor(anchor); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); clip.wrapMode = wrapMode; animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); if (isDefaultAnimation) { animator.DefaultClipId = animation.GetClipIdByName(animationName); animator.playAutomatically = true; } }
public void DrawSpriteTexture(Rect rect, tk2dSpriteDefinition def, Color tint) { Vector2 pixelSize = new Vector3( rect.width / def.untrimmedBoundsData[1].x, rect.height / def.untrimmedBoundsData[1].y); Rect visibleRect = VisibleRect; Vector4 clipRegion = new Vector4(visibleRect.x, visibleRect.y, visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height); bool visible = true; if (rect.xMin > visibleRect.xMax || rect.yMin > visibleRect.yMax || rect.xMax < visibleRect.xMin || rect.yMax < visibleRect.yMin) visible = false; if (Event.current.type == EventType.Repaint && visible) { if (def.material != null) { Mesh tmpMesh = new Mesh(); tmpMesh.vertices = def.positions; tmpMesh.uv = def.uvs; tmpMesh.triangles = def.indices; tmpMesh.RecalculateBounds(); tmpMesh.RecalculateNormals(); Vector3 t = def.untrimmedBoundsData[1] * 0.5f - def.untrimmedBoundsData[0]; float tq = def.untrimmedBoundsData[1].y; mat.mainTexture = def.material.mainTexture; mat.SetColor("_Tint", tint); mat.SetVector("_Clip", clipRegion); Matrix4x4 m = new Matrix4x4(); m.SetTRS(new Vector3(rect.x + t.x * pixelSize.x, rect.y + (tq - t.y) * pixelSize.y, 0), Quaternion.identity, new Vector3(pixelSize.x, -pixelSize.y, 1)); mat.SetPass(0); Graphics.DrawMeshNow(tmpMesh, m * GUI.matrix); Object.DestroyImmediate(tmpMesh); } } }
// mostly taken from the custom character mod itsel // taken from the custom character mod itself public static tk2dSpriteDefinition Copy(tk2dSpriteDefinition orig) { tk2dSpriteDefinition copy = new tk2dSpriteDefinition() { material = orig.material != null ? new Material(orig.material) : null, materialInst = orig.materialInst != null ? new Material(orig.materialInst) : null, boundsDataCenter = orig.boundsDataCenter, boundsDataExtents = orig.boundsDataExtents, colliderConvex = orig.colliderConvex, colliderSmoothSphereCollisions = orig.colliderSmoothSphereCollisions, colliderType = orig.colliderType, colliderVertices = orig.colliderVertices, collisionLayer = orig.collisionLayer, complexGeometry = orig.complexGeometry, extractRegion = orig.extractRegion, flipped = orig.flipped, indices = orig.indices, materialId = orig.materialId, metadata = orig.metadata, name = orig.name, normals = orig.normals, physicsEngine = orig.physicsEngine, position0 = orig.position0, position1 = orig.position1, position2 = orig.position2, position3 = orig.position3, regionH = orig.regionH, regionW = orig.regionW, regionX = orig.regionX, regionY = orig.regionY, tangents = orig.tangents, texelSize = orig.texelSize, untrimmedBoundsDataCenter = orig.untrimmedBoundsDataCenter, untrimmedBoundsDataExtents = orig.untrimmedBoundsDataExtents, uvs = orig.uvs }; return(copy); }
void Awake () { x = transform.localPosition.x; y = transform.localPosition.y; z = transform.localPosition.z; initPos = transform.position; def = GetComponent<tk2dSprite>().CurrentSprite; float w = def.untrimmedBoundsData[1].x; float h = def.untrimmedBoundsData[1].y; // Calculate dimensions in pixel units pixelWidth = w / def.texelSize.x; pixelHeight = h / def.texelSize.y; spriteWidth = pixelWidth * GetComponent<tk2dSprite>().scale.x * spriteScale; spriteHeight = pixelHeight * GetComponent<tk2dSprite>().scale.y * spriteScale; Debug.Log("s width" + spriteWidth ); }
// Draws the sprite texture in the rect given // Will center the sprite in the rect, regardless of anchor set-up public void DrawSpriteTextureInRect(Rect rect, tk2dSpriteDefinition def, Color tint) { if (Event.current.type == EventType.Repaint) { float sw = def.untrimmedBoundsData[1].x; float sh = def.untrimmedBoundsData[1].y; float s_epsilon = 0.00001f; float tileSize = Mathf.Min(rect.width, rect.height); Rect spriteRect = rect; if (sw > s_epsilon && sh > s_epsilon) { // rescale retaining aspect ratio if (sw > sh) spriteRect = new Rect(rect.x, rect.y, tileSize, tileSize * sh / sw); else spriteRect = new Rect(rect.x, rect.y, tileSize * sw / sh, tileSize); spriteRect.x = rect.x + (tileSize - spriteRect.width) / 2; spriteRect.y = rect.y + (tileSize - spriteRect.height) / 2; } DrawSpriteTexture(spriteRect, def, tint); } }
void Awake() { x = transform.localPosition.x; y = transform.localPosition.y; z = transform.localPosition.z; initPos = transform.position; def = GetComponent <tk2dSprite>().CurrentSprite; float w = def.untrimmedBoundsData[1].x; float h = def.untrimmedBoundsData[1].y; // Calculate dimensions in pixel units pixelWidth = w / def.texelSize.x; pixelHeight = h / def.texelSize.y; spriteWidth = pixelWidth * GetComponent <tk2dSprite>().scale.x *spriteScale; spriteHeight = pixelHeight * GetComponent <tk2dSprite>().scale.y *spriteScale; Debug.Log("s width" + spriteWidth); }
public static void DrawSpriteTextureCentered(Rect rect, tk2dSpriteDefinition def, Vector2 translate, float scale, Color tint) { Init(); Vector2 pixelSize = new Vector3(1.0f / def.texelSize.x, 1.0f / def.texelSize.y); Rect visibleRect = VisibleRect; visibleRect = rect; Vector4 clipRegion = new Vector4(visibleRect.x, visibleRect.y, visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height); bool visible = true; // if (rect.xMin > visibleRect.xMax || rect.yMin > visibleRect.yMax || // rect.xMax < visibleRect.xMin || rect.yMax < visibleRect.yMin) // visible = false; if (Event.current.type == EventType.Repaint && visible) { Mesh tmpMesh = new Mesh(); tmpMesh.vertices = def.positions; tmpMesh.uv = def.uvs; tmpMesh.triangles = def.indices; tmpMesh.RecalculateBounds(); tmpMesh.RecalculateNormals(); mat.mainTexture = def.material.mainTexture; mat.SetColor("_Tint", tint); mat.SetVector("_Clip", clipRegion); Matrix4x4 m = new Matrix4x4(); m.SetTRS(new Vector3(rect.x + rect.width / 2.0f + translate.x, rect.y + rect.height / 2.0f + translate.y, 0), Quaternion.identity, new Vector3(pixelSize.x * scale, -pixelSize.y * scale, 1)); mat.SetPass(0); Graphics.DrawMeshNow(tmpMesh, m * GUI.matrix); Object.DestroyImmediate(tmpMesh); } }
public static void SetProjectileSpriteRight(this Projectile proj, string name, int pixelWidth, int pixelHeight, int?overrideColliderPixelWidth = null, int?overrideColliderPixelHeight = null) { try { bool flag = overrideColliderPixelWidth == null; if (flag) { overrideColliderPixelWidth = new int?(pixelWidth); } bool flag2 = overrideColliderPixelHeight == null; if (flag2) { overrideColliderPixelHeight = new int?(pixelHeight); } float num = (float)pixelWidth / 16f; float num2 = (float)pixelHeight / 16f; float x = (float)overrideColliderPixelWidth.Value / 16f; float y = (float)overrideColliderPixelHeight.Value / 16f; proj.GetAnySprite().spriteId = ETGMod.Databases.Items.ProjectileCollection.inst.GetSpriteIdByName(name); tk2dSpriteDefinition tk2dSpriteDefinition = ETGMod.Databases.Items.ProjectileCollection.inst.spriteDefinitions[(PickupObjectDatabase.GetById(12) as Gun).DefaultModule.projectiles[0].GetAnySprite().spriteId].CopyDefinitionFrom(); tk2dSpriteDefinition.boundsDataCenter = new Vector3(num / 2f, num2 / 2f, 0f); tk2dSpriteDefinition.boundsDataExtents = new Vector3(num, num2, 0f); tk2dSpriteDefinition.untrimmedBoundsDataCenter = new Vector3(num / 2f, num2 / 2f, 0f); tk2dSpriteDefinition.untrimmedBoundsDataExtents = new Vector3(num, num2, 0f); tk2dSpriteDefinition.position0 = new Vector3(0f, 0f, 0f); tk2dSpriteDefinition.position1 = new Vector3(0f + num, 0f, 0f); tk2dSpriteDefinition.position2 = new Vector3(0f, 0f + num2, 0f); tk2dSpriteDefinition.position3 = new Vector3(0f + num, 0f + num2, 0f); tk2dSpriteDefinition.colliderVertices[1].x = x; tk2dSpriteDefinition.colliderVertices[1].y = y; tk2dSpriteDefinition.name = name; ETGMod.Databases.Items.ProjectileCollection.inst.spriteDefinitions[proj.GetAnySprite().spriteId] = tk2dSpriteDefinition; proj.baseData.force = 0f; } catch (Exception ex) { ETGModConsole.Log("Ooops! Seems like something got very, Very, VERY wrong. Here's the exception:", false); ETGModConsole.Log(ex.ToString(), false); } }
// Radial sprite public static void SetRadialSpriteindices(int[] indices, int offset, int vStart, tk2dSpriteDefinition spriteDef) { if (spriteDef.positions.Length == 4) { int[] inds = new int[9 * 6] { 0, 4, 1, 1, 4, 5, 1, 1, 1, 2, 2, 2, 2, 6, 3, 3, 6, 7, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 12, 9, 9, 12, 13, 9, 9, 9, 10, 10, 10, 10, 14, 11, 11, 14, 15, 5, 5, 5, 5, 5, 5 // middle bit }; int n = inds.Length; for (int i = 0; i < n; ++i) { indices[offset + i] = vStart + inds[i]; } } }
void InitForSpriteInCollection(tk2dSpriteCollectionData sc, int spriteId) { spriteCollection = sc; searchFilter = ""; UpdateFilter(); tk2dSpriteDefinition def = (spriteId >= 0 && spriteId < sc.Count) ? sc.inst.spriteDefinitions[spriteId] : null; selectedIndex = -1; for (int i = 0; i < selectedDefinitions.Count; ++i) { if (selectedDefinitions[i] == def) { selectedIndex = i; break; } } if (selectedIndex != -1) { makeSelectionVisible = true; } }
public static tk2dSpriteAnimationClip AddAnimation(tk2dSpriteAnimator animator, tk2dSpriteCollectionData collection, List <int> spriteIDs, string clipName, tk2dSpriteAnimationClip.WrapMode wrapMode = tk2dSpriteAnimationClip.WrapMode.Loop, float fps = 15) { if (animator.Library == null) { animator.Library = animator.gameObject.AddComponent <tk2dSpriteAnimation>(); animator.Library.clips = new tk2dSpriteAnimationClip[0]; animator.Library.enabled = true; } List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); for (int i = 0; i < spriteIDs.Count; i++) { tk2dSpriteDefinition sprite = collection.spriteDefinitions[spriteIDs[i]]; if (sprite.Valid) { frames.Add(new tk2dSpriteAnimationFrame() { spriteCollection = collection, spriteId = spriteIDs[i] }); } } var clip = new tk2dSpriteAnimationClip() { name = clipName, fps = fps, wrapMode = wrapMode, }; Array.Resize(ref animator.Library.clips, animator.Library.clips.Length + 1); animator.Library.clips[animator.Library.clips.Length - 1] = clip; clip.frames = frames.ToArray(); return(clip); }
public static tk2dSpriteCollectionData CloneTk2dCollection(tk2dSpriteCollectionData collection, string name = "") { if (string.IsNullOrEmpty(name)) { name = collection.spriteCollectionName; } tk2dSpriteCollectionData newCollection = UObject.Instantiate(collection); for (int i = 0; i < newCollection.materials.Length; i++) { newCollection.materials[i] = UObject.Instantiate(newCollection.materials[i]); } for (int i = 0; i < newCollection.spriteDefinitions.Length; i++) { tk2dSpriteDefinition definition = new tk2dSpriteDefinition(); if (spriteDefinitionType == null) { spriteDefinitionType = typeof(tk2dSpriteDefinition); } if (spriteDefinitionFields == null) { spriteDefinitionFields = spriteDefinitionType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); } foreach (FieldInfo fi in spriteDefinitionFields) { fi.SetValue(definition, fi.GetValue(newCollection.spriteDefinitions[i])); } definition.material = newCollection.materials[definition.materialId]; newCollection.spriteDefinitions[i] = definition; } tk2dCollections.Add(name, newCollection); newCollection.spriteCollectionName = name; newCollection.name = name + " Sprite Collection"; UObject.DontDestroyOnLoad(newCollection); return(newCollection); }
/// <summary> /// Assign the sprites from a 2D Toolkit Collection. /// </summary> public static void AssignSpritesFromTk2dCollection(Layout layout, tk2dSpriteCollectionData spriteCollection) { layout.ResetSpriteLayers(); // did we select anything? if (spriteCollection != null) { List <tk2dSpriteDefinition> spriteDefinitions = new List <tk2dSpriteDefinition>(spriteCollection.spriteDefinitions); // go through our layers foreach (Layer layer in layout.document.allLayers) { UnitySpriteLayer spriteLayer = layout.FindSpriteLayer(layer); spriteLayer.ResetSprites(); // go through the guesses for the current layer foreach (string guess in layer.GetGuessesForSpriteName()) { // find the sprite definition with this name... // HEADS UP: Use the sprite definitions list, because the GetSpriteDefinition() call // uses a cache that's stale when you click Commit from the editor. tk2dSpriteDefinition spriteDef = spriteDefinitions.Find(each => each.name == guess); // did we get it? (null && empty names... gah, this hung me up for a few hours) if (spriteDef != null && spriteDef.name != "") { // hook it up spriteLayer.tk2dSpriteName = guess; spriteLayer.spriteLayerStyle = SpriteLayerStyle.Tk2dSprite; spriteLayer.bounds = spriteDef.GetUntrimmedBounds(); spriteLayer.name = guess; break; } } } } layout.ResetHierarchyLabels(); }
void InitializeSlicedSpriteDimensions() { if (!initializedSlicedSpriteDimensions) { if (slicedSpriteBar != null) { // Until there is a better way to do this. tk2dSpriteDefinition spriteDef = slicedSpriteBar.CurrentSprite; Vector3 extents = spriteDef.boundsData[1]; fullSlicedSpriteDimensions = slicedSpriteBar.dimensions; emptySlicedSpriteDimensions.Set((slicedSpriteBar.borderLeft + slicedSpriteBar.borderRight) * extents.x / spriteDef.texelSize.x, fullSlicedSpriteDimensions.y); } initializedSlicedSpriteDimensions = true; } }
// Token: 0x06000177 RID: 375 RVA: 0x00013598 File Offset: 0x00011798 public static tk2dSpriteDefinition CopyDefinitionFrom(this tk2dSpriteDefinition other) { return(new tk2dSpriteDefinition { boundsDataCenter = other.boundsDataCenter, boundsDataExtents = other.boundsDataExtents, colliderConvex = other.colliderConvex, colliderSmoothSphereCollisions = other.colliderSmoothSphereCollisions, colliderType = other.colliderType, colliderVertices = other.colliderVertices, collisionLayer = other.collisionLayer, complexGeometry = other.complexGeometry, extractRegion = other.extractRegion, flipped = other.flipped, indices = other.indices, material = new Material(other.material), materialId = other.materialId, materialInst = new Material(other.materialInst), metadata = other.metadata, name = other.name, normals = other.normals, physicsEngine = other.physicsEngine, position0 = other.position0, position1 = other.position1, position2 = other.position2, position3 = other.position3, regionH = other.regionH, regionW = other.regionW, regionX = other.regionX, regionY = other.regionY, tangents = other.tangents, texelSize = other.texelSize, untrimmedBoundsDataCenter = other.untrimmedBoundsDataCenter, untrimmedBoundsDataExtents = other.untrimmedBoundsDataExtents, uvs = other.uvs }); }
protected override void DisplayInspectorGUI() { tk2dJellySprite targetObject = this.target as tk2dJellySprite; tk2dSpriteGuiUtility.SpriteSelector(targetObject.m_Tk2DCollection, targetObject.m_SpriteId, new tk2dSpriteGuiUtility.SpriteChangedCallback(SpriteChangedCallbackImpl), null); if (tk2dPreferences.inst.displayTextureThumbs && targetObject.m_Tk2DCollection != null) { tk2dSpriteDefinition def = targetObject.GetCurrentSpriteDef(); if (targetObject.m_Tk2DCollection.version < 1 || def.texelSize == Vector2.zero) { string message = ""; message = "No thumbnail data."; if (targetObject.m_Tk2DCollection.version < 1) { message += "\nPlease rebuild Sprite Collection."; } tk2dGuiUtility.InfoBox(message, tk2dGuiUtility.WarningLevel.Info); } else { GUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(" "); int tileSize = 128; Rect r = GUILayoutUtility.GetRect(tileSize, tileSize, GUILayout.ExpandWidth(false)); tk2dGrid.Draw(r); tk2dSpriteThumbnailCache.DrawSpriteTextureInRect(r, def, Color.white); GUILayout.EndHorizontal(); } } base.DisplayInspectorGUI(); }
// Token: 0x0600004D RID: 77 RVA: 0x0000402C File Offset: 0x0000222C public static tk2dSpriteAnimationClip AddAnimation(tk2dSpriteAnimator animator, tk2dSpriteCollectionData collection, List <int> spriteIDs, string clipName, tk2dSpriteAnimationClip.WrapMode wrapMode = tk2dSpriteAnimationClip.WrapMode.Loop) { bool flag = animator.Library == null; if (flag) { animator.Library = animator.gameObject.AddComponent <tk2dSpriteAnimation>(); animator.Library.clips = new tk2dSpriteAnimationClip[0]; animator.Library.enabled = true; } List <tk2dSpriteAnimationFrame> list = new List <tk2dSpriteAnimationFrame>(); for (int i = 0; i < spriteIDs.Count; i++) { tk2dSpriteDefinition tk2dSpriteDefinition = collection.spriteDefinitions[spriteIDs[i]]; bool valid = tk2dSpriteDefinition.Valid; if (valid) { list.Add(new tk2dSpriteAnimationFrame { spriteCollection = collection, spriteId = spriteIDs[i] }); } } tk2dSpriteAnimationClip tk2dSpriteAnimationClip = new tk2dSpriteAnimationClip { name = clipName, fps = 15f, wrapMode = wrapMode }; Array.Resize <tk2dSpriteAnimationClip>(ref animator.Library.clips, animator.Library.clips.Length + 1); animator.Library.clips[animator.Library.clips.Length - 1] = tk2dSpriteAnimationClip; tk2dSpriteAnimationClip.frames = list.ToArray(); return(tk2dSpriteAnimationClip); }
/// <summary> /// Let's attach a tk2d sprite. /// </summary> /// <param name="targetGameObject">The game object to attach to.</param> public void AttachTk2dSprite(GameObject targetGameObject, tk2dSpriteCollectionData spriteCollectionData) { // sanity. if (targetGameObject == null || tk2dSpriteName == null || spriteCollectionData == null) { return; } // triple check the sprite definitions... this is madness... this is SPARTA! List <tk2dSpriteDefinition> spriteDefinitions = new List <tk2dSpriteDefinition>(spriteCollectionData.spriteDefinitions); tk2dSpriteDefinition spriteDef = spriteDefinitions.Find(each => each.name == tk2dSpriteName); if (spriteDef == null || spriteDef.name == "") { // this condition should never happen because we're finding the definition by name... // if we do trip it up, then, at least don't die a horrible death. return; } // lookup the index int spriteIndex = spriteDefinitions.IndexOf(spriteDef); // create a tk2dsprite tk2dSprite spriteComponent = targetGameObject.AddComponent <tk2dSprite>() as tk2dSprite; // set the sprite based on the index because it's safer that way... :( spriteComponent.SetSprite(spriteCollectionData, spriteIndex); // update the sorting layer spriteComponent.GetComponent <Renderer>().sortingLayerName = sortingLayerName; spriteComponent.SortingOrder = sortingOrder; spriteComponent.Build(); // set visibility spriteComponent.GetComponent <Renderer>().enabled = isVisible; }
public static AssetSpriteData FromTK2D(tk2dSpriteCollectionData sprites, tk2dSpriteDefinition frame, bool separate = false) { if (sprites.materials[0]?.mainTexture == null) { return(new AssetSpriteData { name = separate ? "INCOMPLETE" : (frame.name + "_INCOMPLETE") }); } int texWidth = sprites.materials[0].mainTexture.width; int texHeight = sprites.materials[0].mainTexture.height; return(new AssetSpriteData { name = separate ? null : frame.name, x = separate ? 0 : (int)Math.Floor(texWidth * frame.uvs[0].x), y = separate ? 0 : (int)Math.Floor(texHeight * frame.uvs[0].y), width = (int)Math.Ceiling(texWidth * (frame.uvs[3].x - frame.uvs[0].x)), height = (int)Math.Ceiling(texHeight * (frame.uvs[3].y - frame.uvs[0].y)), flip = frame.uvs[0].x == frame.uvs[1].x ? 1 : 0, attachPoints = sprites.GetAttachPoints(sprites.spriteDefinitions.IndexOf(frame)) ?? new AttachPoint[0] }); }
public static void ConstructOffsetsFromAnchor(this tk2dSpriteDefinition def, tk2dBaseSprite.Anchor anchor, Vector2 scale, bool changesCollider = true) { float xOffset = 0; if (anchor == tk2dBaseSprite.Anchor.LowerCenter || anchor == tk2dBaseSprite.Anchor.MiddleCenter || anchor == tk2dBaseSprite.Anchor.UpperCenter) { xOffset = -(scale.x / 2f); } else if (anchor == tk2dBaseSprite.Anchor.LowerRight || anchor == tk2dBaseSprite.Anchor.MiddleRight || anchor == tk2dBaseSprite.Anchor.UpperRight) { xOffset = -scale.x; } float yOffset = 0; if (anchor == tk2dBaseSprite.Anchor.MiddleLeft || anchor == tk2dBaseSprite.Anchor.MiddleCenter || anchor == tk2dBaseSprite.Anchor.MiddleLeft) { yOffset = -(scale.y / 2f); } else if (anchor == tk2dBaseSprite.Anchor.UpperLeft || anchor == tk2dBaseSprite.Anchor.UpperCenter || anchor == tk2dBaseSprite.Anchor.UpperRight) { yOffset = -scale.y; } def.MakeOffset(new Vector2(xOffset, yOffset), changesCollider); }
public static tk2dSpriteAnimationClip AddAnimation(tk2dSpriteAnimator animator, tk2dSpriteCollectionData collection, List <int> spriteIDs, string clipName, tk2dSpriteAnimationClip.WrapMode wrapMode = tk2dSpriteAnimationClip.WrapMode.Loop, int frameRate = 15, int loopStart = 0, float minFidgetDuration = 0.5f, float maxFidgetDuration = 1) { if (animator.Library == null) { animator.Library = animator.gameObject.AddComponent <tk2dSpriteAnimation>(); animator.Library.clips = new tk2dSpriteAnimationClip[0]; } List <tk2dSpriteAnimationFrame> list = new List <tk2dSpriteAnimationFrame>(); for (int i = 0; i < spriteIDs.Count; i++) { tk2dSpriteDefinition spriteDefinition = collection.spriteDefinitions[spriteIDs[i]]; if (spriteDefinition.Valid) { list.Add(new tk2dSpriteAnimationFrame { spriteCollection = collection, spriteId = spriteIDs[i], invulnerableFrame = false }); } } tk2dSpriteAnimationClip animationClip = new tk2dSpriteAnimationClip() { name = clipName, frames = list.ToArray(), fps = frameRate, wrapMode = wrapMode, loopStart = loopStart, minFidgetDuration = minFidgetDuration, maxFidgetDuration = maxFidgetDuration, }; Array.Resize(ref animator.Library.clips, animator.Library.clips.Length + 1); animator.Library.clips[animator.Library.clips.Length - 1] = animationClip; return(animationClip); }
public static Vector3 ApplySpriteVertexTileFlags(tk2dTileMap tileMap, tk2dSpriteDefinition spriteDef, Vector3 pos, bool flipH, bool flipV, bool rot90) { float num = tileMap.data.tileOrigin.x + (0.5f * tileMap.data.tileSize.x); float num2 = tileMap.data.tileOrigin.y + (0.5f * tileMap.data.tileSize.y); float num3 = pos.x - num; float num4 = pos.y - num2; if (rot90) { float num5 = num3; num3 = num4; num4 = -num5; } if (flipH) { num3 *= -1f; } if (flipV) { num4 *= -1f; } pos.x = num + num3; pos.y = num2 + num4; return pos; }
void PermanentUpgradeLegacyMode() { tk2dSpriteDefinition def = CurrentSprite; // Guess anchor float x = def.untrimmedBoundsData[0].x; float y = def.untrimmedBoundsData[0].y; float w = def.untrimmedBoundsData[1].x; float h = def.untrimmedBoundsData[1].y; if (NearEnough(x, 0, w) && NearEnough(y, -h / 2, h)) { _anchor = tk2dBaseSprite.Anchor.UpperCenter; } else if (NearEnough(x, 0, w) && NearEnough(y, 0, h)) { _anchor = tk2dBaseSprite.Anchor.MiddleCenter; } else if (NearEnough(x, 0, w) && NearEnough(y, h / 2, h)) { _anchor = tk2dBaseSprite.Anchor.LowerCenter; } else if (NearEnough(x, -w / 2, w) && NearEnough(y, -h / 2, h)) { _anchor = tk2dBaseSprite.Anchor.UpperRight; } else if (NearEnough(x, -w / 2, w) && NearEnough(y, 0, h)) { _anchor = tk2dBaseSprite.Anchor.MiddleRight; } else if (NearEnough(x, -w / 2, w) && NearEnough(y, h / 2, h)) { _anchor = tk2dBaseSprite.Anchor.LowerRight; } else if (NearEnough(x, w / 2, w) && NearEnough(y, -h / 2, h)) { _anchor = tk2dBaseSprite.Anchor.UpperLeft; } else if (NearEnough(x, w / 2, w) && NearEnough(y, 0, h)) { _anchor = tk2dBaseSprite.Anchor.MiddleLeft; } else if (NearEnough(x, w / 2, w) && NearEnough(y, h / 2, h)) { _anchor = tk2dBaseSprite.Anchor.LowerLeft; } else { Debug.LogError("tk2dSlicedSprite (" + name + ") error - Unable to determine anchor upgrading from legacy mode. Please fix this manually."); _anchor = tk2dBaseSprite.Anchor.MiddleCenter; } // Calculate dimensions in pixel units float pixelWidth = w / def.texelSize.x; float pixelHeight = h / def.texelSize.y; _dimensions.x = _scale.x * pixelWidth; _dimensions.y = _scale.y * pixelHeight; _scale.Set(1, 1, 1); legacyMode = false; }
public static void GetSpriteGeomDesc(out int numVertices, out int numIndices, tk2dSpriteDefinition spriteDef) { numVertices = spriteDef.positions.Length; numIndices = spriteDef.indices.Length; }
public static void SetTiledSpriteIndices(int[] indices, int offset, int vStart, tk2dSpriteDefinition spriteDef, Vector2 dimensions) { int num; int num2; GetTiledSpriteGeomDesc(out num, out num2, spriteDef, dimensions); int num3 = 0; for (int i = 0; i < num2; i += 6) { indices[offset + i] = (vStart + spriteDef.indices[0]) + num3; indices[(offset + i) + 1] = (vStart + spriteDef.indices[1]) + num3; indices[(offset + i) + 2] = (vStart + spriteDef.indices[2]) + num3; indices[(offset + i) + 3] = (vStart + spriteDef.indices[3]) + num3; indices[(offset + i) + 4] = (vStart + spriteDef.indices[4]) + num3; indices[(offset + i) + 5] = (vStart + spriteDef.indices[5]) + num3; num3 += 4; } }
public static void GetTiledSpriteGeomDesc(out int numVertices, out int numIndices, tk2dSpriteDefinition spriteDef, Vector2 dimensions) { int num = (int) Mathf.Ceil((dimensions.x * spriteDef.texelSize.x) / spriteDef.untrimmedBoundsData[1].x); int num2 = (int) Mathf.Ceil((dimensions.y * spriteDef.texelSize.y) / spriteDef.untrimmedBoundsData[1].y); numVertices = (num * num2) * 4; numIndices = (num * num2) * 6; }
static tk2dSpriteDefinition CreateDefinitionForRegionInTexture(string name, Vector2 textureDimensions, float scale, Rect uvRegion, Rect trimRect, Vector2 anchor, bool rotated) { float h = uvRegion.height; float w = uvRegion.width; float fwidth = textureDimensions.x; float fheight = textureDimensions.y; var def = new tk2dSpriteDefinition(); def.flipped = rotated ? tk2dSpriteDefinition.FlipMode.TPackerCW : tk2dSpriteDefinition.FlipMode.None; def.extractRegion = false; def.name = name; def.colliderType = tk2dSpriteDefinition.ColliderType.Unset; Vector2 uvOffset = new Vector2(0.001f, 0.001f); Vector2 v0 = new Vector2((uvRegion.x + uvOffset.x) / fwidth, 1.0f - (uvRegion.y + uvRegion.height + uvOffset.y) / fheight); Vector2 v1 = new Vector2((uvRegion.x + uvRegion.width - uvOffset.x) / fwidth, 1.0f - (uvRegion.y - uvOffset.y) / fheight); // Correction offset to make the sprite correctly centered at 0, 0 Vector2 offset = new Vector2( trimRect.x - anchor.x, -trimRect.y + anchor.y ); if (rotated) { offset.y -= w; } offset *= scale; Vector3 b0 = new Vector3( -anchor.x * scale, anchor.y * scale, 0 ); Vector3 b1 = b0 + new Vector3( trimRect.width * scale, -trimRect.height * scale, 0 ); Vector3 pos0 = new Vector3(0, -h * scale, 0.0f); Vector3 pos1 = pos0 + new Vector3(w * scale, h * scale, 0.0f); if (rotated) { def.positions = new Vector3[] { new Vector3(-pos1.y + offset.x, pos0.x + offset.y, 0), new Vector3(-pos0.y + offset.x, pos0.x + offset.y, 0), new Vector3(-pos1.y + offset.x, pos1.x + offset.y, 0), new Vector3(-pos0.y + offset.x, pos1.x + offset.y, 0), }; def.uvs = new Vector2[] { new Vector2(v0.x,v1.y), new Vector2(v0.x,v0.y), new Vector2(v1.x,v1.y), new Vector2(v1.x,v0.y), }; } else { def.positions = new Vector3[] { new Vector3(pos0.x + offset.x, pos0.y + offset.y, 0), new Vector3(pos1.x + offset.x, pos0.y + offset.y, 0), new Vector3(pos0.x + offset.x, pos1.y + offset.y, 0), new Vector3(pos1.x + offset.x, pos1.y + offset.y, 0) }; def.uvs = new Vector2[] { new Vector2(v0.x,v0.y), new Vector2(v1.x,v0.y), new Vector2(v0.x,v1.y), new Vector2(v1.x,v1.y) }; } def.normals = new Vector3[0]; def.tangents = new Vector4[0]; def.indices = new int[] { 0, 3, 1, 2, 3, 0 }; Vector3 boundsMin = new Vector3(b0.x, b1.y, 0); Vector3 boundsMax = new Vector3(b1.x, b0.y, 0); // todo - calc trimmed bounds properly def.boundsData = new Vector3[2] { (boundsMax + boundsMin) / 2.0f, (boundsMax - boundsMin) }; def.untrimmedBoundsData = new Vector3[2] { (boundsMax + boundsMin) / 2.0f, (boundsMax - boundsMin) }; def.texelSize = new Vector2(scale, scale); return def; }
public void CopyFrom(tk2dSpriteDefinition.AttachPoint src) { this.name = src.name; this.position = src.position; this.angle = src.angle; }
public bool CompareTo(tk2dSpriteDefinition.AttachPoint src) { return (((this.name == src.name) && (src.position == this.position)) && (src.angle == this.angle)); }
// Finds a sprite with the name and id // matches "baseName" [ 0..9 ]* as id int FindFrameIndex(tk2dSpriteDefinition[] spriteDefs, string baseName, int frameId) { for (int j = 0; j < spriteDefs.Length; ++j) { if (System.String.Compare(baseName, 0, spriteDefs[j].name, 0, baseName.Length, true) == 0) { int thisFrameId = 0; if (System.Int32.TryParse( spriteDefs[j].name.Substring(baseName.Length), out thisFrameId ) && thisFrameId == frameId) { return j; } } } return -1; }
private void UpdateAttachPointTransform(tk2dSpriteDefinition.AttachPoint attachPoint, Transform t) { t.localPosition = Vector3.Scale(attachPoint.position, this.sprite.scale); t.localScale = this.sprite.scale; float num = Mathf.Sign(this.sprite.scale.x) * Mathf.Sign(this.sprite.scale.y); t.localEulerAngles = new Vector3(0f, 0f, attachPoint.angle * num); }
// Finds a sprite with the name and id // matches "baseName" [ 0..9 ]* as id // todo rewrite with regex int GetFrameIndex(tk2dSpriteDefinition def, string baseName) { if (System.String.Compare(baseName, 0, def.name, 0, baseName.Length, true) == 0) { int thisFrameId = 0; if (System.Int32.TryParse( def.name.Substring(baseName.Length), out thisFrameId )) { return thisFrameId; } } return -1; }
public static Vector3 FlipSpriteVertexPosition(tk2dTileMap tileMap, tk2dSpriteDefinition spriteDef, Vector3 pos, bool flipH, bool flipV) { float cx = tileMap.data.tileOrigin.x + 0.5f * tileMap.data.tileSize.x; float cy = tileMap.data.tileOrigin.y + 0.5f * tileMap.data.tileSize.y; if (flipH) pos.x = cx - (pos.x - cx); if (flipV) pos.y = cy - (pos.y - cy); return pos; }
int FindFrameIndex(tk2dSpriteDefinition[] spriteDefs, string baseName, int frameId) { for (int j = 0; j < spriteDefs.Length; ++j) { if (GetFrameIndex(spriteDefs[j], baseName) == frameId) return j; } return -1; }
static tk2dSpriteDefinition CreateDefinitionForRegionInTexture(Texture2D texture, float scale, Rect uvRegion, Vector2 anchor) { float h = uvRegion.height; float w = uvRegion.width; Vector3 pos0 = new Vector3(-anchor.x * scale, -(h - anchor.y) * scale, 0.0f); Vector3 pos1 = pos0 + new Vector3(w * scale, h * scale, 0.0f); var def = new tk2dSpriteDefinition(); def.flipped = false; def.extractRegion = false; def.name = texture.name; def.colliderType = tk2dSpriteDefinition.ColliderType.Unset; float fwidth = texture.width; float fheight = texture.height; Vector2 uvOffset = new Vector2(0.001f, 0.001f); Vector2 v0 = new Vector2((uvRegion.x + uvOffset.x) / fwidth, 1.0f - (uvRegion.y + uvRegion.height + uvOffset.y) / fheight); Vector2 v1 = new Vector2((uvRegion.x + uvRegion.width - uvOffset.x) / fwidth, 1.0f - (uvRegion.y - uvOffset.y) / fheight); Vector3 untrimmedPos0 = pos0; Vector3 untrimmedPos1 = pos1; def.positions = new Vector3[] { new Vector3(pos0.x, pos0.y, 0), new Vector3(pos1.x, pos0.y, 0), new Vector3(pos0.x, pos1.y, 0), new Vector3(pos1.x, pos1.y, 0) }; def.uvs = new Vector2[] { new Vector2(v0.x,v0.y), new Vector2(v1.x,v0.y), new Vector2(v0.x,v1.y), new Vector2(v1.x,v1.y) }; def.normals = new Vector3[0]; def.tangents = new Vector4[0]; def.indices = new int[] { 0, 3, 1, 2, 3, 0 }; Vector3 boundsMin = Vector3.Min(untrimmedPos0, untrimmedPos1); Vector3 boundsMax = Vector3.Max(untrimmedPos0, untrimmedPos1); def.boundsData = new Vector3[2] { (boundsMax + boundsMin) / 2.0f, (boundsMax - boundsMin) }; def.untrimmedBoundsData = new Vector3[2] { (boundsMax + boundsMin) / 2.0f, (boundsMax - boundsMin) }; def.texelSize = new Vector2(scale, scale); return def; }
public Vector2 GetSpriteSizePixels(tk2dSpriteDefinition def) { return new Vector2(def.untrimmedBoundsData[1].x / def.texelSize.x, def.untrimmedBoundsData[1].y / def.texelSize.y); }
public void DrawSpriteTexture(Rect rect, tk2dSpriteDefinition def) { DrawSpriteTexture(rect, def, Color.white); }
public void DrawSpriteTextureCentered(Rect rect, tk2dSpriteDefinition def, Vector2 translate, float scale, Color tint) { Vector2 pixelSize = new Vector3( 1.0f / def.texelSize.x, 1.0f / def.texelSize.y); Rect visibleRect = VisibleRect; visibleRect = rect; Vector4 clipRegion = new Vector4(visibleRect.x, visibleRect.y, visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height); bool visible = true; // if (rect.xMin > visibleRect.xMax || rect.yMin > visibleRect.yMax || // rect.xMax < visibleRect.xMin || rect.yMax < visibleRect.yMin) // visible = false; if (Event.current.type == EventType.Repaint && visible) { Mesh tmpMesh = new Mesh(); tmpMesh.vertices = def.positions; tmpMesh.uv = def.uvs; tmpMesh.triangles = def.indices; tmpMesh.RecalculateBounds(); tmpMesh.RecalculateNormals(); mat.mainTexture = def.material.mainTexture; mat.SetColor("_Tint", tint); mat.SetVector("_Clip", clipRegion); Matrix4x4 m = new Matrix4x4(); m.SetTRS(new Vector3(rect.x + rect.width / 2.0f + translate.x, rect.y + rect.height / 2.0f + translate.y, 0), Quaternion.identity, new Vector3(pixelSize.x * scale, -pixelSize.y * scale, 1)); mat.SetPass(0); Graphics.DrawMeshNow(tmpMesh, m * GUI.matrix); Object.DestroyImmediate(tmpMesh); } }