public static tk2dAnimatedSprite AddComponent(GameObject go, tk2dSpriteAnimation anim, int clipId) { tk2dSpriteAnimationClip clip = anim.clips[clipId]; tk2dAnimatedSprite sprite = go.AddComponent<tk2dAnimatedSprite>(); sprite.SetSprite(clip.frames[0].spriteCollection, clip.frames[0].spriteId); sprite.anim = anim; return sprite; }
public static tk2dSpriteAnimator AddComponent(GameObject go, tk2dSpriteAnimation anim, int clipId) { tk2dSpriteAnimationClip clip = anim.clips[clipId]; tk2dSpriteAnimator animator = go.AddComponent<tk2dSpriteAnimator>(); animator.Library = anim; animator.SetSprite(clip.frames[0].spriteCollection, clip.frames[0].spriteId); return animator; }
public ClipData(PlatformerSpriteController ctrl, tk2dSpriteAnimation lib) { idle = lib.GetClipByName(ctrl.idleClip); move = lib.GetClipByName(ctrl.moveClip); ups = new tk2dSpriteAnimationClip[ctrl.upClips.Length]; for(int i = 0, len = ctrl.upClips.Length; i < len; i++) ups[i] = lib.GetClipByName(ctrl.upClips[i]); downs = new tk2dSpriteAnimationClip[ctrl.downClips.Length]; for(int i = 0, len = ctrl.downClips.Length; i < len; i++) downs[i] = lib.GetClipByName(ctrl.downClips[i]); wallStick = lib.GetClipByName(ctrl.wallStickClip); wallJump = lib.GetClipByName(ctrl.wallJumpClip); slide = lib.GetClipByName(ctrl.slideClip); //climb = lib.GetClipByName(ctrl.climbClip); }
public void AddSpriteAnimation(tk2dSpriteAnimation sc) { spriteAnimations.RemoveAll(item => item == null); foreach (var v in spriteAnimations) if (v == sc) return; spriteAnimations.Add(sc); }
public void SetSpriteAnimation(tk2dSpriteAnimation anim) { if (anim != this.anim) { searchFilter = ""; this.anim = anim; this.name = anim.name; } selectedClip = null; MirrorClips(); FilterClips(); Repaint(); }
static void CreateIndex() { newIndex = ScriptableObject.CreateInstance <tk2dIndex>(); newIndex.version = tk2dIndex.CURRENT_VERSION; List <string> rebuildSpriteCollectionPaths = new List <string>(); // check all prefabs to see if we can find any objects we are interested in List <string> allPrefabPaths = new List <string>(); Stack <string> paths = new Stack <string>(); paths.Push(Application.dataPath); while (paths.Count != 0) { string path = paths.Pop(); string[] files = Directory.GetFiles(path, "*.prefab"); foreach (var file in files) { allPrefabPaths.Add(file.Substring(Application.dataPath.Length - 6)); } foreach (string subdirs in Directory.GetDirectories(path)) { paths.Push(subdirs); } } Debug.Log("hi " + newIndex); // Check all prefabs int currPrefabCount = 1; foreach (string prefabPath in allPrefabPaths) { EditorUtility.DisplayProgressBar("Rebuilding Index", "Scanning project folder...", (float)currPrefabCount / (float)(allPrefabPaths.Count)); GameObject iterGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; if (!iterGo) { continue; } tk2dSpriteCollection spriteCollection = iterGo.GetComponent <tk2dSpriteCollection>(); tk2dSpriteCollectionData spriteCollectionData = iterGo.GetComponent <tk2dSpriteCollectionData>(); tk2dFont font = iterGo.GetComponent <tk2dFont>(); tk2dSpriteAnimation anim = iterGo.GetComponent <tk2dSpriteAnimation>(); Debug.Log(" prefabPath " + prefabPath + " " + newIndex); if (spriteCollection) { Debug.Log(" prefabPath a " + prefabPath + " " + newIndex); tk2dSpriteCollectionData thisSpriteCollectionData = spriteCollection.spriteCollection; if (thisSpriteCollectionData) { if (thisSpriteCollectionData.version < 1) { rebuildSpriteCollectionPaths.Add(AssetDatabase.GetAssetPath(spriteCollection)); } newIndex.AddSpriteCollectionData(thisSpriteCollectionData); } Debug.Log(" prefabPath x " + prefabPath + " " + newIndex); } else if (spriteCollectionData) { Debug.Log(" prefabPath b " + prefabPath + " " + newIndex); string guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(spriteCollectionData)); bool present = false; foreach (var v in newIndex.GetSpriteCollectionIndex()) { if (v.spriteCollectionDataGUID == guid) { present = true; break; } } if (!present && guid != "") { newIndex.AddSpriteCollectionData(spriteCollectionData); } } else if (font) { newIndex.AddOrUpdateFont(font); // unfortunate but necessary } else if (anim) { newIndex.AddSpriteAnimation(anim); } else { iterGo = null; System.GC.Collect(); } Debug.Log(" prefabPath y " + prefabPath + " " + newIndex); //tk2dEditorUtility.UnloadUnusedAssets(); //todo Debug.Log(" prefabPath z " + prefabPath + " " + newIndex); ++currPrefabCount; } EditorUtility.ClearProgressBar(); Debug.Log("hey " + newIndex); // Create index AssetDatabase.CreateAsset(newIndex, indexPath); // unload all unused assets tk2dEditorUtility.UnloadUnusedAssets(); // Rebuild invalid sprite collections if (rebuildSpriteCollectionPaths.Count > 0) { EditorUtility.DisplayDialog("Upgrade required", "Please wait while your sprite collection is upgraded.", "Ok"); int count = 1; foreach (var scPath in rebuildSpriteCollectionPaths) { tk2dSpriteCollection sc = AssetDatabase.LoadAssetAtPath(scPath, typeof(tk2dSpriteCollection)) as tk2dSpriteCollection; EditorUtility.DisplayProgressBar("Rebuilding Sprite Collections", "Rebuilding Sprite Collection: " + sc.name, (float)count / (float)(rebuildSpriteCollectionPaths.Count)); tk2dSpriteCollectionBuilder.Rebuild(sc); sc = null; tk2dEditorUtility.UnloadUnusedAssets(); ++count; } EditorUtility.ClearProgressBar(); } index = newIndex; tk2dSpriteGuiUtility.ResetCache(); }
public override void OnInspectorGUI() { InitializeInspector(); if (spriteCollectionIndex == null || allSpriteCollectionNames == null) { GUILayout.Label("data not found"); if (GUILayout.Button("Refresh")) { initialized = false; InitializeInspector(); } return; } tk2dSpriteAnimation anim = (tk2dSpriteAnimation)target; EditorGUI.indentLevel++; EditorGUILayout.BeginVertical(); if (anim.clips.Length == 0) { if (GUILayout.Button("Add clip")) { anim.clips = new tk2dSpriteAnimationClip[1]; anim.clips[0] = new tk2dSpriteAnimationClip(); anim.clips[0].name = "New Clip 0"; anim.clips[0].frames = new tk2dSpriteAnimationFrame[1]; anim.clips[0].frames[0] = new tk2dSpriteAnimationFrame(); var spriteCollection = tk2dSpriteGuiUtility.GetDefaultSpriteCollection(); anim.clips[0].frames[0].spriteCollection = spriteCollection; anim.clips[0].frames[0].spriteId = spriteCollection.FirstValidDefinitionIndex; } } else // has anim clips { // All clips string[] allClipNames = new string[anim.clips.Length]; for (int i = 0; i < anim.clips.Length; ++i) { allClipNames[i] = anim.clips[i].name; } currentClip = Mathf.Clamp(currentClip, 0, anim.clips.Length); #region AddAndDeleteClipButtons EditorGUILayout.BeginHorizontal(); currentClip = EditorGUILayout.Popup("Clips", currentClip, allClipNames); // Add new clip if (GUILayout.Button("+", GUILayout.MaxWidth(28), GUILayout.MaxHeight(14))) { int previousClipId = currentClip; // try to find an empty slot currentClip = -1; for (int i = 0; i < anim.clips.Length; ++i) { if (anim.clips[i].name.Length == 0) { currentClip = i; break; } } if (currentClip == -1) { tk2dSpriteAnimationClip[] clips = new tk2dSpriteAnimationClip[anim.clips.Length + 1]; for (int i = 0; i < anim.clips.Length; ++i) { clips[i] = anim.clips[i]; } currentClip = anim.clips.Length; clips[currentClip] = new tk2dSpriteAnimationClip(); anim.clips = clips; } string uniqueName = "New Clip "; int uniqueId = 0; for (int i = 0; i < anim.clips.Length; ++i) { string uname = uniqueName + uniqueId.ToString(); if (anim.clips[i].name == uname) { uniqueId++; i = -1; continue; } } anim.clips[currentClip] = new tk2dSpriteAnimationClip(); anim.clips[currentClip].name = uniqueName + uniqueId.ToString(); anim.clips[currentClip].fps = 15; anim.clips[currentClip].wrapMode = tk2dSpriteAnimationClip.WrapMode.Loop; anim.clips[currentClip].frames = new tk2dSpriteAnimationFrame[1]; tk2dSpriteAnimationFrame frame = new tk2dSpriteAnimationFrame(); if (previousClipId < anim.clips.Length && anim.clips[previousClipId] != null && anim.clips[previousClipId].frames != null && anim.clips[previousClipId].frames.Length != 0 && anim.clips[previousClipId].frames[anim.clips[previousClipId].frames.Length - 1] != null && anim.clips[previousClipId].frames[anim.clips[previousClipId].frames.Length - 1].spriteCollection != null) { var previousClip = anim.clips[previousClipId]; var lastFrame = previousClip.frames[previousClip.frames.Length - 1]; frame.spriteCollection = lastFrame.spriteCollection; frame.spriteId = lastFrame.spriteId; } else { var spriteCollection = tk2dSpriteGuiUtility.GetDefaultSpriteCollection(); frame.spriteCollection = spriteCollection; frame.spriteId = spriteCollection.FirstValidDefinitionIndex; } anim.clips[currentClip].frames[0] = frame; GUI.changed = true; } // Delete clip if (GUILayout.Button("-", GUILayout.MaxWidth(28), GUILayout.MaxHeight(14))) { anim.clips[currentClip].name = ""; anim.clips[currentClip].frames = new tk2dSpriteAnimationFrame[0]; currentClip = 0; // find first non zero clip for (int i = 0; i < anim.clips.Length; ++i) { if (anim.clips[i].name != "") { currentClip = i; break; } } GUI.changed = true; } EditorGUILayout.EndHorizontal(); #endregion #region PruneClipList // Prune clip list int lastActiveClip = 0; for (int i = 0; i < anim.clips.Length; ++i) { if (!(anim.clips[i].name == "" && anim.clips[i].frames != null && anim.clips[i].frames.Length == 0)) { lastActiveClip = i; } } if (lastActiveClip != anim.clips.Length - 1) { System.Array.Resize <tk2dSpriteAnimationClip>(ref anim.clips, lastActiveClip + 1); GUI.changed = true; } #endregion // If anything has changed up to now, redraw if (GUI.changed) { EditorUtility.SetDirty(anim); Repaint(); return; } EditorGUI.indentLevel = 2; tk2dSpriteAnimationClip clip = anim.clips[currentClip]; // Clip properties // Name clip.name = EditorGUILayout.TextField("Name", clip.name); #region NumberOfFrames // Number of frames int clipNumFrames = (clip.frames != null)?clip.frames.Length:0; int newFrameCount = 0; if (clip.wrapMode == tk2dSpriteAnimationClip.WrapMode.Single) { newFrameCount = 1; // only one frame, no need to display } else { int maxFrameCount = 400; int startSection = Mathf.Max(clipNumFrames - 5, 1); int endSection = Mathf.Min(clipNumFrames + 5, maxFrameCount); string[] numFrameStr = new string[maxFrameCount - 1]; int div = 20; int divStart = 0; int divEnd = 0; string section = ""; for (int i = 1; i < startSection; ++i) { if (i > divEnd) { divStart = divEnd + 1; divEnd = Mathf.Min(startSection - 1, divStart + div - 1); section = divStart.ToString() + " .. " + divEnd.ToString(); } numFrameStr[i - 1] = section + "/" + i.ToString(); } for (int i = startSection; i < endSection; ++i) { numFrameStr[i - 1] = i.ToString(); } divEnd = endSection - 1; for (int i = endSection; i < maxFrameCount; ++i) { if (i > divEnd) { divStart = Mathf.Max(divEnd + 1, endSection); divEnd = Mathf.Min(((divStart + div) / div) * div, maxFrameCount - 1); section = divStart.ToString() + " .. " + divEnd.ToString(); } numFrameStr[i - 1] = section + "/" + i.ToString(); } newFrameCount = EditorGUILayout.Popup("Num Frames", clipNumFrames - 1, numFrameStr) + 1; if (newFrameCount == 0) { newFrameCount = 1; // minimum = 1 } } if (newFrameCount != clipNumFrames) { // Ungroup if (newFrameCount > clipNumFrames) { tk2dPreferences.inst.groupAnimDisplay = false; scrollPosition.y += 1000000.0f; // push to the end } tk2dSpriteAnimationFrame[] frames = new tk2dSpriteAnimationFrame[newFrameCount]; int c1 = Mathf.Min(clipNumFrames, frames.Length); for (int i = 0; i < c1; ++i) { frames[i] = new tk2dSpriteAnimationFrame(); frames[i].CopyFrom(clip.frames[i]); } if (c1 > 0) { for (int i = c1; i < frames.Length; ++i) { frames[i] = new tk2dSpriteAnimationFrame(); frames[i].CopyFrom(clip.frames[c1 - 1]); } } else { for (int i = 0; i < frames.Length; ++i) { frames[i] = new tk2dSpriteAnimationFrame(); var spriteCollection = tk2dSpriteGuiUtility.GetDefaultSpriteCollection(); frames[i].spriteCollection = spriteCollection; frames[i].spriteId = spriteCollection.FirstValidDefinitionIndex; } } clip.frames = frames; clipNumFrames = newFrameCount; } #endregion // Frame rate if (clip.wrapMode != tk2dSpriteAnimationClip.WrapMode.Single) { clip.fps = EditorGUILayout.FloatField("Frame rate", clip.fps); } // Wrap mode clip.wrapMode = (tk2dSpriteAnimationClip.WrapMode)EditorGUILayout.EnumPopup("Wrap mode", clip.wrapMode); if (clip.wrapMode == tk2dSpriteAnimationClip.WrapMode.LoopSection) { clip.loopStart = EditorGUILayout.IntField("Loop start", clip.loopStart); clip.loopStart = Mathf.Clamp(clip.loopStart, 0, clip.frames.Length - 1); } #region DrawFrames EditorGUI.indentLevel = 0; GUILayout.Space(8); EditorGUILayout.BeginHorizontal(EditorStyles.toolbar); GUILayout.Label("Frames"); GUILayout.FlexibleSpace(); // Reverse if (clip.wrapMode != tk2dSpriteAnimationClip.WrapMode.Single && GUILayout.Button("Reverse", EditorStyles.toolbarButton)) { System.Array.Reverse(clip.frames); GUI.changed = true; } // Auto fill if (clip.wrapMode != tk2dSpriteAnimationClip.WrapMode.Single && clip.frames.Length >= 1) { AutoFill(clip); } if (GUILayout.Button(tk2dPreferences.inst.horizontalAnimDisplay?"H":"V", EditorStyles.toolbarButton, GUILayout.MaxWidth(24))) { tk2dPreferences.inst.horizontalAnimDisplay = !tk2dPreferences.inst.horizontalAnimDisplay; Repaint(); } tk2dPreferences.inst.groupAnimDisplay = GUILayout.Toggle(tk2dPreferences.inst.groupAnimDisplay, "Group", EditorStyles.toolbarButton); EditorGUILayout.EndHorizontal(); // Sanitize frame data for (int i = 0; i < clip.frames.Length; ++i) { if (clip.frames[i].spriteCollection == null || clip.frames[i].spriteCollection == null || clip.frames[i].spriteCollection.inst.spriteDefinitions.Length == 0) { EditorUtility.DisplayDialog("Warning", "Invalid sprite collection found.\nThis clip will now be deleted", "Ok"); clip.name = ""; clip.frames = new tk2dSpriteAnimationFrame[0]; Repaint(); return; } if (clip.frames[i].spriteId < 0 || clip.frames[i].spriteId >= clip.frames[i].spriteCollection.inst.Count) { EditorUtility.DisplayDialog("Warning", "Invalid frame found, resetting to frame 0", "Ok"); clip.frames[i].spriteId = 0; } } // Warning when one of the frames has different poly count if (clipNumFrames > 0) { bool differentPolyCount = false; int polyCount = clip.frames[0].spriteCollection.inst.spriteDefinitions[clip.frames[0].spriteId].positions.Length; for (int i = 1; i < clipNumFrames; ++i) { int thisPolyCount = clip.frames[i].spriteCollection.inst.spriteDefinitions[clip.frames[i].spriteId].positions.Length; if (thisPolyCount != polyCount) { differentPolyCount = true; break; } } if (differentPolyCount) { Color bg = GUI.backgroundColor; GUI.backgroundColor = Color.red; GUILayout.TextArea("Sprites have different poly counts. Performance will be affected"); GUI.backgroundColor = bg; } } // Draw frames EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); DrawClipEditor(clip); EditorGUILayout.EndHorizontal(); #endregion } EditorGUILayout.EndVertical(); EditorGUI.indentLevel--; if (GUI.changed) { EditorUtility.SetDirty(anim); } GUILayout.Space(64); }
void Awake() { if(anim == null) anim = GetComponent<tk2dSpriteAnimator>(); mIsLeft = defaultLeft; if(anim) { anim.AnimationCompleted += OnAnimationComplete; anim.AnimationEventTriggered += OnAnimationFrameEvent; anim.Sprite.FlipX = mIsLeft ? leftFlip : !leftFlip; mDefaultAnimLib = anim.Library; mDefaultClipDat = new ClipData(this, mDefaultAnimLib); mLibClips = new ClipData[animLibs.Length]; for(int i = 0, max = animLibs.Length; i < max; i++) { mLibClips[i] = new ClipData(this, animLibs[i]); } } if(multiSprites) { mMultiSprites = GetComponentsInChildren<tk2dBaseSprite>(true); for(int i = 0; i < mMultiSprites.Length; i++) mMultiSprites[i].FlipX = mIsLeft ? leftFlip : !leftFlip; } if(controller == null) controller = GetComponent<PlatformerController>(); }
public static BasicBeamController GenerateBeamPrefab(this Projectile projectile, string spritePath, Vector2 colliderDimensions, Vector2 colliderOffsets, List <string> beamAnimationPaths = null, int beamFPS = -1, List <string> impactVFXAnimationPaths = null, int beamImpactFPS = -1, Vector2?impactVFXColliderDimensions = null, Vector2?impactVFXColliderOffsets = null, List <string> endVFXAnimationPaths = null, int beamEndFPS = -1, Vector2?endVFXColliderDimensions = null, Vector2?endVFXColliderOffsets = null, List <string> muzzleVFXAnimationPaths = null, int beamMuzzleFPS = -1, Vector2?muzzleVFXColliderDimensions = null, Vector2?muzzleVFXColliderOffsets = null) { try { float convertedColliderX = colliderDimensions.x / 16f; float convertedColliderY = colliderDimensions.y / 16f; float convertedOffsetX = colliderOffsets.x / 16f; float convertedOffsetY = colliderOffsets.y / 16f; int spriteID = SpriteBuilder.AddSpriteToCollection(spritePath, ETGMod.Databases.Items.ProjectileCollection); tk2dTiledSprite tiledSprite = projectile.gameObject.GetOrAddComponent <tk2dTiledSprite>(); tiledSprite.SetSprite(ETGMod.Databases.Items.ProjectileCollection, spriteID); tk2dSpriteDefinition def = tiledSprite.GetCurrentSpriteDef(); def.colliderVertices = new Vector3[] { new Vector3(convertedOffsetX, convertedOffsetY, 0f), new Vector3(convertedColliderX, convertedColliderY, 0f) }; def.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.MiddleLeft); //tiledSprite.anchor = tk2dBaseSprite.Anchor.MiddleCenter; tk2dSpriteAnimator animator = projectile.gameObject.GetOrAddComponent <tk2dSpriteAnimator>(); tk2dSpriteAnimation animation = projectile.gameObject.GetOrAddComponent <tk2dSpriteAnimation>(); animation.clips = new tk2dSpriteAnimationClip[0]; animator.Library = animation; UnityEngine.Object.Destroy(projectile.GetComponentInChildren <tk2dSprite>()); BasicBeamController beamController = projectile.gameObject.GetOrAddComponent <BasicBeamController>(); //---------------- Sets up the animation for the main part of the beam if (beamAnimationPaths != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "beam_idle", frames = new tk2dSpriteAnimationFrame[0], fps = beamFPS }; List <string> spritePaths = beamAnimationPaths; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in spritePaths) { tk2dSpriteCollectionData collection = ETGMod.Databases.Items.ProjectileCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.MiddleLeft); frameDef.colliderVertices = def.colliderVertices; frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); beamController.beamAnimation = "beam_idle"; } //------------- Sets up the animation for the part of the beam that touches the wall if (endVFXAnimationPaths != null && endVFXColliderDimensions != null && endVFXColliderOffsets != null) { SetupBeamPart(animation, endVFXAnimationPaths, "beam_end", beamEndFPS, (Vector2)endVFXColliderDimensions, (Vector2)endVFXColliderOffsets); beamController.beamEndAnimation = "beam_end"; } else { SetupBeamPart(animation, beamAnimationPaths, "beam_end", beamFPS, null, null, def.colliderVertices); beamController.beamEndAnimation = "beam_end"; } //---------------Sets up the animaton for the VFX that plays over top of the end of the beam where it hits stuff if (impactVFXAnimationPaths != null && impactVFXColliderDimensions != null && impactVFXColliderOffsets != null) { SetupBeamPart(animation, impactVFXAnimationPaths, "beam_impact", beamImpactFPS, (Vector2)impactVFXColliderDimensions, (Vector2)impactVFXColliderOffsets); beamController.impactAnimation = "beam_impact"; } //--------------Sets up the animation for the very start of the beam if (muzzleVFXAnimationPaths != null && muzzleVFXColliderDimensions != null && muzzleVFXColliderOffsets != null) { SetupBeamPart(animation, muzzleVFXAnimationPaths, "beam_start", beamMuzzleFPS, (Vector2)muzzleVFXColliderDimensions, (Vector2)muzzleVFXColliderOffsets); beamController.beamStartAnimation = "beam_start"; } else { SetupBeamPart(animation, beamAnimationPaths, "beam_start", beamFPS, null, null, def.colliderVertices); beamController.beamStartAnimation = "beam_start"; } return(beamController); } catch (Exception e) { ETGModConsole.Log(e.ToString()); return(null); } }
static void DoCreateSpriteObject() { tk2dSpriteCollectionData sprColl = null; if (sprColl == null) { // try to inherit from other Sprites in scene tk2dSprite spr = GameObject.FindObjectOfType(typeof(tk2dSprite)) as tk2dSprite; if (spr) { sprColl = spr.collection; } } if (sprColl == null) { tk2dSpriteCollectionIndex[] spriteCollections = tk2dEditorUtility.GetOrCreateIndex().GetSpriteCollectionIndex(); foreach (var v in spriteCollections) { GameObject scgo = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(v.spriteCollectionDataGUID), typeof(GameObject)) as GameObject; var sc = scgo.GetComponent <tk2dSpriteCollectionData>(); if (sc != null && sc.spriteDefinitions != null && sc.spriteDefinitions.Length > 0) { sprColl = sc; break; } } if (sprColl == null) { EditorUtility.DisplayDialog("Create Sprite", "Unable to create sprite as no SpriteCollections have been found.", "Ok"); return; } } tk2dSpriteAnimation[] anims = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations(); tk2dSpriteAnimation anim = null; foreach (var a in anims) { if (a != null && a.clips != null && a.clips.Length > 0) { anim = a; break; } } if (anim == null) { EditorUtility.DisplayDialog("Create Animated Sprite", "Unable to create animated sprite as no SpriteAnimations have been found.", "Ok"); return; } if (anim.clips[0].frames.Length == 0 || anim.clips[0].frames[0].spriteCollection == null) { EditorUtility.DisplayDialog("Create Animated Sprite", "Invalid SpriteAnimation has been found.", "Ok"); return; } GameObject go = tk2dEditorUtility.CreateGameObjectInScene("AnimatedSprite"); tk2dAnimatedSprite sprite = go.AddComponent <tk2dAnimatedSprite>(); sprite.collection = anim.clips[0].frames[0].spriteCollection; sprite.Build(); sprite.spriteId = anim.clips[0].frames[0].spriteId; sprite.anim = anim; }
static void DoCreateSpriteObject() { tk2dSpriteCollectionData sprColl = null; if (sprColl == null) { // try to inherit from other Sprites in scene tk2dSprite spr = GameObject.FindObjectOfType(typeof(tk2dSprite)) as tk2dSprite; if (spr) { sprColl = spr.Collection; } } if (sprColl == null) { tk2dSpriteCollectionIndex[] spriteCollections = tk2dEditorUtility.GetOrCreateIndex().GetSpriteCollectionIndex(); foreach (var v in spriteCollections) { GameObject scgo = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(v.spriteCollectionDataGUID), typeof(GameObject)) as GameObject; var sc = scgo.GetComponent <tk2dSpriteCollectionData>(); if (sc != null && sc.spriteDefinitions != null && sc.spriteDefinitions.Length > 0) { sprColl = sc; break; } } if (sprColl == null) { EditorUtility.DisplayDialog("Create Sprite", "Unable to create sprite as no SpriteCollections have been found.", "Ok"); return; } } tk2dGenericIndexItem[] animIndex = tk2dEditorUtility.GetOrCreateIndex().GetSpriteAnimations(); tk2dSpriteAnimation anim = null; int clipId = -1; foreach (var animIndexItem in animIndex) { tk2dSpriteAnimation a = animIndexItem.GetAsset <tk2dSpriteAnimation>(); if (a != null && a.clips != null && a.clips.Length > 0) { for (int i = 0; i < a.clips.Length; ++i) { if (!a.clips[i].Empty && a.clips[i].frames[0].spriteCollection != null && a.clips[i].frames[0].spriteId >= 0) { clipId = i; break; } } if (clipId != -1) { anim = a; break; } } } if (anim == null || clipId == -1) { EditorUtility.DisplayDialog("Create Animated Sprite", "Unable to create animated sprite as no SpriteAnimations have been found.", "Ok"); return; } GameObject go = tk2dEditorUtility.CreateGameObjectInScene("AnimatedSprite"); tk2dAnimatedSprite sprite = go.AddComponent <tk2dAnimatedSprite>(); sprite.SwitchCollectionAndSprite(anim.clips[clipId].frames[0].spriteCollection, anim.clips[clipId].frames[0].spriteId); sprite.anim = anim; sprite.clipId = clipId; sprite.Build(); Selection.activeGameObject = go; Undo.RegisterCreatedObjectUndo(go, "Create AnimatedSprite"); }
public SpriteAnimData(string name, Texture2D texture, float width, float height, ScaleType scaleType, int frames, bool loop, float duration) { this.name = name; this.frames = frames; float textureWidth = texture.width / frames; float textureHeight = texture.height; switch (scaleType) { case ScaleType.NONE: this.width = textureWidth; this.height = textureHeight; break; case ScaleType.SCALED_WIDTH: this.width = width; this.height = width * textureHeight / textureWidth; break; case ScaleType.SCALED_HEIGHT: this.width = height * textureWidth / textureHeight; this.height = height; break; case ScaleType.SCALED: default: this.width = width; this.height = height; break; } this.regionWidth = textureWidth; this.regionHeight = texture.wrapMode == TextureWrapMode.Repeat ? this.height * textureWidth / this.width : textureHeight ; string[] names = new string[frames]; Rect[] regions = new Rect[frames]; Vector2[] anchors = new Vector2[frames]; for (int i = 0; i < frames; i++) { names[i] = String.Format("{0}_frame{1}", name, i); regions[i] = new Rect(this.regionWidth * i, 0f, this.regionWidth, this.regionHeight); anchors[i] = new Vector2(this.regionWidth / 2, this.regionHeight / 2); } tk2dRuntime.SpriteCollectionSize size = tk2dRuntime.SpriteCollectionSize.ForTk2dCamera(); this.data = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture(texture, size, names, regions, anchors); this.data.gameObject.name = String.Format("DataSpriteAnim{0}", name); tk2dSpriteAnimationFrame[] animationFrames = new tk2dSpriteAnimationFrame[frames]; for (int i = 0; i < frames; i++) { tk2dSpriteAnimationFrame frame = new tk2dSpriteAnimationFrame(); frame.spriteCollection = this.data; frame.spriteId = i; animationFrames[i] = frame; } tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip(); clip.fps = frames / duration; clip.name = name; clip.wrapMode = loop ? tk2dSpriteAnimationClip.WrapMode.Loop : tk2dSpriteAnimationClip.WrapMode.Once; clip.frames = animationFrames; this.anim = this.data.gameObject.AddComponent<tk2dSpriteAnimation>(); this.anim.clips = new tk2dSpriteAnimationClip[] { clip }; }
private void _PatchClipsInAnimation(tk2dSpriteAnimationClip[] patch_clips, tk2dSpriteAnimation target, tk2dSpriteCollectionData collection, int sprite_id_offset, Action <tk2dSpriteAnimationClip> each_clip = null) { _Logger.Debug($"Patching clips - clips length before: {target.clips.Length}"); // init a hash of the patch clip names // we'll use this later for lookup when we create the new clip list // if a patch defines a clip 'dodge_bw', then the patch's clip should be used // instead of the target's, and this is how we decide that // HashSet instead of List because HashSet lookup is O(1) while List is O(n) var clip_name_set = new HashSet <string>(); for (int i = 0; i < patch_clips.Length; i++) { var patch_clip = patch_clips[i]; clip_name_set.Add(patch_clip.name); } // not using target.clips in the constructor here // because we want to validate each target clip against clip_name_set // before we add them here var clip_list = new List <tk2dSpriteAnimationClip>(); for (int i = 0; i < target.clips.Length; i++) { var target_clip = target.clips[i]; if (!clip_name_set.Contains(target_clip.name)) { clip_list.Add(target_clip); } else { _Logger.Debug($"Not adding original clip '{target_clip.name}', because a patch clip exists"); } } for (int i = 0; i < patch_clips.Length; i++) { var patch_clip = patch_clips[i]; var new_clip = new tk2dSpriteAnimationClip(); CopyClip(patch_clip, new_clip); var new_frames = new_clip.frames.Clone() as tk2dSpriteAnimationFrame[]; for (int j = 0; j < new_frames.Length; j++) { var new_frame = new_frames[j]; // we use the sprite ID offset from _PatchCollection here! new_frame.spriteId += sprite_id_offset; // and update the sprite collection new_frame.spriteCollection = collection; } new_clip.frames = new_frames; if (each_clip != null) { each_clip.Invoke(new_clip); } _Logger.Debug($"Adding patch clip '{new_clip.name}'"); clip_list.Add(new_clip); } target.clips = clip_list.ToArray(); _Logger.Debug($"Patching clips - clips length after: {target.clips.Length}"); }
/// <summary> /// Adds a tiled trail to the Projectile /// </summary> /// <param name="timeTillAnimStart">How long after spawning until the trail will begin to play it's animation, if it has one.</param> public static void AddTrailToProjectile(this Projectile target, string spritePath, Vector2 colliderDimensions, Vector2 colliderOffsets, List <string> animPaths = null, int animFPS = -1, List <string> startAnimPaths = null, int startAnimFPS = -1, float timeTillAnimStart = -1, float cascadeTimer = -1, float softMaxLength = -1, bool destroyOnEmpty = false) { try { GameObject newTrailObject = new GameObject(); newTrailObject.InstantiateAndFakeprefab(); newTrailObject.transform.parent = target.transform; float convertedColliderX = colliderDimensions.x / 16f; float convertedColliderY = colliderDimensions.y / 16f; float convertedOffsetX = colliderOffsets.x / 16f; float convertedOffsetY = colliderOffsets.y / 16f; int spriteID = SpriteBuilder.AddSpriteToCollection(spritePath, ETGMod.Databases.Items.ProjectileCollection); tk2dTiledSprite tiledSprite = newTrailObject.GetOrAddComponent <tk2dTiledSprite>(); tiledSprite.SetSprite(ETGMod.Databases.Items.ProjectileCollection, spriteID); tk2dSpriteDefinition def = tiledSprite.GetCurrentSpriteDef(); def.colliderVertices = new Vector3[] { new Vector3(convertedOffsetX, convertedOffsetY, 0f), new Vector3(convertedColliderX, convertedColliderY, 0f) }; def.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.MiddleLeft); tk2dSpriteAnimator animator = newTrailObject.GetOrAddComponent <tk2dSpriteAnimator>(); tk2dSpriteAnimation animation = newTrailObject.GetOrAddComponent <tk2dSpriteAnimation>(); animation.clips = new tk2dSpriteAnimationClip[0]; animator.Library = animation; TrailController trail = newTrailObject.AddComponent <TrailController>(); //---------------- Sets up the animation for the main part of the trail if (animPaths != null) { BeamToolbox.SetupBeamPart(animation, animPaths, "trail_mid", animFPS, null, null, def.colliderVertices); trail.animation = "trail_mid"; trail.usesAnimation = true; } else { trail.usesAnimation = false; } if (startAnimPaths != null) { BeamToolbox.SetupBeamPart(animation, startAnimPaths, "trail_start", startAnimFPS, null, null, def.colliderVertices); trail.startAnimation = "trail_start"; trail.usesStartAnimation = true; } else { trail.usesStartAnimation = false; } //Trail Variables if (softMaxLength > 0) { trail.usesSoftMaxLength = true; trail.softMaxLength = softMaxLength; } if (cascadeTimer > 0) { trail.usesCascadeTimer = true; trail.cascadeTimer = cascadeTimer; } if (timeTillAnimStart > 0) { trail.usesGlobalTimer = true; trail.globalTimer = timeTillAnimStart; } trail.destroyOnEmpty = destroyOnEmpty; } catch (Exception e) { ETGModConsole.Log(e.ToString()); } }
private static void BuildLibrary(LichsBookItem targetBook) { List <string> spriteNames = new List <string>(); string[] resources = ResourceExtractor.GetResourceNames(); string spriteDirectory = "LichItems/Resources/InfinilichTransformation"; for (int i = 0; i < resources.Length; i++) { if (resources[i].StartsWith(spriteDirectory.Replace('/', '.'), StringComparison.OrdinalIgnoreCase)) { spriteNames.Add(resources[i]); } } List <Texture2D> sprites = new List <Texture2D>(); foreach (string name in spriteNames) { sprites.Add(ResourceExtractor.GetTextureFromResource(name)); } tk2dSpriteAnimation library = Instantiate((PickupObjectDatabase.GetById(163) as BulletArmorItem).knightLibrary); DontDestroyOnLoad(library); var orig = library.clips[0].frames[0].spriteCollection; var copyCollection = Instantiate(orig); tk2dSpriteDefinition copydef = CopyDefinitionFrom((PickupObjectDatabase.GetById(607) as BankMaskItem).OverrideHandSprite.GetCurrentSpriteDef()); copydef.name = "knight_hand_001"; int handId = SpriteBuilder.AddSpriteToCollection(copydef, copyCollection); DontDestroyOnLoad(copyCollection); RuntimeAtlasPage page = new RuntimeAtlasPage(); for (int i = 0; i < sprites.Count; i++) { var tex = sprites[i]; var def = copyCollection.GetSpriteDefinition(tex.name); if (def != null) { def.ReplaceTexture(tex); def.name = def.name.Replace("knight", "inflich"); MakeOffset(def, new Vector2(-0.0625f, 0f), false); } } page.Apply(); foreach (var clip in library.clips) { for (int i = 0; i < clip.frames.Length; i++) { clip.frames[i].spriteCollection = copyCollection; } } foreach (tk2dSpriteAnimationClip clip in library.clips) { foreach (tk2dSpriteAnimationFrame frame in clip.frames) { if (!string.IsNullOrEmpty(frame.eventAudio) && (frame.eventAudio == "Play_FS" || frame.eventAudio == "Play_CHR_boot_stairs_01")) { frame.eventAudio = ""; } } } GameObject spriteObj = new GameObject("OverrideHandSprite"); spriteObj.SetActive(false); DontDestroyOnLoad(spriteObj); tk2dSprite sprite = spriteObj.AddComponent <tk2dSprite>(); sprite.SetSprite(copyCollection, handId); targetBook.replacementHandSprite = sprite; targetBook.replacementLibrary = library; }
/// <summary> /// Adds a tk2dAnimatedSprite as a component to the gameObject passed in, setting up necessary parameters and building geometry. /// </summary> public static tk2dAnimatedSprite AddComponent(GameObject go, tk2dSpriteAnimation anim, int clipId) { var clip = anim.clips[clipId]; tk2dAnimatedSprite animSprite = go.AddComponent<tk2dAnimatedSprite>(); animSprite.Collection = clip.frames[0].spriteCollection; animSprite.spriteId = clip.frames[0].spriteId; animSprite.anim = anim; return animSprite; }
public void RefreshClips() { if(anim) { mDefaultAnimLib = anim.Library; mDefaultClipDat = new ClipData(this, mDefaultAnimLib); } }
public static VFXPool CreateVFXPool(string name, List <string> spritePaths, int fps, IntVector2 Dimensions, tk2dBaseSprite.Anchor anchor, bool usesZHeight, float zHeightOffset, bool persist = false, VFXAlignment alignment = VFXAlignment.NormalAligned, float emissivePower = -1, Color?emissiveColour = null) { GameObject Obj = new GameObject(name); VFXPool pool = new VFXPool(); pool.type = VFXPoolType.All; VFXComplex complex = new VFXComplex(); VFXObject vfObj = new VFXObject(); Obj.SetActive(false); FakePrefab.MarkAsFakePrefab(Obj); UnityEngine.Object.DontDestroyOnLoad(Obj); tk2dSpriteCollectionData VFXSpriteCollection = SpriteBuilder.ConstructCollection(Obj, (name + "_Collection")); int spriteID = SpriteBuilder.AddSpriteToCollection(spritePaths[0], VFXSpriteCollection); tk2dSprite sprite = Obj.GetOrAddComponent <tk2dSprite>(); sprite.SetSprite(VFXSpriteCollection, spriteID); tk2dSpriteDefinition defaultDef = sprite.GetCurrentSpriteDef(); defaultDef.colliderVertices = new Vector3[] { new Vector3(0f, 0f, 0f), new Vector3((Dimensions.x / 16), (Dimensions.y / 16), 0f) }; tk2dSpriteAnimator animator = Obj.GetOrAddComponent <tk2dSpriteAnimator>(); tk2dSpriteAnimation animation = Obj.GetOrAddComponent <tk2dSpriteAnimation>(); animation.clips = new tk2dSpriteAnimationClip[0]; animator.Library = animation; tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "start", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); for (int i = 0; i < spritePaths.Count; i++) { tk2dSpriteCollectionData collection = VFXSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(spritePaths[i], collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.ConstructOffsetsFromAnchor(anchor); frameDef.colliderVertices = defaultDef.colliderVertices; if (emissivePower > 0) { frameDef.material.shader = ShaderCache.Acquire("Brave/LitTk2dCustomFalloffTintableTiltedCutoutEmissive"); } if (emissivePower > 0) { frameDef.material.SetFloat("_EmissiveColorPower", emissivePower); } if (emissiveColour != null) { frameDef.material.SetColor("_EmissiveColor", (Color)emissiveColour); } if (emissivePower > 0) { frameDef.materialInst.shader = ShaderCache.Acquire("Brave/LitTk2dCustomFalloffTintableTiltedCutoutEmissive"); } if (emissivePower > 0) { frameDef.materialInst.SetFloat("_EmissiveColorPower", emissivePower); } if (emissiveColour != null) { frameDef.materialInst.SetColor("_EmissiveColor", (Color)emissiveColour); } frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } if (emissivePower > 0) { sprite.renderer.material.shader = ShaderCache.Acquire("Brave/LitTk2dCustomFalloffTintableTiltedCutoutEmissive"); } if (emissivePower > 0) { sprite.renderer.material.SetFloat("_EmissiveColorPower", emissivePower); } if (emissiveColour != null) { sprite.renderer.material.SetColor("_EmissiveColor", (Color)emissiveColour); } clip.frames = frames.ToArray(); clip.wrapMode = tk2dSpriteAnimationClip.WrapMode.Once; animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); if (!persist) { SpriteAnimatorKiller kill = animator.gameObject.AddComponent <SpriteAnimatorKiller>(); kill.fadeTime = -1f; kill.animator = animator; kill.delayDestructionTime = -1f; } animator.playAutomatically = true; animator.DefaultClipId = animator.GetClipIdByName("start"); vfObj.attached = true; vfObj.persistsOnDeath = persist; vfObj.usesZHeight = usesZHeight; vfObj.zHeight = zHeightOffset; vfObj.alignment = alignment; vfObj.destructible = false; vfObj.effect = Obj; complex.effects = new VFXObject[] { vfObj }; pool.effects = new VFXComplex[] { complex }; return(pool); }
static void CreateIndex() { tk2dIndex newIndex = ScriptableObject.CreateInstance <tk2dIndex>(); List <string> rebuildSpriteCollectionPaths = new List <string>(); // check all prefabs to see if we can find any objects we are interested in List <string> allPrefabPaths = new List <string>(); Stack <string> paths = new Stack <string>(); paths.Push(Application.dataPath); while (paths.Count != 0) { string path = paths.Pop(); string[] files = Directory.GetFiles(path, "*.prefab"); foreach (var file in files) { allPrefabPaths.Add(file.Substring(Application.dataPath.Length - 6)); } foreach (string subdirs in Directory.GetDirectories(path)) { paths.Push(subdirs); } } // Check all prefabs int currPrefabCount = 1; foreach (string prefabPath in allPrefabPaths) { EditorUtility.DisplayProgressBar("Rebuilding Index", "Scanning project folder...", (float)currPrefabCount / (float)(allPrefabPaths.Count)); GameObject iterGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject; if (!iterGo) { continue; } tk2dSpriteCollection spriteCollection = iterGo.GetComponent <tk2dSpriteCollection>(); tk2dFont font = iterGo.GetComponent <tk2dFont>(); tk2dSpriteAnimation anims = iterGo.GetComponent <tk2dSpriteAnimation>(); if (spriteCollection) { tk2dSpriteCollectionData spriteCollectionData = spriteCollection.spriteCollection; if (spriteCollectionData) { if (spriteCollectionData.version < 1) { rebuildSpriteCollectionPaths.Add(AssetDatabase.GetAssetPath(spriteCollection)); } newIndex.AddSpriteCollectionData(spriteCollectionData); } } else if (font) { newIndex.AddFont(font); } else if (anims) { newIndex.AddSpriteAnimation(anims); } else { iterGo = null; System.GC.Collect(); } tk2dEditorUtility.UnloadUnusedAssets(); ++currPrefabCount; } EditorUtility.ClearProgressBar(); // Create index AssetDatabase.CreateAsset(newIndex, indexPath); // unload all unused assets tk2dEditorUtility.UnloadUnusedAssets(); // Rebuild invalid sprite collections if (rebuildSpriteCollectionPaths.Count > 0) { EditorUtility.DisplayDialog("Upgrade required", "Please wait while your sprite collection is upgraded.", "Ok"); int count = 1; foreach (var scPath in rebuildSpriteCollectionPaths) { tk2dSpriteCollection sc = AssetDatabase.LoadAssetAtPath(scPath, typeof(tk2dSpriteCollection)) as tk2dSpriteCollection; EditorUtility.DisplayProgressBar("Rebuilding Sprite Collections", "Rebuilding Sprite Collection: " + sc.name, (float)count / (float)(rebuildSpriteCollectionPaths.Count)); tk2dSpriteCollectionBuilder.Rebuild(sc); sc = null; tk2dEditorUtility.UnloadUnusedAssets(); ++count; } EditorUtility.ClearProgressBar(); } }
public static void SetupHatSprites(List <string> spritePaths, GameObject hatObj, int fps, Vector2 hatSize) { Hat hatness = hatObj.GetComponent <Hat>(); if (hatness) { string collectionName = hatness.hatName; collectionName = collectionName.Replace(" ", "_"); tk2dSpriteCollectionData HatSpriteCollection = SpriteBuilder.ConstructCollection(hatObj, (collectionName + "_Collection")); int spriteID = SpriteBuilder.AddSpriteToCollection(spritePaths[0], HatSpriteCollection); tk2dSprite hatBaseSprite = hatObj.GetOrAddComponent <tk2dSprite>(); hatBaseSprite.SetSprite(HatSpriteCollection, spriteID); tk2dSpriteDefinition def = hatBaseSprite.GetCurrentSpriteDef(); def.colliderVertices = new Vector3[] { new Vector3(0f, 0f, 0f), new Vector3((hatSize.x / 16), (hatSize.y / 16), 0f) }; hatBaseSprite.PlaceAtPositionByAnchor(hatObj.transform.position, tk2dBaseSprite.Anchor.LowerCenter); hatBaseSprite.depthUsesTrimmedBounds = true; hatBaseSprite.IsPerpendicular = true; hatBaseSprite.UpdateZDepth(); hatBaseSprite.HeightOffGround = 0.2f; List <string> SouthAnimation = new List <string>(); List <string> NorthAnimation = new List <string>(); List <string> EastAnimation = new List <string>(); List <string> WestAnimation = new List <string>(); List <string> NorthWestAnimation = new List <string>(); List <string> NorthEastAnimation = new List <string>(); List <string> SouthEastAnimation = new List <string>(); List <string> SouthWestAnimation = new List <string>(); switch (hatness.hatDirectionality) { case Hat.HatDirectionality.EIGHTWAY: foreach (string path in spritePaths) { if (path.ToLower().Contains("_north_")) { NorthAnimation.Add(path); } if (path.ToLower().Contains("_south_")) { SouthAnimation.Add(path); } if (path.ToLower().Contains("_west_")) { WestAnimation.Add(path); } if (path.ToLower().Contains("_east_")) { EastAnimation.Add(path); } if (path.ToLower().Contains("_northwest_")) { NorthWestAnimation.Add(path); } if (path.ToLower().Contains("_northeast_")) { NorthEastAnimation.Add(path); } if (path.ToLower().Contains("_southwest_")) { SouthWestAnimation.Add(path); } if (path.ToLower().Contains("_southeast_")) { SouthEastAnimation.Add(path); } } break; case Hat.HatDirectionality.SIXWAY: foreach (string path in spritePaths) { if (path.ToLower().Contains("_north_")) { NorthAnimation.Add(path); } if (path.ToLower().Contains("_south_")) { SouthAnimation.Add(path); } if (path.ToLower().Contains("_west_")) { WestAnimation.Add(path); } if (path.ToLower().Contains("_east_")) { EastAnimation.Add(path); } if (path.ToLower().Contains("_northwest_")) { NorthWestAnimation.Add(path); } if (path.ToLower().Contains("_northeast_")) { NorthEastAnimation.Add(path); } } break; case Hat.HatDirectionality.FOURWAY: foreach (string path in spritePaths) { if (path.ToLower().Contains("_west_")) { WestAnimation.Add(path); } if (path.ToLower().Contains("_east_")) { EastAnimation.Add(path); } if (path.ToLower().Contains("_north_")) { NorthAnimation.Add(path); } if (path.ToLower().Contains("_south_")) { SouthAnimation.Add(path); } } break; case Hat.HatDirectionality.TWOWAYHORIZONTAL: foreach (string path in spritePaths) { if (path.ToLower().Contains("_west_")) { WestAnimation.Add(path); } if (path.ToLower().Contains("_east_")) { EastAnimation.Add(path); } } break; case Hat.HatDirectionality.TWOWAYVERTICAL: foreach (string path in spritePaths) { if (path.ToLower().Contains("_north_")) { NorthAnimation.Add(path); } if (path.ToLower().Contains("_south_")) { SouthAnimation.Add(path); } } break; case Hat.HatDirectionality.NONE: foreach (string path in spritePaths) { if (path.ToLower().Contains("_south_")) { SouthAnimation.Add(path); } } break; } //SET UP THE ANIMATOR AND THE ANIMATION tk2dSpriteAnimator animator = hatObj.GetOrAddComponent <tk2dSpriteAnimator>(); tk2dSpriteAnimation animation = hatObj.GetOrAddComponent <tk2dSpriteAnimation>(); animation.clips = new tk2dSpriteAnimationClip[0]; animator.Library = animation; //SOUTH ANIMATION if (SouthAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_south", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in SouthAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } //NORTH ANIMATIONS if (NorthAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_north", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in NorthAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } //WEST ANIMATIONS if (WestAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_west", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in WestAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } //EAST ANIMATIONS if (EastAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_east", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in EastAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } //NORTHEAST if (NorthEastAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_northeast", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in NorthEastAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } //NORTHWEST ANIMATION if (NorthWestAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_northwest", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in NorthWestAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } //SOUTHWEST ANIMATION if (SouthWestAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_southwest", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in SouthWestAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } //SOUTHEAST ANIMATION if (SouthEastAnimation != null) { tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip() { name = "hat_southeast", frames = new tk2dSpriteAnimationFrame[0], fps = fps }; List <tk2dSpriteAnimationFrame> frames = new List <tk2dSpriteAnimationFrame>(); foreach (string path in SouthEastAnimation) { tk2dSpriteCollectionData collection = HatSpriteCollection; int frameSpriteId = SpriteBuilder.AddSpriteToCollection(path, collection); tk2dSpriteDefinition frameDef = collection.spriteDefinitions[frameSpriteId]; frameDef.colliderVertices = def.colliderVertices; frameDef.ConstructOffsetsFromAnchor(tk2dBaseSprite.Anchor.LowerCenter); frames.Add(new tk2dSpriteAnimationFrame { spriteId = frameSpriteId, spriteCollection = collection }); } clip.frames = frames.ToArray(); animation.clips = animation.clips.Concat(new tk2dSpriteAnimationClip[] { clip }).ToArray(); } } }