Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/DaVikingCode/UnityRuntimeSpriteSheetsGenerator
Unity – generate SpriteSheets at runtime!
https://github.com/DaVikingCode/UnityRuntimeSpriteSheetsGenerator
runtime spritesheets unity
Last synced: about 4 hours ago
JSON representation
Unity – generate SpriteSheets at runtime!
- Host: GitHub
- URL: https://github.com/DaVikingCode/UnityRuntimeSpriteSheetsGenerator
- Owner: DaVikingCode
- License: mit
- Created: 2017-01-17T13:11:07.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-23T09:20:04.000Z (about 4 years ago)
- Last Synced: 2024-08-02T05:13:29.731Z (3 months ago)
- Topics: runtime, spritesheets, unity
- Language: C#
- Homepage: http://davikingcode.com/blog/unity-generate-spritesheets-at-runtime/
- Size: 207 KB
- Stars: 540
- Watchers: 30
- Forks: 140
- Open Issues: 1
-
Metadata Files:
- Readme: README.MD
- License: LICENSE.txt
Awesome Lists containing this project
README
Unity Runtime SpriteSheets Generator
====================================Unity and plugins provide many great ways to build Sprite Sheets. However they're used directly into Unity Editor or with an external software which is perfect in many case, but none provide the ability to generate SpriteSheets at runtime.
The RectanglePacking algorithm is a port of the AS3 version made by [Ville Koskela](https://github.com/villekoskelaorg/RectanglePacking). Assets used in the demo come from [Kenney](http://kenney.nl/).
You could combine the generated Sprite Sheets.png with a [pngquant](https://pngquant.org/) compression via this script [PngQuantNativeProcess](https://github.com/DaVikingCode/PngQuantNativeProcess).
Please note that Unity provides a [Texture2D.PackTextures method](https://docs.unity3d.com/ScriptReference/Texture2D.PackTextures.html), I didn't test it nor made benchmark performances.
Example:
--------
Add the `AssetPacker` component to your GameObject:
![AssetPacker](http://davikingcode.com/blog/wp-content/uploads/2017/01/AssetPacker.png)
```csharp
using DaVikingCode.AssetPacker;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;public class AssetPackerExample : MonoBehaviour {
public Image anim;AssetPacker assetPacker;
void Start () {string[] files = Directory.GetFiles(Application.persistentDataPath + "/Textures", "*.png");
assetPacker = GetComponent();
assetPacker.OnProcessCompleted.AddListener(LaunchAnimations);
assetPacker.AddTexturesToPack(files);
assetPacker.Process();
}void LaunchAnimations() {
StartCoroutine(LoadAnimation());
}IEnumerator LoadAnimation() {
Sprite[] sprites = assetPacker.GetSprites("walking");
int i = 0;
while (i < sprites.Length) {anim.sprite = sprites[i++];
yield return new WaitForSeconds(0.1f);
// loop
if (i == sprites.Length)
i = 0;
}
}
}```