void LoadingAsset(ResManager.Asset asset, System.Action <ResManager.Asset> onLoaded) { if (asset.url.LastIndexOf(".xml") > 0) { string xmlUrl = Application.streamingAssetsPath + "/" + asset.url; if (File.Exists(xmlUrl) && asset.type == ResManager.AssetType.Sprites) { string atlasPath = asset.url.Substring(0, asset.url.LastIndexOf(".xml")) + ".png"; string pngUrl = Application.streamingAssetsPath + "/" + atlasPath; LoadPng(pngUrl, asset, null); //load xml string config = File.ReadAllText(xmlUrl); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(config); XmlNode root = xmlDoc.SelectSingleNode("TextureAtlas"); XmlNodeList nodeList = root.ChildNodes; asset.sprites = new Dictionary <string, Sprite> (); //遍历所有子节点 foreach (XmlNode xn in nodeList) { if (!(xn is XmlElement)) { continue; } XmlElement xe = (XmlElement)xn; string fn = xe.GetAttribute("name").Replace('/', '_'); float x = float.Parse(xe.GetAttribute("x")); float y = float.Parse(xe.GetAttribute("y")); float w = float.Parse(xe.GetAttribute("width")); float h = float.Parse(xe.GetAttribute("height")); Sprite s = Sprite.Create(asset.texture, new Rect(x, asset.texture.height - h - y, w, h), Vector2.one * 0.5f, 100, 1, asset.meshType); s.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild; s.name = fn; asset.sprites [fn] = s; } if (onLoaded != null) { onLoaded(asset); } } } else { string url = Application.streamingAssetsPath + "/" + asset.url; LoadPng(url, asset, onLoaded); } }
/// <summary> /// 通过StreamingAssets下面的路径创建asset /// </summary> /// <returns>The asset.</returns> /// <param name="url">URL.</param> /// <param name="textureReadonly">texture Readonly.</param> public static ResManager.Asset CreateAsset(string url, bool textureReadonly = true) { ResManager.Asset asset = new ResManager.Asset(); asset.meshType = SpriteMeshType.FullRect; asset.warpMode = TextureWrapMode.Clamp; asset.path = ResManager.AssetPath.StreamingAssets; asset.textureReadonly = textureReadonly; asset.url = url; if (url.LastIndexOf(".xml") > 0) { asset.type = ResManager.AssetType.Sprites; } else { asset.type = ResManager.AssetType.Sprite; } return(asset); }
void LoadPng(string url, ResManager.Asset asset, System.Action <ResManager.Asset> onLoaded) { Texture2D texture = new Texture2D(2, 2, TextureFormat.RGBA32, false); texture.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild; byte[] bytes; if (File.Exists(url + ".txt")) { bytes = ResManager.ReverseBytes(File.ReadAllBytes(url + ".txt")); } else if (File.Exists(url)) { bytes = File.ReadAllBytes(url); } else { print("Load error : " + url); return; } texture.LoadImage(bytes, asset.textureReadonly); asset.texture = texture; asset.texture.name = url.Substring(url.LastIndexOf("/") + 1); if (asset.type == ResManager.AssetType.Sprite) { asset.texture.wrapMode = asset.warpMode; asset.sprite = Sprite.Create(asset.texture, new Rect(0f, 0f, asset.texture.width, asset.texture.height), Vector2.one * 0.5f, 100, 1, asset.meshType); asset.sprite.name = asset.texture.name; asset.sprite.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild; if (onLoaded != null) { onLoaded(asset); } } else if (asset.type == ResManager.AssetType.Texture2D) { asset.texture.wrapMode = asset.warpMode; if (onLoaded != null) { onLoaded(asset); } } }
public virtual void RemoveSprite(bool disposeAsset = false) { if (replaceMatTexture) { if (material) { if (restoreOnDisable) { material.SetTexture(nameId, prevTex); } } } else { if (m_Image) { m_Image.sprite = transparentSprite; #if UNITY_EDITOR if (!Application.isPlaying) { UnityEditor.EditorUtility.SetDirty(m_Image); } #endif } else if (GetComponent <SpriteRenderer>()) { SpriteRenderer sr = GetComponent <SpriteRenderer>(); sr.sprite = null; } } if (disposeAsset && _asset != null && ResManager.IsInited()) { #if !UNITY_EDITOR ResManager.Instance.DisposeAsset(_asset); #endif _asset = null; } }
/// <summary> /// 加载 /// </summary> public virtual void LoadSprite() { if (!string.IsNullOrEmpty(_url)) { if (Application.isPlaying) { ResManager.Asset asset = ResManager.Instance.GetAsset(url); if (asset == null) { asset = CreateAsset(_url, textureReadonly); ResManager.Instance.LoadAsset(asset, delegate(ResManager.Asset obj) { if (string.IsNullOrEmpty(_url)) { RemoveSprite(); } else if (asset.url.Equals(_url)) { _asset = obj; if (_asset != null && cacheAsset) { _asset.cached = cacheAsset; } ShowSprite(); onInited.Invoke(); } }); } else { _asset = asset; if (_asset != null && cacheAsset) { _asset.cached = cacheAsset; } ShowSprite(); onInited.Invoke(); } } else { #if UNITY_EDITOR ResManager.Asset asset = CreateAsset(url); LoadingAsset(asset, delegate(ResManager.Asset obj) { if (string.IsNullOrEmpty(_url)) { RemoveSprite(); } else if (asset.url.Equals(_url)) { _asset = obj; ShowSprite(); onInited.Invoke(); } }); #endif } } else { RemoveSprite(); } }