Skip to content

Commit f9ebbec

Browse files
authored
Load sprites runtime due to occasional missing references when importing SDK to a fresh project (#114)
1 parent a0635cc commit f9ebbec

6 files changed

Lines changed: 37 additions & 51 deletions

File tree

Assets/Plugins/StreamChat/SampleProject/Textures/twemoji_sprite_atlas.png renamed to Assets/Plugins/StreamChat/SampleProject/Resources/twemoji_sprite_atlas.png

File renamed without changes.

Assets/Plugins/StreamChat/SampleProject/Textures/twemoji_sprite_atlas.png.meta renamed to Assets/Plugins/StreamChat/SampleProject/Resources/twemoji_sprite_atlas.png.meta

File renamed without changes.
Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,56 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using StreamChat.Core.LowLevelClient;
45
using TMPro;
5-
using UnityEditor;
66
using UnityEngine;
7-
using UnityEngine.Serialization;
8-
#if UNITY_EDITOR
9-
#endif
107

118
namespace StreamChat.SampleProject.Configs
129
{
1310
[CreateAssetMenu(fileName = "EmojiConfig",
1411
menuName = StreamChatLowLevelClient.MenuPrefix + "Demo/Create emoji config asset", order = 1)]
1512
public class EmojiConfigAsset : ScriptableObject, IEmojiConfig
1613
{
17-
public IEnumerable<Sprite> AllSprites => _allSprites;
18-
public IEnumerable<Sprite> ReactionSprites => _reactionSprites;
19-
2014
public TMP_SpriteAsset TMPSpriteAsset => _tmpSpriteAsset;
2115

22-
[FormerlySerializedAs("_sprites")]
23-
[SerializeField]
24-
private Sprite[] _allSprites;
16+
public Texture2D EmojisAtlasTexture => _sourceSpritesAtlas;
17+
public IEnumerable<Sprite> ReactionSprites => _reactions;
18+
public IEnumerable<Sprite> AllSprites => _emojis;
2519

26-
[SerializeField]
27-
private Sprite[] _reactionSprites;
20+
public void LoadEmojisSprites()
21+
{
22+
var sprites = Resources.LoadAll<Sprite>(_sourceSpritesAtlas.name);
23+
_emojis.AddRange(sprites);
24+
25+
var reactionNames = _reactionSpritesNames.Split(',').Select(n => n.Trim()).ToList();
26+
if (reactionNames.Count == 0)
27+
{
28+
Debug.LogError("No reaction names in the configuration file");
29+
return;
30+
}
31+
32+
foreach (var s in sprites)
33+
{
34+
if (reactionNames.Contains(s.name))
35+
{
36+
_reactions.Add(s);
37+
}
38+
}
39+
}
40+
41+
private readonly List<Sprite> _emojis = new List<Sprite>();
42+
private readonly List<Sprite> _reactions = new List<Sprite>();
2843

2944
[SerializeField]
3045
private TMP_SpriteAsset _tmpSpriteAsset;
3146

32-
#if UNITY_EDITOR
3347
[SerializeField]
3448
private Texture2D _sourceSpritesAtlas;
3549

3650
[Header("Reaction names to be found in source atlas. Separated by comma")]
3751
[SerializeField]
3852
private string _reactionSpritesNames;
3953

40-
protected void OnValidate()
41-
{
42-
if (_sourceSpritesAtlas == null)
43-
{
44-
return;
45-
}
46-
47-
var path = AssetDatabase.GetAssetPath(_sourceSpritesAtlas);
48-
var sprites = AssetDatabase.LoadAllAssetsAtPath(path)
49-
.OfType<Sprite>().ToDictionary(_ => _.name, _ => _);
50-
51-
var found = new List<Sprite>();
52-
53-
var keys = _reactionSpritesNames.Split(',').Select(_ => _.TrimStart().TrimEnd())
54-
.Where(_ => _ != string.Empty);
55-
56-
foreach (var key in keys)
57-
{
58-
if (!sprites.ContainsKey(key))
59-
{
60-
Debug.LogWarning("Failed to find sprite with key: " + key);
61-
continue;
62-
}
63-
64-
found.Add(sprites[key]);
65-
}
66-
67-
if (found.Any())
68-
{
69-
_reactionSprites = found.ToArray();
70-
}
71-
}
72-
#endif
54+
private IChatViewContext _viewContext;
7355
}
7456
}

Assets/Plugins/StreamChat/SampleProject/Scripts/Configs/IEmojiConfig.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ namespace StreamChat.SampleProject.Configs
66
{
77
public interface IEmojiConfig
88
{
9-
IEnumerable<Sprite> AllSprites { get; }
10-
IEnumerable<Sprite> ReactionSprites { get; }
119
TMP_SpriteAsset TMPSpriteAsset { get; }
10+
Texture2D EmojisAtlasTexture { get; }
11+
IEnumerable<Sprite> ReactionSprites { get; }
12+
IEnumerable<Sprite> AllSprites { get; }
13+
14+
void LoadEmojisSprites();
1215
}
1316
}

Assets/Plugins/StreamChat/SampleProject/Scripts/StreamChatClientBehaviour.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if UNITY_EDITOR
2+
using UnityEditor;
23
#endif
34
using System;
45
using System.Collections;
@@ -12,7 +13,6 @@
1213
using StreamChat.SampleProject.Utils;
1314
using StreamChat.SampleProject.Views;
1415
using TMPro;
15-
using UnityEditor;
1616
using UnityEngine;
1717
using UnityEngine.Serialization;
1818
using Object = UnityEngine.Object;

Assets/Plugins/StreamChat/SampleProject/Scripts/Views/ViewFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using StreamChat.Core.Helpers;
55
using StreamChat.Core.StatefulModels;
6-
using StreamChat.SampleProject.Utils;
76
using StreamChat.SampleProject.Configs;
87
using StreamChat.SampleProject.Popups;
98
using UnityEngine;
@@ -23,6 +22,8 @@ public ViewFactory(IAppConfig config, Transform popupsContainer)
2322
_appConfig = config ?? throw new ArgumentNullException(nameof(config));
2423
_config = config.ViewFactoryConfig ?? throw new ArgumentNullException(nameof(config.ViewFactoryConfig));
2524
_popupsContainer = popupsContainer ? popupsContainer : throw new ArgumentNullException(nameof(popupsContainer));
25+
26+
_appConfig.Emojis.LoadEmojisSprites();
2627
}
2728

2829
public void Init(IChatViewContext viewContext)

0 commit comments

Comments
 (0)