rimworld-animation-studio/Assets/Scripts/DirectionalGraphic.cs
2022-09-25 23:10:41 -05:00

51 lines
1.6 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using UnityEngine;
namespace RimWorldAnimationStudio
{
[Serializable]
public class DirectionalGraphic
{
public string path = "Invalid file path";
[XmlIgnore] public Sprite sprite = null;
public void SetPath(string path)
{
string streamingAssets = Path.GetFullPath(Application.streamingAssetsPath);
string combinedPath = Path.GetFullPath(Path.Combine(streamingAssets, path));
if (path == null || path.Any() == false || Path.GetExtension(path) != ".png")
{ this.path = null; return; }
if (File.Exists(path) == false && File.Exists(combinedPath) == false)
{ this.path = null; return; }
if (path.Contains(streamingAssets))
{
path = path.Replace(streamingAssets, "");
path = path.TrimStart(new char[] { '\\', '/' });
}
this.path = path;
sprite = LoadSprite(Path.GetFullPath(Path.Combine(streamingAssets, path)));
}
public Sprite LoadSprite(string path)
{
if (path == null || path.Any() == false || File.Exists(path) == false || Path.GetExtension(path) != ".png") return null;
byte[] pngBytes = File.ReadAllBytes(path);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(pngBytes);
Sprite sprite = Sprite.Create(texture, new Rect(0.0f, 0.0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 85.0f);
return sprite;
}
}
}