123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- using System;
- using System.IO;
- using UnityEngine;
- using Spine;
- public class AtlasAsset : ScriptableObject
- {
- public TextAsset atlasFile;
- public Material[] materials;
- private Atlas atlas;
- public void Reset()
- {
- atlas = null;
- }
-
- public Atlas GetAtlas()
- {
- if (atlasFile == null)
- {
- Debug.LogError("Atlas file not set for atlas asset: " + name, this);
- Reset();
- return null;
- }
- if (materials == null || materials.Length == 0)
- {
- Debug.LogError("Materials not set for atlas asset: " + name, this);
- Reset();
- return null;
- }
- if (atlas != null)
- return atlas;
- try
- {
- atlas = new Atlas(new StringReader(atlasFile.text), "", new MaterialsTextureLoader(this));
- atlas.FlipV();
- return atlas;
- }
- catch (Exception ex)
- {
- Debug.LogError("Error reading atlas file for atlas asset: " + name + "\n" + ex.Message + "\n" + ex.StackTrace, this);
- return null;
- }
- }
- }
- public class MaterialsTextureLoader : TextureLoader
- {
- AtlasAsset atlasAsset;
- public MaterialsTextureLoader(AtlasAsset atlasAsset)
- {
- this.atlasAsset = atlasAsset;
- }
- public void Load(AtlasPage page, String path)
- {
- String name = Path.GetFileNameWithoutExtension(path);
- Material material = null;
- foreach (Material other in atlasAsset.materials)
- {
- if (other.mainTexture == null)
- {
- Debug.LogError("Material is missing texture: " + other.name, other);
- return;
- }
- if (other.mainTexture.name == name)
- {
- material = other;
- break;
- }
- }
- if (material == null)
- {
- Debug.LogError("Material with texture name \"" + name + "\" not found for atlas asset: " + atlasAsset.name, atlasAsset);
- return;
- }
- page.rendererObject = material;
-
- if (page.width == 0 || page.height == 0)
- {
- page.width = material.mainTexture.width;
- page.height = material.mainTexture.height;
- }
- }
- public void Unload(object texture)
- {
- }
- }
|