Texture resize done using raw data from texture2d. Got rid of unused plugins. Improved image change timing
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
using Image = UnityEngine.UI.Image;
|
||||
using Color = UnityEngine.Color;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using SixLabors.ImageSharp;
|
||||
|
||||
public class PhotoFrame : MonoBehaviour
|
||||
{
|
||||
const float PHOTO_INTERVAL = 5f;
|
||||
const float ANIMATION_LENGTH = 2f;
|
||||
|
||||
[SerializeField]
|
||||
GameObject mainUI;
|
||||
|
||||
@@ -35,6 +33,7 @@ public class PhotoFrame : MonoBehaviour
|
||||
|
||||
public void Start()
|
||||
{
|
||||
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
||||
currentImage = 0;
|
||||
ActivateUI(true);
|
||||
}
|
||||
@@ -86,13 +85,12 @@ public class PhotoFrame : MonoBehaviour
|
||||
NextImage.color = transparent;
|
||||
while (true)
|
||||
{
|
||||
var startTime = Time.realtimeSinceStartup;
|
||||
int imageIndex = Random.Range(0, loader.images.Count);
|
||||
string imageUrl = loader.images[imageIndex];
|
||||
|
||||
var path = Application.persistentDataPath + "/source." + System.IO.Path.GetExtension(imageUrl);
|
||||
|
||||
var request = UnityWebRequest.Get(imageUrl);
|
||||
request.downloadHandler = new DownloadHandlerFile(path);
|
||||
request.downloadHandler = new DownloadHandlerTexture(true);
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
if (request.isNetworkError || request.isHttpError)
|
||||
@@ -101,19 +99,28 @@ public class PhotoFrame : MonoBehaviour
|
||||
continue;
|
||||
}
|
||||
|
||||
SetImageTexture(NextImage, path);
|
||||
request.downloadHandler.Dispose();
|
||||
var downloadTime = Time.realtimeSinceStartup - startTime;
|
||||
if (PHOTO_INTERVAL > downloadTime)
|
||||
{
|
||||
yield return new WaitForSeconds(PHOTO_INTERVAL - downloadTime);
|
||||
}
|
||||
|
||||
var dh = request.downloadHandler as DownloadHandlerTexture;
|
||||
var tex = dh.texture;
|
||||
|
||||
SetImageTexture(NextImage, tex);
|
||||
dh.Dispose();
|
||||
request.Dispose();
|
||||
|
||||
float currentStart = CurrentImage.color.a;
|
||||
float currentFinish = transparent.a;
|
||||
float nextStart = NextImage.color.a;
|
||||
float nextFinish = opaque.a;
|
||||
float startTime = Time.time;
|
||||
while (Time.time < startTime + 2f)
|
||||
startTime = Time.time;
|
||||
while (Time.time < startTime + ANIMATION_LENGTH)
|
||||
{
|
||||
float ct = Time.time;
|
||||
float x = (ct - startTime) / 2f;
|
||||
float x = (ct - startTime) / ANIMATION_LENGTH;
|
||||
float currentA = currentStart + x * (currentFinish - currentStart);
|
||||
Color cC = new Color(CurrentImage.color.r, CurrentImage.color.g, CurrentImage.color.b, currentA);
|
||||
CurrentImage.color = cC;
|
||||
@@ -122,7 +129,6 @@ public class PhotoFrame : MonoBehaviour
|
||||
NextImage.color = nC;
|
||||
yield return null;
|
||||
}
|
||||
yield return new WaitForSeconds(5f);
|
||||
|
||||
currentImage = (currentImage + 1) % images.Length;
|
||||
Resources.UnloadUnusedAssets();
|
||||
@@ -130,7 +136,7 @@ public class PhotoFrame : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void SetImageTexture(Image nextImage, string path)
|
||||
private void SetImageTexture(Image nextImage, Texture2D tex)
|
||||
{
|
||||
if (nextImage.sprite != null)
|
||||
{
|
||||
@@ -155,31 +161,13 @@ public class PhotoFrame : MonoBehaviour
|
||||
maxWidth = 2048;
|
||||
maxHeight = (int)(2048 / imageWidth * imageHeight);
|
||||
}
|
||||
Texture2D tex = new Texture2D(2, 2);
|
||||
tex.LoadImage(File.ReadAllBytes(path));
|
||||
float factor = Mathf.Min((float) maxWidth / tex.width, (float)maxHeight / tex.height);
|
||||
int newWidth = (int)(tex.width * factor);
|
||||
int newHeight = (int)(tex.height * factor);
|
||||
Destroy(tex);
|
||||
var newPath = Scale(path, newWidth, newHeight);
|
||||
tex = new Texture2D(2, 2);
|
||||
tex.LoadImage(File.ReadAllBytes(newPath));
|
||||
//TextureScale.Bilinear(tex, newWidth, newHeight);
|
||||
//TextureScale.Point(tex, newWidth, newHeight);
|
||||
//Color[] cs = tex.GetPixels();
|
||||
TextureScale.Bilinear(tex, newWidth, newHeight);
|
||||
|
||||
var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.zero);
|
||||
nextImage.sprite = sprite;
|
||||
nextImage.rectTransform.anchorMin = new Vector2((maxWidth - (float)newWidth) / maxWidth / 2, (maxHeight - (float)newHeight) / maxHeight / 2);
|
||||
}
|
||||
|
||||
private string Scale(string path, int newWidth, int newHeight)
|
||||
{
|
||||
var resultPath = Path.GetDirectoryName(path) + "/target." + Path.GetExtension(path);
|
||||
using (SixLabors.ImageSharp.Image image = SixLabors.ImageSharp.Image.Load(path))
|
||||
{
|
||||
image.Mutate(x => x.Resize(newWidth, newHeight));
|
||||
image.Save(resultPath);
|
||||
}
|
||||
return resultPath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user