Skip to content

Commit

Permalink
fix: Handle null or whitespace source path in DynamicImage
Browse files Browse the repository at this point in the history
  • Loading branch information
Vatsal Ambastha committed Jun 1, 2020
1 parent 0f442c5 commit a287126
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions Assets/Adrenak.UPF/Runtime/Core/DynamicImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ namespace Adrenak.UPF {
[Serializable]
public class DynamicImage : Image {
public enum Source {
Asset,
ResourcePath,
RemotePath
}

public enum Compression{
public enum Compression {
None,
HighQuality,
LowQuality
}

Compression _oldCompression = Compression.LowQuality;
[SerializeField] Compression _compression = Compression.LowQuality;
public Compression compression{
public Compression compression {
get => _compression;
set {
_compression = value;
Expand All @@ -35,7 +36,7 @@ public Source source {
get => _source;
set {
_source = value;
if(NeedsRefresh())
if (NeedsRefresh())
Refresh();
}
}
Expand All @@ -53,19 +54,30 @@ public string path {

[ContextMenu("Refresh")]
public void Refresh() {
if (_oldSource == source) {

}
switch (source) {
case Source.Asset:
break;

case Source.ResourcePath:
if (string.IsNullOrWhiteSpace(path)) {
Debug.LogWarning("Source path is null or whitespace");
break;
}

var resourceSprite = Resources.Load<Sprite>(path);
if(resourceSprite == null){
if (resourceSprite == null) {
Debug.LogError($"Not Resource found at {path}");
break;
}
SetSprite(resourceSprite);
break;

case Source.RemotePath:
if (string.IsNullOrWhiteSpace(path)) {
Debug.LogWarning("Source path is null or whitespace");
break;
}

try {
DownloadSprite(path,
result => SetSprite(result),
Expand All @@ -85,10 +97,10 @@ bool NeedsRefresh() {
if (_oldCompression != compression)
result = true;

if (_oldSource != source)
if (_oldSource != source)
result = true;

if(!_oldPath.Equals(path))
if (!_oldPath.Equals(path))
result = true;

_oldCompression = compression;
Expand All @@ -100,7 +112,7 @@ bool NeedsRefresh() {
void SetSprite(Sprite s) {
if (sprite != null) {
var id = sprite.GetInstanceID();
if(id < 0){
if (id < 0) {
if (Application.isPlaying) {
Destroy(sprite.texture);
Destroy(sprite);
Expand Down Expand Up @@ -132,7 +144,7 @@ IEnumerator DownloadSpriteCo(string path, Action<Sprite> onSuccess, Action<Excep
var tex = new Texture2D(2, 2);
tex.LoadImage(www.bytes);
tex.Apply();
if(compression != Compression.None)
if (compression != Compression.None)
tex.Compress(compression == Compression.HighQuality);

onSuccess?.Invoke(tex.ToSprite());
Expand Down

0 comments on commit a287126

Please sign in to comment.