Skip to content

Commit

Permalink
Use Unity to pack sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoco007 committed Dec 31, 2023
1 parent e8e81ce commit 6d46320
Show file tree
Hide file tree
Showing 21 changed files with 646 additions and 208 deletions.
Binary file modified Source/CustomAvatar/Resources/Assets
Binary file not shown.
11 changes: 0 additions & 11 deletions Source/CustomAvatar/Resources/README.md

This file was deleted.

Binary file removed Source/CustomAvatar/Resources/ui.dds
Binary file not shown.
30 changes: 9 additions & 21 deletions Source/CustomAvatar/UI/AvatarListViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using CustomAvatar.Avatar;
Expand All @@ -29,6 +28,7 @@
using Polyglot;
using TMPro;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
using Zenject;

Expand All @@ -46,21 +46,21 @@ internal class AvatarListViewController : ViewController, TableView.IDataSource
private PlayerOptionsViewController _playerOptionsViewController;
private LevelCollectionViewController _levelCollectionViewController;
private PlatformLeaderboardViewController _leaderboardViewController;
private AssetLoader _assetLoader;

private FileSystemWatcher _fileSystemWatcher;
private TableView _tableView;

private readonly List<AvatarListItem> _avatars = new();
private AvatarListTableCell _tableCellPrefab;

private Texture2D _atlas;
private Sprite _blankAvatarSprite;
private Sprite _noAvatarSprite;
private Sprite _reloadSprite;
private Sprite _loadErrorSprite;

[Inject]
internal void Construct(ILogger<AvatarListViewController> logger, PlayerAvatarManager avatarManager, DiContainer container, MirrorViewController mirrorViewController, PlayerOptionsViewController playerOptionsViewController, LevelCollectionViewController levelCollectionViewController, PlatformLeaderboardViewController leaderboardViewController)
internal void Construct(ILogger<AvatarListViewController> logger, PlayerAvatarManager avatarManager, DiContainer container, MirrorViewController mirrorViewController, PlayerOptionsViewController playerOptionsViewController, LevelCollectionViewController levelCollectionViewController, PlatformLeaderboardViewController leaderboardViewController, AssetLoader assetLoader)
{
_logger = logger;
_avatarManager = avatarManager;
Expand All @@ -69,28 +69,16 @@ internal void Construct(ILogger<AvatarListViewController> logger, PlayerAvatarMa
_playerOptionsViewController = playerOptionsViewController;
_levelCollectionViewController = levelCollectionViewController;
_leaderboardViewController = leaderboardViewController;
_assetLoader = assetLoader;
}

protected void Start()
{
using (Stream fileStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CustomAvatar.Resources.ui.dds"))
{
_atlas = SimpleDdsLoader.LoadImage(fileStream);
_noAvatarSprite = Sprite.Create(_atlas, new Rect(0, 0, 256, 256), new Vector2(0.5f, 0.5f));
_blankAvatarSprite = Sprite.Create(_atlas, new Rect(256, 0, 256, 256), new Vector2(0.5f, 0.5f));
_reloadSprite = Sprite.Create(_atlas, new Rect(0, 256, 128, 128), new Vector2(0.5f, 0.5f));
_loadErrorSprite = Sprite.Create(_atlas, new Rect(256, 256, 256, 256), new Vector2(0.5f, 0.5f));
}
}

protected override void OnDestroy()
{
Destroy(_noAvatarSprite);
Destroy(_blankAvatarSprite);
Destroy(_reloadSprite);
Destroy(_atlas);

base.OnDestroy();
SpriteAtlas atlas = _assetLoader.uiSpriteAtlas;
_noAvatarSprite = atlas.GetSprite("NoAvatarIcon");
_blankAvatarSprite = atlas.GetSprite("BlankAvatarIcon");
_reloadSprite = atlas.GetSprite("ReloadButtonIcon");
_loadErrorSprite = atlas.GetSprite("LoadErrorIcon");
}

protected override async void DidActivate(bool firstActivation, bool addedToHierarchy, bool screenSystemEnabling)
Expand Down
8 changes: 8 additions & 0 deletions Source/CustomAvatar/Utilities/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Threading.Tasks;
using CustomAvatar.Logging;
using UnityEngine;
using UnityEngine.U2D;
using Zenject;
using Object = UnityEngine.Object;

Expand All @@ -38,6 +39,8 @@ internal AssetLoader(ILogger<AssetLoader> logger)

internal Shader unlitShader { get; private set; }

internal SpriteAtlas uiSpriteAtlas { get; private set; }

public void Initialize()
{
LoadAssetsAsync().ContinueWith((task) => _logger.LogCritical(task.Exception), TaskContinuationOptions.OnlyOnFaulted);
Expand All @@ -47,6 +50,7 @@ public void Dispose()
{
Object.Destroy(stereoMirrorShader);
Object.Destroy(unlitShader);
Object.Destroy(uiSpriteAtlas);
}

private async Task LoadAssetsAsync()
Expand Down Expand Up @@ -80,6 +84,10 @@ private async Task LoadAssetsAsync()
unlitShader = (Shader)asset;
break;

case "assets/sprites/ui.spriteatlasv2":
uiSpriteAtlas = (SpriteAtlas)asset;
break;

default:
_logger.LogError($"Unexpected asset '{name}'");
break;
Expand Down
171 changes: 0 additions & 171 deletions Source/CustomAvatar/Utilities/SimpleDdsLoader.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static void BuildAssetBundle()
assetNames = new[] {
"Assets/Shaders/StereoRender.shader",
"Assets/Shaders/UnlitOverlay.shader",
"Assets/Sprites/UI.spriteatlasv2",
},
};

Expand Down
8 changes: 8 additions & 0 deletions Unity/BuiltInAssets/Assets/Sprites.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6d46320

Please sign in to comment.