1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using StreamChat . Core . LowLevelClient ;
45using TMPro ;
5- using UnityEditor ;
66using UnityEngine ;
7- using UnityEngine . Serialization ;
8- #if UNITY_EDITOR
9- #endif
107
118namespace 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}
0 commit comments