format code

This commit is contained in:
EliyaFishman 2022-12-07 18:19:39 +02:00
parent b5bec07e27
commit 5818234018
12 changed files with 172 additions and 220 deletions

View file

@ -2,37 +2,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using Bread2Unity;
using UnityEditor; using UnityEditor;
using UnityEditor.Animations; using UnityEditor.Animations;
using UnityEditor.Graphs;
using UnityEngine; using UnityEngine;
using Animation = Bread2Unity.Animation;
namespace Bread2Unity namespace Bread2Unity
{ {
public static class AnimationCreator public static class AnimationCreator
{ {
private class BccadCurve : AnimationCurve
{
private float _prev;
public new void AddKey(float time, float value)
{
if (keys.Length != 0 && !(Math.Abs(value - _prev) > 0.000001)) return;
AddKey(new Keyframe(time, value, float.PositiveInfinity, float.PositiveInfinity));
_prev = value;
}
public void CopyLastKey(float time)
{
Keyframe lastKey = keys.LastOrDefault();
base.AddKey(new Keyframe(time, keys.Length > 0 ? lastKey.value : 1, float.PositiveInfinity,
float.PositiveInfinity));
}
}
public static void CreateAnimation(BccadPrefab bccadPrefab, BCCAD bccad, PrefabData prefabData, public static void CreateAnimation(BccadPrefab bccadPrefab, BCCAD bccad, PrefabData prefabData,
List<Sprite> sprites) List<Sprite> sprites)
{ {
@ -42,10 +19,7 @@ namespace Bread2Unity
$"Assets\\Resources\\Sprites\\Games\\{char.ToUpperInvariant(rootPrefabName[0]) + rootPrefabName.Substring(1)}"; $"Assets\\Resources\\Sprites\\Games\\{char.ToUpperInvariant(rootPrefabName[0]) + rootPrefabName.Substring(1)}";
var animationsFolderPath = spritesFolderPath + $"/Animations/{prefabData.Name}"; var animationsFolderPath = spritesFolderPath + $"/Animations/{prefabData.Name}";
if (!Directory.Exists(animationsFolderPath)) if (!Directory.Exists(animationsFolderPath)) Directory.CreateDirectory(animationsFolderPath);
{
Directory.CreateDirectory(animationsFolderPath);
}
var controller = var controller =
AnimatorController.CreateAnimatorControllerAtPath( AnimatorController.CreateAnimatorControllerAtPath(
@ -77,7 +51,7 @@ namespace Bread2Unity
{ {
var animationClip = new AnimationClip(); var animationClip = new AnimationClip();
var prefab = bccadPrefab.ParentObject; var prefab = bccadPrefab.ParentObject;
for (int childIndex = 0; childIndex < prefab.transform.childCount; childIndex++) for (var childIndex = 0; childIndex < prefab.transform.childCount; childIndex++)
{ {
var child = prefab.transform.GetChild(childIndex).gameObject; var child = prefab.transform.GetChild(childIndex).gameObject;
@ -87,7 +61,7 @@ namespace Bread2Unity
var stepScaleX = new BccadCurve(); var stepScaleX = new BccadCurve();
var stepScaleY = new BccadCurve(); var stepScaleY = new BccadCurve();
var stepRotation = new BccadCurve(); var stepRotation = new BccadCurve();
var enabledCurve = new BccadCurve(); var enabledCurve = new BccadCurve();
var xTransformCurve = new BccadCurve(); var xTransformCurve = new BccadCurve();
@ -116,7 +90,7 @@ namespace Bread2Unity
stepRotation.AddKey(currentTime, currentStep.Rotation); stepRotation.AddKey(currentTime, currentStep.Rotation);
stepScaleX.AddKey(currentTime, currentStep.StretchX); stepScaleX.AddKey(currentTime, currentStep.StretchX);
stepScaleY.AddKey(currentTime, currentStep.StretchY); stepScaleY.AddKey(currentTime, currentStep.StretchY);
var bccadSprite = currentStep.BccadSprite; var bccadSprite = currentStep.BccadSprite;
// Find the index of part of the game object // Find the index of part of the game object
var partIndex = bccadSprite.parts.Select((value, index) => new { value, index }) var partIndex = bccadSprite.parts.Select((value, index) => new { value, index })
@ -180,11 +154,8 @@ namespace Bread2Unity
if (animation.Steps.Select(step => step.Rotation).Distinct().Count() > 1) if (animation.Steps.Select(step => step.Rotation).Distinct().Count() > 1)
animationClip.SetCurve("", typeof(Transform), "localEulerAngles.z", stepRotation); animationClip.SetCurve("", typeof(Transform), "localEulerAngles.z", stepRotation);
if (childIndex == 0) if (childIndex == 0) enabledCurve.CopyLastKey(currentTime);
{
enabledCurve.CopyLastKey(currentTime);
}
var spriteBinding = new EditorCurveBinding var spriteBinding = new EditorCurveBinding
{ {
@ -207,15 +178,13 @@ namespace Bread2Unity
//Check if there is any need for z animation //Check if there is any need for z animation
var setOfZIndexes = new HashSet<float>(); var setOfZIndexes = new HashSet<float>();
foreach (var sprite in spritesAssociatedWithPrefab) foreach (var sprite in spritesAssociatedWithPrefab)
{ for (var i = 0; i < sprite.parts.Count && setOfZIndexes.Count < 2; i++)
for (int i = 0; i < sprite.parts.Count && setOfZIndexes.Count < 2; i++)
{ {
var part = sprite.parts[i]; var part = sprite.parts[i];
if (bccadPrefab.RegionToChild[part.RegionIndex] != child) if (bccadPrefab.RegionToChild[part.RegionIndex] != child)
continue; continue;
setOfZIndexes.Add(i); setOfZIndexes.Add(i);
} }
}
if ((from part in partsOfGameObject select part.PosX).Distinct().Count() > 1 || if ((from part in partsOfGameObject select part.PosX).Distinct().Count() > 1 ||
(from part in partsOfGameObject select part.PosY).Distinct().Count() > 1 || (from part in partsOfGameObject select part.PosY).Distinct().Count() > 1 ||
@ -229,9 +198,7 @@ namespace Bread2Unity
} }
if ((from part in partsOfGameObject select part.Rotation).Distinct().Count() > 1) if ((from part in partsOfGameObject select part.Rotation).Distinct().Count() > 1)
{
animationClip.SetCurve(child.name, typeof(Transform), "localEulerAngles.z", rotationCurve); animationClip.SetCurve(child.name, typeof(Transform), "localEulerAngles.z", rotationCurve);
}
if ((from part in partsOfGameObject select part.StretchX).Distinct().Count() > 1 || if ((from part in partsOfGameObject select part.StretchX).Distinct().Count() > 1 ||
(from part in partsOfGameObject select part.StretchY).Distinct().Count() > 1) (from part in partsOfGameObject select part.StretchY).Distinct().Count() > 1)
@ -257,5 +224,25 @@ namespace Bread2Unity
return animationClip; return animationClip;
} }
private class BccadCurve : AnimationCurve
{
private float _prev;
public new void AddKey(float time, float value)
{
if (keys.Length != 0 && !(Math.Abs(value - _prev) > 0.000001)) return;
AddKey(new Keyframe(time, value, float.PositiveInfinity, float.PositiveInfinity));
_prev = value;
}
public void CopyLastKey(float time)
{
var lastKey = keys.LastOrDefault();
base.AddKey(new Keyframe(time, keys.Length > 0 ? lastKey.value : 1, float.PositiveInfinity,
float.PositiveInfinity));
}
}
} }
} }

View file

@ -1,34 +1,34 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Bread2Unity;
using UnityEngine; using UnityEngine;
namespace Bread2Unity namespace Bread2Unity
{ {
public class BccadPrefab public class BccadPrefab
{ {
public GameObject ParentObject { get; } private readonly BCCAD _bccad;
public readonly Dictionary<RegionIndex, GameObject> RegionToChild = new Dictionary<RegionIndex, GameObject>();
private readonly List<GameObject> _children = new List<GameObject>(); private readonly List<GameObject> _children = new List<GameObject>();
private readonly PrefabData _data; private readonly PrefabData _data;
private readonly BCCAD _bccad; public readonly Dictionary<RegionIndex, GameObject> RegionToChild = new Dictionary<RegionIndex, GameObject>();
public float HeightRatio { get; }
public float WidthRatio { get; }
public BccadPrefab(PrefabData data, BCCAD bccad, Texture2D texture) public BccadPrefab(PrefabData data, BCCAD bccad, Texture2D texture)
{ {
ParentObject = new GameObject(data.Name); ParentObject = new GameObject(data.Name);
_data = data; _data = data;
HeightRatio = (float)texture.height / bccad.sheetH; HeightRatio = (float)texture.height / bccad.SheetH;
WidthRatio = (float)texture.width / bccad.sheetW; WidthRatio = (float)texture.width / bccad.SheetW;
_bccad = bccad; _bccad = bccad;
CalculateParts(); CalculateParts();
} }
public GameObject ParentObject { get; }
public float HeightRatio { get; }
public float WidthRatio { get; }
private void CalculateParts() private void CalculateParts()
{ {
var defaultSprite = _bccad.sprites[_data.SpriteIndex]; var defaultSprite = _bccad.Sprites[_data.SpriteIndex];
if (_data.Animations.Count == 0) if (_data.Animations.Count == 0)
{ {
for (var index = 0; index < defaultSprite.parts.Count; index++) for (var index = 0; index < defaultSprite.parts.Count; index++)
@ -74,10 +74,7 @@ namespace Bread2Unity
} }
} }
foreach (var r in regionsList) foreach (var r in regionsList) RegionToChild.Add(r, child);
{
RegionToChild.Add(r, child);
}
childIndex++; childIndex++;
} }
@ -91,12 +88,8 @@ namespace Bread2Unity
{ {
var regionsOfSprite = sprite.parts.Select(part => part.RegionIndex).ToArray(); var regionsOfSprite = sprite.parts.Select(part => part.RegionIndex).ToArray();
if (regionsOfSprite.Intersect(regions).Any()) if (regionsOfSprite.Intersect(regions).Any())
{
foreach (var r in regionsOfSprite) foreach (var r in regionsOfSprite)
{
notAdjacentRegions.Remove(r); notAdjacentRegions.Remove(r);
}
}
} }
return notAdjacentRegions; return notAdjacentRegions;
@ -104,7 +97,7 @@ namespace Bread2Unity
public List<Tuple<int, SpritePart, GameObject>> GetHiddenParts() public List<Tuple<int, SpritePart, GameObject>> GetHiddenParts()
{ {
var sprite = _bccad.sprites[_data.SpriteIndex]; var sprite = _bccad.Sprites[_data.SpriteIndex];
// index, part, game object // index, part, game object
var hiddenParts = new List<Tuple<int, SpritePart, GameObject>>(); var hiddenParts = new List<Tuple<int, SpritePart, GameObject>>();
var gameObjects = new List<GameObject>(_children); var gameObjects = new List<GameObject>(_children);
@ -120,12 +113,13 @@ namespace Bread2Unity
var region = RegionToChild.FirstOrDefault(keyValuePair => keyValuePair.Value == gameObject) var region = RegionToChild.FirstOrDefault(keyValuePair => keyValuePair.Value == gameObject)
.Key; .Key;
var partIndexPairs = _data.Animations.SelectMany(anim => anim.Steps).Select(s => s.BccadSprite) var partIndexPairs = _data.Animations.SelectMany(anim => anim.Steps).Select(s => s.BccadSprite)
.SelectMany(bccadSprite => bccadSprite.parts.Select((part, index) => new {part, index})); .SelectMany(bccadSprite => bccadSprite.parts.Select((part, index) => new { part, index }));
// Get the first possible part that the game object can have. // Get the first possible part that the game object can have.
var partIndexPair = partIndexPairs var partIndexPair = partIndexPairs
.First(pair => pair.part.RegionIndex.Equals(region)); .First(pair => pair.part.RegionIndex.Equals(region));
hiddenParts.Add(new Tuple<int, SpritePart, GameObject>(partIndexPair.index, partIndexPair.part, gameObject)); hiddenParts.Add(
new Tuple<int, SpritePart, GameObject>(partIndexPair.index, partIndexPair.part, gameObject));
} }
return hiddenParts; return hiddenParts;

View file

@ -1,11 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Bread2Unity;
using NUnit.Framework;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using Animation = Bread2Unity.Animation;
namespace Bread2Unity namespace Bread2Unity
{ {
@ -21,7 +18,7 @@ namespace Bread2Unity
var root = PrefabUtility.LoadPrefabContents(prefabAssetPath); var root = PrefabUtility.LoadPrefabContents(prefabAssetPath);
foreach (var prefabData in prefabDataList) foreach (var prefabData in prefabDataList)
{ {
var defaultSprite = bccad.sprites[prefabData.SpriteIndex]; var defaultSprite = bccad.Sprites[prefabData.SpriteIndex];
var bccadPrefab = new BccadPrefab(prefabData, bccad, texture); var bccadPrefab = new BccadPrefab(prefabData, bccad, texture);
var newPrefab = bccadPrefab.ParentObject; var newPrefab = bccadPrefab.ParentObject;
newPrefab.transform.SetParent(root.transform); newPrefab.transform.SetParent(root.transform);

View file

@ -1,37 +1,33 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using Bread2Unity;
namespace Bread2Unity namespace Bread2Unity
{ {
public class SpriteCreator : MonoBehaviour public class SpriteCreator : MonoBehaviour
{ {
public const int PixelsPerUnit = 100; public const int PixelsPerUnit = 100;
public static Texture2D ComputeSprites(BCCAD bccad, string texturePath, string prefabName, bool shouldRotate = false)
public static Texture2D ComputeSprites(BCCAD bccad, string texturePath, string prefabName,
bool shouldRotate = false)
{ {
var textureName = Path.GetFileName(texturePath); var textureName = Path.GetFileName(texturePath);
var spritesFolder = var spritesFolder =
$"Assets\\Resources\\Sprites\\Games\\{char.ToUpperInvariant(prefabName[0]) + prefabName.Substring(1)}\\"; $"Assets\\Resources\\Sprites\\Games\\{char.ToUpperInvariant(prefabName[0]) + prefabName.Substring(1)}\\";
if (!Directory.Exists(spritesFolder)) if (!Directory.Exists(spritesFolder)) Directory.CreateDirectory(spritesFolder);
{
Directory.CreateDirectory(spritesFolder);
}
var destTexturePath = var destTexturePath =
spritesFolder + spritesFolder +
$"{textureName}"; $"{textureName}";
var newTexture = new Texture2D(bccad.sheetW, bccad.sheetH); var newTexture = new Texture2D(bccad.SheetW, bccad.SheetH);
newTexture.LoadImage(File.ReadAllBytes(texturePath)); newTexture.LoadImage(File.ReadAllBytes(texturePath));
var finalTexture = shouldRotate ? RotateTexture(newTexture) : newTexture; var finalTexture = shouldRotate ? RotateTexture(newTexture) : newTexture;
finalTexture.name = textureName.Substring(0, textureName.Length - ".png".Length); finalTexture.name = textureName.Substring(0, textureName.Length - ".png".Length);
File.WriteAllBytes(destTexturePath, finalTexture.EncodeToPNG()); File.WriteAllBytes(destTexturePath, finalTexture.EncodeToPNG());
AssetDatabase.ImportAsset(destTexturePath); AssetDatabase.ImportAsset(destTexturePath);
var ti = AssetImporter.GetAtPath(destTexturePath) as TextureImporter; var ti = AssetImporter.GetAtPath(destTexturePath) as TextureImporter;
if (ti != null) if (ti != null)
{ {
ti.isReadable = true; ti.isReadable = true;
@ -43,18 +39,18 @@ namespace Bread2Unity
ti.textureCompression = TextureImporterCompression.Uncompressed; ti.textureCompression = TextureImporterCompression.Uncompressed;
var newData = new List<SpriteMetaData>(); var newData = new List<SpriteMetaData>();
var rectCtr = 0; var rectCtr = 0;
var heightRatio = (float)finalTexture.height / bccad.sheetH; var heightRatio = (float)finalTexture.height / bccad.SheetH;
var widthRatio = (float)finalTexture.width / bccad.sheetW; var widthRatio = (float)finalTexture.width / bccad.SheetW;
foreach (var r in bccad.regions) foreach (var r in bccad.Regions)
{ {
var smd = new SpriteMetaData var smd = new SpriteMetaData
{ {
pivot = new Vector2(0.5f, 0.5f), pivot = new Vector2(0.5f, 0.5f),
alignment = 0, alignment = 0,
name = finalTexture.name + "_" + rectCtr, name = finalTexture.name + "_" + rectCtr,
rect = new Rect(r.regionX * widthRatio, rect = new Rect(r.RegionX * widthRatio,
finalTexture.height - (r.regionH + r.regionY) * heightRatio, r.regionW * widthRatio, finalTexture.height - (r.RegionH + r.RegionY) * heightRatio, r.RegionW * widthRatio,
r.regionH * heightRatio) r.RegionH * heightRatio)
}; };
newData.Add(smd); newData.Add(smd);
@ -70,11 +66,11 @@ namespace Bread2Unity
public static Texture2D RotateTexture(Texture2D image) public static Texture2D RotateTexture(Texture2D image)
{ {
Texture2D var
target = new Texture2D(image.height, image.width, image.format, target = new Texture2D(image.height, image.width, image.format,
false); //flip image width<>height, as we rotated the image, it might be a rect. not a square image false); //flip image width<>height, as we rotated the image, it might be a rect. not a square image
Color32[] pixels = image.GetPixels32(0); var pixels = image.GetPixels32(0);
pixels = RotateTextureGrid(pixels, image.width, image.height); pixels = RotateTextureGrid(pixels, image.width, image.height);
target.SetPixels32(pixels); target.SetPixels32(pixels);
target.Apply(); target.Apply();
@ -87,15 +83,11 @@ namespace Bread2Unity
private static Color32[] RotateTextureGrid(Color32[] tex, int wid, int hi) private static Color32[] RotateTextureGrid(Color32[] tex, int wid, int hi)
{ {
Color32[] ret = new Color32[wid * hi]; //reminder we are flipping these in the target var ret = new Color32[wid * hi]; //reminder we are flipping these in the target
for (int y = 0; y < hi; y++) for (var y = 0; y < hi; y++)
{ for (var x = 0; x < wid; x++)
for (int x = 0; x < wid; x++) ret[hi - 1 - y + x * hi] = tex[x + y * wid]; //juggle the pixels around
{
ret[(hi - 1) - y + x * hi] = tex[x + y * wid]; //juggle the pixels around
}
}
return ret; return ret;
} }

View file

@ -1,10 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Bread2Unity;
using UnityEngine; using UnityEngine;
using Animation = Bread2Unity.Animation;
namespace Bread2Unity namespace Bread2Unity
{ {
@ -12,39 +9,39 @@ namespace Bread2Unity
{ {
public static BCCAD Read(byte[] bytes) public static BCCAD Read(byte[] bytes)
{ {
BCCAD bccad = new BCCAD(); var bccad = new BCCAD();
var byteBuffer = new ByteBuffer(bytes); var byteBuffer = new ByteBuffer(bytes);
byteBuffer.ReadInt(); //timestamp byteBuffer.ReadInt(); //timestamp
bccad.sheetW = byteBuffer.ReadUShort(); bccad.SheetW = byteBuffer.ReadUShort();
bccad.sheetH = byteBuffer.ReadUShort(); bccad.SheetH = byteBuffer.ReadUShort();
// Sprites // Sprites
var spritesNum = byteBuffer.ReadInt(); var spritesNum = byteBuffer.ReadInt();
for (int i = 0; i < spritesNum; i++) for (var i = 0; i < spritesNum; i++)
{ {
BccadSprite bccadSprite = new BccadSprite(); var bccadSprite = new BccadSprite();
var partsNum = byteBuffer.ReadInt(); var partsNum = byteBuffer.ReadInt();
for (int j = 0; j < partsNum; j++) for (var j = 0; j < partsNum; j++)
{ {
SpritePart part = new SpritePart(); var part = new SpritePart();
var region = new Region var region = new Region
{ {
regionX = byteBuffer.ReadUShort(), RegionX = byteBuffer.ReadUShort(),
regionY = byteBuffer.ReadUShort(), RegionY = byteBuffer.ReadUShort(),
regionW = byteBuffer.ReadUShort(), RegionW = byteBuffer.ReadUShort(),
regionH = byteBuffer.ReadUShort() RegionH = byteBuffer.ReadUShort()
}; };
var result = bccad.regions.FindIndex(x => Equals(x, region)); var result = bccad.Regions.FindIndex(x => Equals(x, region));
if (result == -1) if (result == -1)
{ {
bccad.regions.Add(region); bccad.Regions.Add(region);
part.RegionIndex = new RegionIndex(bccad.regions.Count - 1, 0); part.RegionIndex = new RegionIndex(bccad.Regions.Count - 1, 0);
} }
else else
{ {
var repeatedNumber = bccadSprite.parts.Count(p => p.RegionIndex.Index == result); var repeatedNumber = bccadSprite.parts.Count(p => p.RegionIndex.Index == result);
part.RegionIndex = new RegionIndex(result, repeatedNumber); part.RegionIndex = new RegionIndex(result, repeatedNumber);
} }
part.PosX = byteBuffer.ReadShort(); part.PosX = byteBuffer.ReadShort();
part.PosY = byteBuffer.ReadShort(); part.PosY = byteBuffer.ReadShort();
part.StretchX = byteBuffer.ReadFloat(); part.StretchX = byteBuffer.ReadFloat();
@ -61,11 +58,8 @@ namespace Bread2Unity
Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f); Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f);
part.Multicolor.a = byteBuffer.ReadByte() / 255f; part.Multicolor.a = byteBuffer.ReadByte() / 255f;
for (int k = 0; k < 12; k++) for (var k = 0; k < 12; k++) byteBuffer.ReadByte();
{
byteBuffer.ReadByte();
}
part.designation = byteBuffer.ReadByte(); part.designation = byteBuffer.ReadByte();
part.unknown = byteBuffer.ReadShort(); part.unknown = byteBuffer.ReadShort();
@ -76,30 +70,24 @@ namespace Bread2Unity
bccadSprite.parts.Add(part); bccadSprite.parts.Add(part);
} }
bccad.sprites.Add(bccadSprite); bccad.Sprites.Add(bccadSprite);
} }
// Animations // Animations
var animationsNum = byteBuffer.ReadInt(); var animationsNum = byteBuffer.ReadInt();
for (int i = 0; i < animationsNum; i++) for (var i = 0; i < animationsNum; i++)
{ {
var anim = new Animation(); var anim = new Animation();
var nameBuilder = new StringBuilder(); var nameBuilder = new StringBuilder();
var length = Convert.ToInt32(byteBuffer.ReadByte()); var length = Convert.ToInt32(byteBuffer.ReadByte());
for (int j = 0; j < length; j++) for (var j = 0; j < length; j++) nameBuilder.Append(Convert.ToChar(byteBuffer.ReadByte()));
{
nameBuilder.Append(Convert.ToChar(byteBuffer.ReadByte())); for (var j = 0; j < 4 - (length + 1) % 4; j++) byteBuffer.ReadByte();
}
for (int j = 0; j < 4 - ((length + 1) % 4); j++)
{
byteBuffer.ReadByte();
}
anim.Name = nameBuilder.ToString(); anim.Name = nameBuilder.ToString();
anim.InterpolationInt = byteBuffer.ReadInt(); anim.InterpolationInt = byteBuffer.ReadInt();
var stepsNum = byteBuffer.ReadInt(); var stepsNum = byteBuffer.ReadInt();
for (int j = 0; j < stepsNum; j++) for (var j = 0; j < stepsNum; j++)
{ {
var spriteIndex = byteBuffer.ReadUShort(); var spriteIndex = byteBuffer.ReadUShort();
var step = new AnimationStep var step = new AnimationStep
@ -114,7 +102,7 @@ namespace Bread2Unity
Color = new Color(Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f, Color = new Color(Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f,
Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f, Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f,
Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f), Convert.ToInt32(byteBuffer.ReadByte() & 0xff) / 255f),
BccadSprite = bccad.sprites[spriteIndex] BccadSprite = bccad.Sprites[spriteIndex]
}; };
byteBuffer.ReadByte(); byteBuffer.ReadByte();
byteBuffer.ReadByte(); byteBuffer.ReadByte();
@ -123,7 +111,7 @@ namespace Bread2Unity
anim.Steps.Add(step); anim.Steps.Add(step);
} }
bccad.animations.Add(anim); bccad.Animations.Add(anim);
} }
return bccad; return bccad;

View file

@ -1,10 +1,8 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using UnityEngine;
using UnityEditor; using UnityEditor;
using Bread2Unity; using UnityEngine;
namespace Bread2Unity namespace Bread2Unity
{ {
@ -13,38 +11,22 @@ namespace Bread2Unity
private const string EditorFolderName = "bread2unity"; private const string EditorFolderName = "bread2unity";
private const float Width = 657; private const float Width = 657;
private const float Height = 442; private const float Height = 442;
private GameObject _prefab; private readonly List<string> _animationsIndexes = new List<string>();
private readonly List<PrefabData> _prefabDataList = new List<PrefabData>();
private DataModel _animation; private DataModel _animation;
private List<PrefabData> _prefabDataList = new List<PrefabData>(); private GameObject _prefab;
private List<string> _animationsIndexes = new List<string> ();
private bool _shouldRotate;
private Vector2 _scrollPosition; private Vector2 _scrollPosition;
private bool _shouldRotate;
[MenuItem("Tools/bread2unity")]
public static void ShowWindow()
{
var window = GetWindow<Bread2UnityGUI>("bread2unity");
var x = Screen.currentResolution.width / 2f - Width;
var y = Screen.currentResolution.height / 2f - Height;
window.position = new Rect(x,y,Width,Height);
}
public void CreateGUI()
{
_animationsIndexes.Add("");
_prefabDataList.Add(new PrefabData("", 0));
}
public void OnGUI() public void OnGUI()
{ {
// Logo // Logo
Texture logo = var logo =
(Texture)AssetDatabase.LoadAssetAtPath($"Assets/Editor/{EditorFolderName}/logo.png", typeof(Texture)); (Texture)AssetDatabase.LoadAssetAtPath($"Assets/Editor/{EditorFolderName}/logo.png", typeof(Texture));
GUILayout.Box(logo, GUILayout.ExpandWidth(true), GUILayout.Height(60)); GUILayout.Box(logo, GUILayout.ExpandWidth(true), GUILayout.Height(60));
GUILayout.Space(30); GUILayout.Space(30);
GUIStyle desc = EditorStyles.label; var desc = EditorStyles.label;
desc.wordWrap = true; desc.wordWrap = true;
desc.fontStyle = FontStyle.BoldAndItalic; desc.fontStyle = FontStyle.BoldAndItalic;
// Description // Description
@ -110,7 +92,7 @@ namespace Bread2Unity
_shouldRotate = GUILayout.Toggle(_shouldRotate, "Rotate Spritesheet"); _shouldRotate = GUILayout.Toggle(_shouldRotate, "Rotate Spritesheet");
GUILayout.Space(12f); GUILayout.Space(12f);
// Create button // Create button
if (GUILayout.Button("Generate Assets") && _prefab != null) if (GUILayout.Button("Generate Assets") && _prefab != null)
{ {
@ -123,9 +105,10 @@ namespace Bread2Unity
if (!string.IsNullOrEmpty(pngFilePath)) if (!string.IsNullOrEmpty(pngFilePath))
{ {
var bccad = BCCAD.Read(File.ReadAllBytes(bccadFilePath)); var bccad = BCCAD.Read(File.ReadAllBytes(bccadFilePath));
var spriteTexture = SpriteCreator.ComputeSprites(bccad, pngFilePath, _prefab.name, _shouldRotate); var spriteTexture =
SpriteCreator.ComputeSprites(bccad, pngFilePath, _prefab.name, _shouldRotate);
//Create prefab from prefab data //Create prefab from prefab data
for (int i = 0; i < _prefabDataList.Count; i++) for (var i = 0; i < _prefabDataList.Count; i++)
{ {
List<int> animationIndexes; List<int> animationIndexes;
var prefabData = _prefabDataList[i]; var prefabData = _prefabDataList[i];
@ -134,7 +117,7 @@ namespace Bread2Unity
else else
animationIndexes = _animationsIndexes[i].Split(',').Select(int.Parse).ToList(); animationIndexes = _animationsIndexes[i].Split(',').Select(int.Parse).ToList();
prefabData.Animations = prefabData.Animations =
animationIndexes.Select(index => bccad.animations[index]).ToList(); animationIndexes.Select(index => bccad.Animations[index]).ToList();
} }
PrefabCreator.CreatePrefab(_prefab, bccad, _prefabDataList, spriteTexture); PrefabCreator.CreatePrefab(_prefab, bccad, _prefabDataList, spriteTexture);
@ -143,14 +126,28 @@ namespace Bread2Unity
} }
GUILayout.Space(12f); GUILayout.Space(12f);
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
if (GUILayout.Button("Bread Download", GUILayout.Height(40))) if (GUILayout.Button("Bread Download", GUILayout.Height(40)))
{
Application.OpenURL("https://github.com/rhmodding/bread"); Application.OpenURL("https://github.com/rhmodding/bread");
}
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
} }
public void CreateGUI()
{
_animationsIndexes.Add("");
_prefabDataList.Add(new PrefabData("", 0));
}
[MenuItem("Tools/bread2unity")]
public static void ShowWindow()
{
var window = GetWindow<Bread2UnityGUI>("bread2unity");
var x = Screen.currentResolution.width / 2f - Width;
var y = Screen.currentResolution.height / 2f - Height;
window.position = new Rect(x, y, Width, Height);
}
} }
} }

View file

@ -6,14 +6,14 @@ namespace Bread2Unity
{ {
private readonly byte[] _bytes; private readonly byte[] _bytes;
public int ReadPoint{ get; private set; }
public ByteBuffer(byte[] bytes) public ByteBuffer(byte[] bytes)
{ {
_bytes = bytes; _bytes = bytes;
ReadPoint = 0; ReadPoint = 0;
} }
public int ReadPoint { get; private set; }
public ushort ReadUShort() public ushort ReadUShort()
{ {
var result = BitConverter.ToUInt16(_bytes, ReadPoint); var result = BitConverter.ToUInt16(_bytes, ReadPoint);
@ -30,21 +30,21 @@ namespace Bread2Unity
public float ReadFloat() public float ReadFloat()
{ {
float result = BitConverter.ToSingle(_bytes, ReadPoint); var result = BitConverter.ToSingle(_bytes, ReadPoint);
ReadPoint += sizeof(float); ReadPoint += sizeof(float);
return result; return result;
} }
public byte ReadByte() public byte ReadByte()
{ {
byte result = _bytes[ReadPoint]; var result = _bytes[ReadPoint];
ReadPoint += sizeof(byte); ReadPoint += sizeof(byte);
return result; return result;
} }
public int ReadInt() public int ReadInt()
{ {
int result = BitConverter.ToInt32(_bytes, ReadPoint); var result = BitConverter.ToInt32(_bytes, ReadPoint);
ReadPoint += sizeof(int); ReadPoint += sizeof(int);
return result; return result;
} }

View file

@ -1,4 +1,3 @@
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -6,30 +5,29 @@ namespace Bread2Unity
{ {
public class Animation public class Animation
{ {
public List<AnimationStep> Steps = new List<AnimationStep>();
public string Name;
public int InterpolationInt = 0; public int InterpolationInt = 0;
public string Name;
public List<AnimationStep> Steps = new List<AnimationStep>();
public bool Interpolated => (InterpolationInt & 0b1) > 0; public bool Interpolated => (InterpolationInt & 0b1) > 0;
} }
public class AnimationStep public class AnimationStep
{ {
public BccadSprite BccadSprite;
public Color Color; //needs to add
public ushort Delay; public ushort Delay;
public BccadSprite BccadSprite;
public short TranslateX;
public short TranslateY;
public float Depth; public float Depth;
public float StretchX;//needs to add public byte Opacity; //needs to add
public float StretchY; //needs to add
public float Rotation; //needs to add public float Rotation; //needs to add
public byte Opacity;//needs to add public float StretchX; //needs to add
public Color Color;//needs to add public float StretchY; //needs to add
public short TranslateX;
public short TranslateY;
} }
} }

View file

@ -1,5 +1,3 @@
using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
@ -12,28 +10,29 @@ namespace Bread2Unity
public class SpritePart public class SpritePart
{ {
public RegionIndex RegionIndex; public float blDepth;
public float brDepth;
public short PosX;
public short PosY;
public float StretchX; public byte designation;
public float StretchY;
public float Rotation;
public bool FlipX; public bool FlipX;
public bool FlipY; public bool FlipY;
public byte designation;
public short unknown;
public float tlDepth;
public float blDepth;
public float trDepth;
public float brDepth;
public Color Multicolor; public Color Multicolor;
public short PosX;
public short PosY;
public RegionIndex RegionIndex;
public float Rotation;
public Color ScreenColor; public Color ScreenColor;
public float StretchX;
public float StretchY;
public float tlDepth;
public float trDepth;
public short unknown;
// public Color GetColor() => new Color(Multicolor.r, Multicolor.g, Multicolor.b, Multicolor.a); // public Color GetColor() => new Color(Multicolor.r, Multicolor.g, Multicolor.b, Multicolor.a);
} }
@ -60,7 +59,7 @@ namespace Bread2Unity
public override int GetHashCode() public override int GetHashCode()
{ {
int hash = 23; var hash = 23;
hash = hash * 31 + Index; hash = hash * 31 + Index;
hash = hash * 31 + RepeatedNumber; hash = hash * 31 + RepeatedNumber;
return hash; return hash;

View file

@ -1,52 +1,52 @@
using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
namespace Bread2Unity namespace Bread2Unity
{ {
public class DataModel public class DataModel
{ {
public List<Region> regions = new List<Region>(); public List<Animation> Animations = new List<Animation>();
public List<BccadSprite> sprites = new List<BccadSprite>(); public List<Region> Regions = new List<Region>();
public List<Animation> animations = new List<Animation>(); public int SheetH;
public int sheetW; public int SheetW;
public int sheetH; public List<BccadSprite> Sprites = new List<BccadSprite>();
} }
public class Region public class Region
{ {
public ushort regionX; public ushort RegionH;
public ushort regionY; public ushort RegionW;
public ushort regionW; public ushort RegionX;
public ushort regionH; public ushort RegionY;
public override string ToString() public override string ToString()
{ {
return $"regionX: {regionX} regionY: {regionY} regionW: {regionW} regionH: {regionH}"; return $"regionX: {RegionX} regionY: {RegionY} regionW: {RegionW} regionH: {RegionH}";
} }
protected bool Equals(Region other) protected bool Equals(Region other)
{ {
return regionX == other.regionX && regionY == other.regionY && regionW == other.regionW && return RegionH == other.RegionH && RegionW == other.RegionW && RegionX == other.RegionX &&
regionH == other.regionH; RegionY == other.RegionY;
} }
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true; if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false; if (obj.GetType() != GetType()) return false;
return Equals((Region)obj); return Equals((Region)obj);
} }
public override int GetHashCode() public override int GetHashCode()
{ {
int hash = 23; unchecked
hash = hash * 31 + regionX; {
hash = hash * 31 + regionY; var hashCode = RegionH.GetHashCode();
hash = hash * 31 + regionW; hashCode = (hashCode * 397) ^ RegionW.GetHashCode();
hash = hash * 31 + regionH; hashCode = (hashCode * 397) ^ RegionX.GetHashCode();
return hash; hashCode = (hashCode * 397) ^ RegionY.GetHashCode();
return hashCode;
}
} }
} }
} }

View file

@ -1,12 +1,11 @@
using System.Collections.Generic; using System.Collections.Generic;
using Bread2Unity;
namespace Bread2Unity namespace Bread2Unity
{ {
public class PrefabData public class PrefabData
{ {
public string Name;
public List<Animation> Animations; public List<Animation> Animations;
public string Name;
public int SpriteIndex; public int SpriteIndex;
public PrefabData(string name, int spriteIndex) public PrefabData(string name, int spriteIndex)

View file

@ -20,6 +20,7 @@ Rhythm Heaven animation to Unity animation converter
- [X] remove bccad test - [X] remove bccad test
- [X] add default line for bcaad data - [X] add default line for bcaad data
- [X] change window height and width - [X] change window height and width
- [x] fix imports
- [ ] change sprite order with sprite renderer. - [ ] change sprite order with sprite renderer.
- [ ] write a normal readme - [ ] write a normal readme