281 lines
5.8 KiB
C#
281 lines
5.8 KiB
C#
|
using System;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.Gonbest.MagicCube;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 组合纹理的相关数据类
|
|||
|
/// </summary>
|
|||
|
public class UISutureTextureData
|
|||
|
{
|
|||
|
#region//私有变量
|
|||
|
//名字
|
|||
|
private string _name;
|
|||
|
private string _leftName;
|
|||
|
private string _rightName;
|
|||
|
|
|||
|
//左边纹理
|
|||
|
private Texture2D _left;
|
|||
|
|
|||
|
//右边纹理
|
|||
|
private Texture2D _right;
|
|||
|
|
|||
|
//是否使用自定义加载
|
|||
|
private bool _useCustomLoad = false;
|
|||
|
|
|||
|
//是否使用流文件
|
|||
|
private bool _isFromStreamFile = false;
|
|||
|
|
|||
|
//扩展名
|
|||
|
private string _fileExt = ".png";
|
|||
|
|
|||
|
//加载完成回调
|
|||
|
private Action<bool> _onLoadFinished;
|
|||
|
#endregion
|
|||
|
|
|||
|
#region//属性定义
|
|||
|
//当前纹理数据是否有效
|
|||
|
public bool IsValid
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _left != null && _right != null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public Texture2D Left
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _left;
|
|||
|
}
|
|||
|
set
|
|||
|
{
|
|||
|
_left = value;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public Texture2D Right
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _right;
|
|||
|
}
|
|||
|
set
|
|||
|
{
|
|||
|
_right = value;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public string Name
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _name;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool UseCustomLoad
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _useCustomLoad;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public string LeftName
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _leftName;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public string RightName
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _rightName;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool IsFromStreamFile
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _isFromStreamFile;
|
|||
|
}
|
|||
|
|
|||
|
set
|
|||
|
{
|
|||
|
_isFromStreamFile = value;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public string FileExt
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _fileExt;
|
|||
|
}
|
|||
|
|
|||
|
set
|
|||
|
{
|
|||
|
_fileExt = value;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region//构造函数
|
|||
|
public UISutureTextureData()
|
|||
|
{
|
|||
|
_useCustomLoad = false;
|
|||
|
}
|
|||
|
|
|||
|
public UISutureTextureData(bool useCustomLoad)
|
|||
|
{
|
|||
|
_useCustomLoad = useCustomLoad;
|
|||
|
}
|
|||
|
|
|||
|
public UISutureTextureData(string name, Texture2D left, Texture2D right)
|
|||
|
{
|
|||
|
_name = name;
|
|||
|
_leftName = _name + "_l";
|
|||
|
_rightName = _name + "_r";
|
|||
|
_left = left;
|
|||
|
_right = right;
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region//公共接口
|
|||
|
public void Load(string name, Action<bool> loadFinished)
|
|||
|
{
|
|||
|
if (_name != name)
|
|||
|
{
|
|||
|
//Debug.Log("UISutureTextureData:Load:" + name);
|
|||
|
UnLoad();
|
|||
|
_name = name;
|
|||
|
|
|||
|
_leftName = _name + "_l";
|
|||
|
_rightName = _name + "_r";
|
|||
|
|
|||
|
_onLoadFinished = loadFinished;
|
|||
|
if (!String.IsNullOrEmpty(_name))
|
|||
|
{
|
|||
|
if (!_useCustomLoad)
|
|||
|
{
|
|||
|
if (_isFromStreamFile)
|
|||
|
{
|
|||
|
OnLoadFromStreamFile();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
OnLoadFromResource();
|
|||
|
}
|
|||
|
OnLoadFinishedCallBack(true);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
OnCustomLoadImp();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void UnLoad()
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
if (!String.IsNullOrEmpty(_name))
|
|||
|
{
|
|||
|
if (!_useCustomLoad)
|
|||
|
{
|
|||
|
#if !UNITY_EDITOR
|
|||
|
if (_left != null)
|
|||
|
{
|
|||
|
GameObject.Destroy(_left);
|
|||
|
Resources.UnloadAsset(_left);
|
|||
|
}
|
|||
|
if (_right != null)
|
|||
|
{
|
|||
|
GameObject.Destroy(_right);
|
|||
|
Resources.UnloadAsset(_right);
|
|||
|
}
|
|||
|
#endif
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
OnCustomUnLoadImp();
|
|||
|
}
|
|||
|
|
|||
|
_left = null;
|
|||
|
_right = null;
|
|||
|
_name = null;
|
|||
|
_leftName = null;
|
|||
|
_rightName = null;
|
|||
|
_onLoadFinished = null;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void OnLoadFromResource()
|
|||
|
{
|
|||
|
_left = Resources.Load<Texture2D>(_leftName);
|
|||
|
_right = Resources.Load<Texture2D>(_rightName);
|
|||
|
}
|
|||
|
private void OnLoadFromStreamFile()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
AppManager.Instance.ReadBytesAsync(_leftName + _fileExt, bytes => {
|
|||
|
|
|||
|
Debug.Log("OnLoadFromStreamFile:" + _leftName + _fileExt + " Is Exist: " + (bytes != null).ToString());
|
|||
|
if (bytes != null)
|
|||
|
{
|
|||
|
_left = new Texture2D(256, 256, TextureFormat.RGBA32, false);
|
|||
|
_left.LoadImage(bytes);
|
|||
|
_left.wrapMode = TextureWrapMode.Clamp;
|
|||
|
_left.filterMode = FilterMode.Bilinear;
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
AppManager.Instance.ReadBytesAsync(_rightName + _fileExt, bytes => {
|
|||
|
Debug.Log("OnLoadFromStreamFile:" + _rightName + _fileExt + " Is Exist:" + (bytes != null).ToString());
|
|||
|
if (bytes != null)
|
|||
|
{
|
|||
|
_right = new Texture2D(256, 256, TextureFormat.RGBA32, false);
|
|||
|
_right.LoadImage(bytes);
|
|||
|
_right.wrapMode = TextureWrapMode.Clamp;
|
|||
|
_right.filterMode = FilterMode.Bilinear;
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
catch(Exception ex)
|
|||
|
{
|
|||
|
Debug.LogError("读取图片文件失败!" + _leftName + "::" + _rightName);
|
|||
|
Debug.LogException(ex);
|
|||
|
}
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
#region//子类继承处理 和调用
|
|||
|
protected virtual void OnCustomLoadImp()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
protected virtual void OnCustomUnLoadImp()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
protected void OnLoadFinishedCallBack(bool loaded)
|
|||
|
{
|
|||
|
if (_onLoadFinished != null) _onLoadFinished(loaded);
|
|||
|
}
|
|||
|
#endregion
|
|||
|
|
|||
|
}
|