private string Generate(StoredAsset originalAsset) { using (var inputStream = originalAsset.FileStream) using (var outputStream = new MemoryStream()) using (var factory = new ImageFactory()) { factory.Load(inputStream) .Resize(new Size(0, 150)) .Format(new JpegFormat() { Quality = 70 }) .Save(outputStream); var outputBytes = new byte[outputStream.Length]; outputStream.Read(outputBytes, 0, (int)outputStream.Length); var thumbnail = _fileRepository.StoreAsset(originalAsset.Id, "thumbnail" + originalAsset.Extension, originalAsset.ContentType, outputBytes); return(thumbnail.StorageLocation); } }
internal static List <UnityEngine.Object> DeepCopyAsset(UnityEngine.Object original) { var originalStylesheet = original as UnityEngine.UIElements.StyleSheet; if (originalStylesheet == null) { return(new List <UnityEngine.Object>()); } var clonedStylesheet = ScriptableObject.Instantiate(originalStylesheet) as UnityEngine.UIElements.StyleSheet; var addedAssets = new Dictionary <UnityEngine.Object, List <UnityEngine.Object> >(); var newAssets = new List <UnityEngine.Object>(); var newScalableImages = new List <ScalableImage>(); // Clone assets for (int i = 0; i < clonedStylesheet.assets.Length; ++i) { var asset = clonedStylesheet.assets[i]; // The first cloned asset is the "main" asset that should be added to the stylesheet's list List <UnityEngine.Object> clonedAssets = null; if (!addedAssets.TryGetValue(asset, out clonedAssets)) { clonedAssets = CloneAsset(asset); if (clonedAssets.Count > 0) { addedAssets[asset] = clonedAssets; } } if (clonedAssets?.Count > 0) { newAssets.Add(clonedAssets[0]); } } // Clone scalable images for (int i = 0; i < clonedStylesheet.scalableImages.Length; ++i) { var si = clonedStylesheet.scalableImages[i]; List <UnityEngine.Object> clonedImages = null; if (!addedAssets.TryGetValue(si.normalImage, out clonedImages)) { var tex = CloneAsset(si.normalImage); var texHighRes = CloneAsset(si.highResolutionImage); if (tex.Count > 0 && texHighRes.Count > 0) { clonedImages = new List <UnityEngine.Object> { tex[0], texHighRes[0] }; addedAssets[si.normalImage] = clonedImages; } } if (clonedImages?.Count > 0) { newScalableImages.Add(new ScalableImage() { normalImage = clonedImages[0] as Texture2D, highResolutionImage = clonedImages[1] as Texture2D }); } } // Scan through resource paths and convert them to asset references var assetPaths = new Dictionary <string, StoredAsset>(); var scalableImagePaths = new Dictionary <string, StoredAsset>(); foreach (var rule in clonedStylesheet.rules) { foreach (var prop in rule.properties) { for (int valueIndex = 0; valueIndex < prop.values.Length; ++valueIndex) { var value = prop.values[valueIndex]; if (value.valueType != StyleValueType.ResourcePath) { continue; } var path = clonedStylesheet.strings[value.valueIndex]; bool isResource = false; int assetIndex = -1; bool isScalableImage = false; int scalableImageIndex = -1; StoredAsset sa; if (scalableImagePaths.TryGetValue(path, out sa)) { scalableImageIndex = sa.index; isScalableImage = true; } else if (assetPaths.TryGetValue(path, out sa)) { assetIndex = sa.index; isResource = true; } else { var asset = LoadResource(path); var clonedAssets = CloneAsset(asset); addedAssets[asset] = clonedAssets; if (asset is Texture2D) { // Try to load the @2x version var highResPath = Path.Combine( Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path) + "@2x" + Path.GetExtension(path)); var highResTex = LoadResource(highResPath); if (highResTex != null) { scalableImageIndex = newScalableImages.Count; var highResClones = CloneAsset(highResTex); newScalableImages.Add(new ScalableImage() { normalImage = clonedAssets[0] as Texture2D, highResolutionImage = highResClones[0] as Texture2D }); scalableImagePaths[path] = new StoredAsset() { si = newScalableImages[newScalableImages.Count - 1], index = scalableImageIndex }; clonedAssets.Add(highResClones[0]); addedAssets[asset] = clonedAssets; isScalableImage = true; } } if (!isScalableImage && clonedAssets.Count > 0) { assetIndex = newAssets.Count; newAssets.AddRange(clonedAssets); Object resource = clonedAssets[0]; assetPaths[path] = new StoredAsset() { resource = resource, index = assetIndex }; isResource = true; } } if (isResource) { value.valueType = StyleValueType.AssetReference; value.valueIndex = assetIndex; prop.values[valueIndex] = value; } else if (isScalableImage) { value.valueType = StyleValueType.ScalableImage; value.valueIndex = scalableImageIndex; prop.values[valueIndex] = value; } else { Debug.LogError("ResourcePath was not converted to AssetReference when converting stylesheet : " + path); } } } } clonedStylesheet.assets = newAssets.ToArray(); clonedStylesheet.scalableImages = newScalableImages.ToArray(); // Store all added assets in a hashset to avoid duplicates var cleanAssets = new HashSet <UnityEngine.Object>(); foreach (var assets in addedAssets.Values) { foreach (var a in assets) { cleanAssets.Add(a); } } // The cloned stylesheet should be the first item in the list, since it is the "main" asset var result = cleanAssets.ToList(); result.Insert(0, clonedStylesheet); return(result); }