public void GenerateBarrelPositionCode() { GaeAnimationInfo animation = MainSpriteController.instance.currentFrame.animationInfo; FrameInfo info = MainSpriteController.instance.currentFrame; if (animation != null) { if (!info.path.Contains("idle_001")) { Debug.Log("this shouldnt ever happen, but ill let it slide"); } float barrelOffsetXString = info.muzzleflashPositionX / 16f; float barrelOffsetYString = info.muzzleflashPositionY / 16f; string offsetCode = $"gun.barrelOffset.localPosition = new Vector3({barrelOffsetXString}f, {barrelOffsetYString}f, 0f);"; if (!string.IsNullOrEmpty(animation.AnimationDirectory)) { File.WriteAllText(Path.Combine(animation.AnimationDirectory, animation.animationName + " barrel offset code" + ".txt"), offsetCode); Debug.Log("nice, it (should have) worked"); } else { Debug.LogError("Shit, path was empty!"); } } }
public void SetAnimation(GaeAnimationInfo animation) { currentAnimationInfo = animation; index = 0; UpdateSprite(true); if (animation == null) { mainSprite.sprite = null; } }
public FrameInfo(Texture2D texture, Sprite sprite, string path, GaeAnimationInfo animationInfo) { this.animationInfo = animationInfo; this.texture = texture; this.sprite = sprite; this.path = path; this.hand1PositionX = 0; this.hand1PositionY = 0; this.hand2PositionX = 0; this.hand2PositionY = 0; this.offsetX = 0; this.offsetY = 0; this.isTwoHanded = false; }
public FrameInfo(Texture2D texture, string path, GaeAnimationInfo animationInfo) { this.animationInfo = animationInfo; this.texture = texture; this.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f), 1); this.path = path; this.hand1PositionX = 0; this.hand1PositionY = 0; this.hand2PositionX = 0; this.hand2PositionY = 0; this.offsetX = 0; this.offsetY = 0; this.isTwoHanded = false; }
//similiar to importSingleImages, however it only has one path, and attempts to find other matching files based on that. loads all the frame info objects into a single tab public void ImportAnimation() { Image.sprite = DefaultTexture; paths = StandaloneFileBrowser.OpenFilePanel("gotta make them guns ey?", "", "png", false); if (paths.Length > 0) { FileInfo info = new FileInfo(paths[0]); string fileName = Path.GetFileNameWithoutExtension(paths[0]); string animationName = RemoveTrailingDigits(fileName); FileInfo[] files = info.Directory.GetFiles(animationName + "_*.png"); GaeAnimationInfo animationInfo = new GaeAnimationInfo(); animationInfo.AnimationDirectory = info.DirectoryName; animationInfo.frames = new FrameInfo[files.Length]; for (int i = 0; i < animationInfo.frames.Length; i++) { animationInfo.frames[i] = LoadSingleFrame(files[i].FullName, animationInfo); } animationInfo.animationName = RemoveTrailingDigits(Path.GetFileNameWithoutExtension(files[0].FullName)); RectTransform viewPort = StaticRefrences.Instance.TabArea.GetComponent <ScrollRect>().content; RectTransform tabArea = StaticRefrences.Instance.TabArea.GetComponent <RectTransform>(); var newButton = Instantiate(ImgTabPrefab, viewPort, false); newButton.name = animationInfo.animationName + " button"; RectTransform buttonRectTransform = newButton.GetComponent <RectTransform>(); buttonRectTransform.localPosition = new Vector3(0, 0, 0); float ratio = tabArea.rect.width / buttonRectTransform.rect.width; newButton.transform.localScale = new Vector3(ratio, ratio, 1f); TabDisplay newButtonTabDisplay = newButton.GetComponent <TabDisplay>(); newButtonTabDisplay.TMPtext.text = animationInfo.animationName; newButtonTabDisplay.animationInfo = animationInfo; StaticRefrences.Instance.spriteController.SetAnimation(animationInfo); } }
//this method attempts to load the image as well as a corrosponding json file, and create a matching FrameInfo object based off of those private FrameInfo LoadSingleFrame(string path, GaeAnimationInfo animationInfo) { path = System.IO.Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path)); string pngPath = path + ".png"; string jsonPath = path + ".json"; if (!File.Exists(pngPath)) { return(null); } byte[] bytes = File.ReadAllBytes(pngPath); Texture2D tex = new Texture2D(2, 2); tex.LoadImage(bytes); tex.filterMode = FilterMode.Point; Sprite sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f), 1); if (File.Exists(jsonPath)) { try { string jsonInfo = File.ReadAllText(jsonPath); FrameJsonInfo frameJsonInfo = JsonConvert.DeserializeObject <FrameJsonInfo>(jsonInfo); for (int i = 1; i < frameJsonInfo.attachPoints.Length; i++) { //this bit of code relies on conversion exceptions to make sure the data type is right. this only happens to class members are marked as [JsonRequiredAttribute] //this is probably really bad practice. especially since it relies on loaded objects to be of " Newtonsoft.Json.Linq.JObject" type but i dont know any better as of writing this try { frameJsonInfo.attachPoints[i] = (frameJsonInfo.attachPoints[i] as Newtonsoft.Json.Linq.JObject).ToObject <ArrayTypeUnkownAndSize>(); } catch (Exception) {} try { frameJsonInfo.attachPoints[i] = (frameJsonInfo.attachPoints[i] as Newtonsoft.Json.Linq.JObject).ToObject <AttachPoint>(); } catch (Exception) {} } float convertedX1 = 0; float convertedX2 = 0; float convertedY1 = 0; float convertedY2 = 0; bool isTwoHanded = false; for (int i = 0; i < frameJsonInfo.attachPoints.Length; i++) { if (frameJsonInfo.attachPoints[i] is AttachPoint) { if ((frameJsonInfo.attachPoints[i] as AttachPoint).name == "PrimaryHand") { AttachPoint hand1 = frameJsonInfo.attachPoints[i] as AttachPoint; convertedX1 = hand1.position.x * 16 - frameJsonInfo.x; convertedY1 = hand1.position.y * 16 - frameJsonInfo.y; } else if ((frameJsonInfo.attachPoints[i] as AttachPoint).name == "SecondaryHand") { AttachPoint hand2 = frameJsonInfo.attachPoints[i] as AttachPoint; convertedX2 = hand2.position.x * 16 - frameJsonInfo.x; convertedY2 = hand2.position.y * 16 - frameJsonInfo.y; isTwoHanded = true; } } } return(new FrameInfo(tex, sprite, convertedX1, convertedY1, convertedX2, convertedY2, frameJsonInfo.x, frameJsonInfo.y, isTwoHanded, path, animationInfo)); } catch (Exception) { throw new Exception("json seems to be invalid! or i dont know how to read jsons (prob the second one ;) )!"); } } return(new FrameInfo(tex, sprite, pngPath, animationInfo)); }
public void GenerateOffsetCode() { GaeAnimationInfo animation = MainSpriteController.instance.currentAnimation; if (animation != null) { StringBuilder builder = new StringBuilder("//make sure the animation name and variable names are correct, the program may have made the wrong decision \n"); builder.Append("// it is better to be getting your clips like so \"gun.sprite.spriteAnimator.GetClipByName(gun.shootAnimation);\" and vary the animation name of course\n"); builder.Append("tk2dSpriteAnimationClip animationclip = gun.sprite.spriteAnimator.GetClipByName(\"" + animation.animationName.Trim('_') + "\");\n"); builder.Append("float[] offsetsX = new float[] {"); builder.Append((animation.frames[0].offsetX / 16).ToString("f4", culture)); builder.Append("f"); for (int i = 1; i < animation.frames.Length; i++) { builder.Append(","); builder.Append((animation.frames[i].offsetX / 16).ToString("f4", culture)); builder.Append("f"); } builder.Append("};\n"); builder.Append("float[] offsetsY = new float[] {"); builder.Append((animation.frames[0].offsetY / 16).ToString("f4", culture)); builder.Append("f"); for (int i = 1; i < animation.frames.Length; i++) { builder.Append(","); builder.Append((animation.frames[i].offsetY / 16).ToString("f4", culture)); builder.Append("f"); } builder.Append("};\n"); builder.Append("//simple method \n"); builder.Append("for (int i = 0; i < offsetsX.Length && i < offsetsY.Length && i < animationclip.frames.Length; i++)"); builder.Append("{"); builder.Append("int id = animationclip.frames[i].spriteId;"); builder.Append("tk2dSpriteDefinition def = frames.spriteCollection.spriteDefinitions[id];"); builder.Append("Vector2 offset = new Vector2(offsetsX[i],offsetsY[i]);"); builder.Append("def.position0 += offset;"); builder.Append("def.position1 += offset;"); builder.Append("def.position2 += offset;"); builder.Append("def.position3 += offset;"); builder.Append("}\n\n"); builder.Append("//it is reccomended you keep a cleaner more maintainable code by doing this\n"); builder.Append("//first add this method to your toolbox\n\n"); builder.Append("static void AddOffsetToTk2DFrame(tk2dSpriteAnimationFrame frame, Vector2 offset)\n"); builder.Append("for (int i = 0; i < offsetsX.Length && i < offsetsY.Length && i < animationclip.frames.Length; i++)\n"); builder.Append("{\n"); builder.Append("\tint id = animationclip.frames[i].spriteId;\n"); builder.Append("\ttk2dSpriteDefinition def = frames.spriteCollection.spriteDefinitions[id];\n"); builder.Append("\tdef.position0 += offset;\n"); builder.Append("\tdef.position1 += offset;\n"); builder.Append("\tdef.position2 += offset;\n"); builder.Append("\tdef.position3 += offset;\n"); builder.Append("}\n\n"); builder.Append("//then you can this \n"); builder.Append("for (int i = 0; i < offsetsX.Length && i < offsetsY.Length && i < animationclip.frames.Length; i++)\n"); builder.Append("{\n"); builder.Append("\tyourToolBox.AddOffsetToTk2DFrame(animationclip.frames[i],new Vector2(offsetsX[i],offsetsY[i]));\n"); builder.Append("}"); if (!string.IsNullOrEmpty(animation.AnimationDirectory)) { File.WriteAllText(Path.Combine(animation.AnimationDirectory, animation.animationName + " offset code" + ".txt"), builder.ToString()); Debug.Log("nice, it (should have) worked"); } else { Debug.LogError("Shit, path was empty!"); } } }
public FrameInfo(Texture2D texture, Sprite sprite, float hand1X, float hand1Y, float hand2X, float hand2Y, float offsetX, float offsetY, bool isTwoHanded, string path, GaeAnimationInfo animationInfo) { this.animationInfo = animationInfo; this.texture = texture; this.sprite = sprite; this.hand1PositionX = hand1X; this.hand1PositionY = hand1Y; this.hand2PositionX = hand2X; this.hand2PositionY = hand2Y; this.offsetX = offsetX; this.offsetY = offsetY; this.isTwoHanded = isTwoHanded; this.path = path; }