public static UITextureAtlas CreateTextureAtlas( string textureFile, string atlasName, Material baseMaterial, int spriteWidth, int spriteHeight, string[] spriteNames) { Logger.Debug("Processing, textureFile: " + textureFile + ", atlasName: " + atlasName + ", width: " + (object)spriteWidth + ", height: " + (object)spriteHeight + ", baseMaterial" + (object)baseMaterial); int width = spriteWidth * spriteNames.Length; int height = spriteHeight; Texture2D texture = UIUtils.createTexture(textureFile, width, height); UITextureAtlas instance = (UITextureAtlas)ScriptableObject.CreateInstance <UITextureAtlas>(); Material material = (Material)Object.Instantiate <Material>((M0)baseMaterial); material.set_mainTexture((Texture)texture); instance.set_material(material); ((Object)instance).set_name(atlasName); for (int index = 0; index < spriteNames.Length; ++index) { float num = 1f / (float)spriteNames.Length; UITextureAtlas.SpriteInfo spriteInfo1 = new UITextureAtlas.SpriteInfo(); spriteInfo1.set_name(spriteNames[index]); spriteInfo1.set_texture(texture); spriteInfo1.set_region(new Rect((float)index * num, 0.0f, num, 1f)); UITextureAtlas.SpriteInfo spriteInfo2 = spriteInfo1; instance.AddSprite(spriteInfo2); } return(instance); }
public static bool SetThumbnails(string name, SpriteTextureInfo info, UITextureAtlas atlas, string[] states = null) { if (atlas == null || atlas.texture == null) return false; Texture2D atlasTex = atlas.texture; float atlasWidth = atlasTex.width; float atlasHeight = atlasTex.height; float rectWidth = info.width / atlasWidth; float rectHeight = info.height / atlasHeight; int x = info.startX; int y = info.startY; if (states == null) states = new string[] { "" }; for (int i = 0; i < states.Length; i++, x += info.width) { if (x < 0 || x + info.width > atlasWidth || y < 0 || y > atlasHeight) continue; Texture2D spriteTex = new Texture2D(info.width, info.height); spriteTex.SetPixels(atlasTex.GetPixels(x, y, info.width, info.height)); UITextureAtlas.SpriteInfo sprite = new UITextureAtlas.SpriteInfo() { name = name + states[i], region = new Rect(x / atlasWidth, y / atlasHeight, rectWidth, rectHeight), texture = spriteTex }; atlas.AddSprite(sprite); } return true; }
/// <summary> /// Create a new atlas. /// </summary> public static UITextureAtlas CreateTextureAtlas(string atlasName, Texture2D [] textures) { const int maxSize = 2048; Texture2D texture2D = new Texture2D(maxSize, maxSize, TextureFormat.ARGB32, false); Rect[] regions = texture2D.PackTextures(textures, 2, maxSize); Material material = Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); material.mainTexture = texture2D; UITextureAtlas textureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); textureAtlas.material = material; textureAtlas.name = atlasName; for (int i = 0; i < textures.Length; i++) { UITextureAtlas.SpriteInfo item = new UITextureAtlas.SpriteInfo { name = textures[i].name, texture = textures[i], region = regions[i], }; textureAtlas.AddSprite(item); } return(textureAtlas); }
public static UITextureAtlas CreateTextureAtlas(Texture2D texture2D, string atlasName, string[] spriteNames) { UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Assert(atlas, "uitextureAtlas"); Material material = Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); Assert(material, "material"); material.mainTexture = texture2D.TryMakeReadable(); atlas.material = material; atlas.name = atlasName; int n = spriteNames.Length; for (int i = 0; i < n; i++) { float num = 1f / (float)spriteNames.Length; UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = texture2D, region = new Rect(i * num, 0f, num, 1f) }; atlas.AddSprite(spriteInfo); } return(atlas); }
UITextureAtlas CreateAtlas(string atlasName, string[] spriteNames, Texture2D[] spriteTextures) { Texture2D atlasTex = new Texture2D(1024, 1024, TextureFormat.ARGB32, false); Texture2D[] textures = spriteTextures; Rect[] rects = new Rect[1]; rects = atlasTex.PackTextures(textures, 2, 1024); UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Material material = (Material)Material.Instantiate(UIView.GetAView().defaultAtlas.material); material.mainTexture = atlasTex; atlas.material = material; atlas.name = atlasName; for (int i = 0; i < rects.Length; i++) { if (spriteNames[i] != null && textures[i] != null) { UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo() { name = spriteNames[i], texture = textures[i], region = rects[i] }; atlas.AddSprite(spriteInfo); } } return(atlas); }
UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, Material baseMaterial, int spriteWidth, int spriteHeight, string[] spriteNames) { Texture2D tex = new Texture2D(spriteWidth * spriteNames.Length, spriteHeight, TextureFormat.ARGB32, false) { filterMode = FilterMode.Bilinear }; { // LoadTexture tex.LoadImage(ResourceLoader.loadResourceData(textureFile)); tex.Apply(true, true); } UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); { // Setup atlas Material material = (Material)Material.Instantiate(baseMaterial); material.mainTexture = tex; atlas.material = material; atlas.name = atlasName; } // Add sprites for (int i = 0; i < spriteNames.Length; ++i) { float uw = 1.0f / spriteNames.Length; var spriteInfo = new UITextureAtlas.SpriteInfo() { name = spriteNames[i], texture = tex, region = new Rect(i * uw, 0, uw, 1), }; atlas.AddSprite(spriteInfo); } return(atlas); }
/// <summary> /// Creates a new sprite using the size of the image inside the atlas. /// </summary> /// <param name="dimensions">The location and size of the sprite within the atlas (in pixels).</param> /// <param name="spriteName">The name of the sprite to create</param> /// <param name="atlasName">The name of the atlas to add the sprite to.</param> /// <returns></returns> public static bool AddSpriteToAtlas(Rect dimensions, string spriteName, string atlasName) { bool returnValue = false; if (m_atlasStore.ContainsKey(atlasName)) { UITextureAtlas foundAtlas = m_atlasStore[atlasName]; Texture2D atlasTexture = foundAtlas.texture; Vector2 atlasSize = new Vector2(atlasTexture.width, atlasTexture.height); Rect relativeLocation = new Rect(new Vector2(dimensions.position.x / atlasSize.x, dimensions.position.y / atlasSize.y), new Vector2(dimensions.width / atlasSize.x, dimensions.height / atlasSize.y)); Texture2D spriteTexture = new Texture2D((int)Math.Round(dimensions.width), (int)Math.Round(dimensions.height)); spriteTexture.SetPixels(atlasTexture.GetPixels((int)dimensions.position.x, (int)dimensions.position.y, (int)dimensions.width, (int)dimensions.height)); UITextureAtlas.SpriteInfo createdSprite = new UITextureAtlas.SpriteInfo() { name = spriteName, region = relativeLocation, texture = spriteTexture }; foundAtlas.AddSprite(createdSprite); returnValue = true; } return(returnValue); }
UITextureAtlas CreateMyAtlas(string AtlasName, Material BaseMat, string[] sPritesNames) { var size = 1024; Texture2D atlasTex = new Texture2D(size, size, TextureFormat.ARGB32, false); Texture2D[] textures = new Texture2D[sPritesNames.Length]; Rect[] rects = new Rect[sPritesNames.Length]; for (int i = 0; i < sPritesNames.Length; i++) { textures[i] = ResourceLoader.loadTexture(0, 0, sPritesNames[i] + ".png"); } rects = atlasTex.PackTextures(textures, 2, size); UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Material material = Material.Instantiate(BaseMat); material.mainTexture = atlasTex; atlas.material = material; atlas.name = AtlasName; for (int i = 0; i < sPritesNames.Length; i++) { var spriteInfo = new UITextureAtlas.SpriteInfo() { name = sPritesNames[i], texture = atlasTex, region = rects[i] }; atlas.AddSprite(spriteInfo); } return(atlas); }
public static UITextureAtlas CreateAtlas(string atlasName, string[] spriteNames, Texture2D[] sprites) { UITextureAtlas textureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Texture2D texture2D = new Texture2D(0, 0, TextureFormat.ARGB32, false); int maxSize = 4096; Rect[] regions = texture2D.PackTextures(sprites, 4, maxSize); Shader shader = Shader.Find("UI/Default UI Shader"); Material material = new Material(shader); material.mainTexture = texture2D; textureAtlas.material = material; textureAtlas.name = atlasName; for (int i = 0; i < spriteNames.Length; i++) { UITextureAtlas.SpriteInfo item = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = sprites[i], region = regions[i], }; textureAtlas.AddSprite(item); } return(textureAtlas); }
public static UITextureAtlas CreateAtlas(Texture2D[] sprites) { UITextureAtlas atlas = new UITextureAtlas(); atlas.material = new Material(GetUIAtlasShader()); Texture2D texture = new Texture2D(0, 0); Rect[] rects = texture.PackTextures(sprites, 0); for (int i = 0; i < rects.Length; ++i) { Texture2D sprite = sprites[i]; Rect rect = rects[i]; UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo(); spriteInfo.name = sprite.name; spriteInfo.texture = sprite; spriteInfo.region = rect; spriteInfo.border = new RectOffset(); atlas.AddSprite(spriteInfo); } atlas.material.mainTexture = texture; return(atlas); }
private static UITextureAtlas CreateTextureAtlas() { Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false); Texture2D[] textures = new Texture2D[] { Texture2D.blackTexture, Texture2D.whiteTexture }; Rect[] regions = texture2D.PackTextures(textures, 0, 10); Material material = Object.Instantiate(UIView.GetAView().defaultAtlas.material); material.mainTexture = texture2D; UITextureAtlas textureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); textureAtlas.material = material; textureAtlas.name = "MCSI"; for (int i = 0; i < textures.Length; i++) { textureAtlas.AddSprite(new UITextureAtlas.SpriteInfo { name = i.ToString(), texture = textures[i], region = regions[i], }); } return(textureAtlas); }
public static UITextureAtlas CreateTextureAtlas(string atlasName, string filename, FileDirectory dir, string[] spriteNames, int rows, int cols) { Texture2D texture2D = LoadTextureFromFile(GetFilePath(dir, filename + ".png")); Debug.Print(texture2D.width, texture2D.height); UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Material material = UnityEngine.Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); material.mainTexture = texture2D; atlas.material = material; atlas.name = atlasName; float spriteWidth = 1.0f / cols; float spriteHeight = 1.0f / rows; for (int i = 0; i < spriteNames.Length; i++) { float x = (i % cols) / (float)cols; float y = (1.0f - spriteHeight) - ((i / cols) / (float)rows); atlas.AddSprite(new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = texture2D, region = new Rect(x, y, spriteWidth, spriteHeight) }); } return(atlas); }
public static UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, int spriteWidth, int spriteHeight, string[] spriteNames) { Texture2D texture2D = LoadTextureFromAssembly( textureFile, spriteWidth * spriteNames.Length, spriteHeight); UITextureAtlas uitextureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); //Assert(uitextureAtlas != null, "uitextureAtlas"); Material material = Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); //Assert(material != null, "material"); material.mainTexture = texture2D; uitextureAtlas.material = material; uitextureAtlas.name = atlasName; int num2; for (int i = 0; i < spriteNames.Length; i = num2) { float num = 1f / (float)spriteNames.Length; UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = texture2D, region = new Rect((float)i * num, 0f, num, 1f) }; uitextureAtlas.AddSprite(spriteInfo); num2 = i + 1; } return(uitextureAtlas); }
public static UITextureAtlas GenerateLinearAtlas(string name, Texture2D texture, int numSprites, string[] spriteNames) { UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); atlas.padding = 0; atlas.name = name; var shader = Shader.Find("UI/Default UI Shader"); if (shader != null) { atlas.material = new Material(shader); } atlas.material.mainTexture = texture; float spriteWidth = (float)texture.width / (float)numSprites; for (int i = 0; i < numSprites; ++i) { float x = (float)i / (float)numSprites; var sprite = new UITextureAtlas.SpriteInfo { name = spriteNames[i], region = new Rect(x, 0f, spriteWidth / (float)texture.width, 1f) }; var spriteTexture = new Texture2D((int)spriteWidth, texture.height); spriteTexture.SetPixels(texture.GetPixels((int)((float)texture.width * sprite.region.x), (int)((float)texture.height * sprite.region.y), (int)spriteWidth, texture.height)); sprite.texture = spriteTexture; atlas.AddSprite(sprite); } return(atlas); }
public static UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, Material baseMaterial, int spriteWidth, int spriteHeight, string[] spriteNames) { Texture2D texture2D = new Texture2D(spriteWidth * spriteNames.Length, spriteHeight, TextureFormat.ARGB32, false); texture2D.filterMode = FilterMode.Bilinear; Assembly executingAssembly = Assembly.GetExecutingAssembly(); Stream manifestResourceStream = executingAssembly.GetManifestResourceStream("RoundaboutBuilder.Resources." + textureFile); byte[] array = new byte[manifestResourceStream.Length]; manifestResourceStream.Read(array, 0, array.Length); texture2D.LoadImage(array); texture2D.Apply(true, true); UITextureAtlas uitextureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Material material = UnityEngine.Object.Instantiate <Material>(baseMaterial); material.mainTexture = texture2D; uitextureAtlas.material = material; uitextureAtlas.name = atlasName; int num2; for (int i = 0; i < spriteNames.Length; i = num2) { float num = 1f / (float)spriteNames.Length; UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = texture2D, region = new Rect((float)i * num, 0f, num, 1f) }; uitextureAtlas.AddSprite(spriteInfo); num2 = i + 1; } return(uitextureAtlas); }
private Rect CopySpriteToAtlas(UITextureAtlas atlas, int x, int y, string name, Texture2D texture) { var atlasTexture = atlas.material.mainTexture as Texture2D; for (int _x = 0; _x < texture.width; _x++) { for (int _y = 0; _y < texture.height; _y++) { atlasTexture.SetPixel(x + _x, y + _y, texture.GetPixel(_x, _y)); } } float u = (float)x / atlasTexture.width; float v = (float)y / atlasTexture.height; float s = (float)(texture.width) / atlasTexture.width; float t = (float)(texture.height) / atlasTexture.height; var sprite = new UITextureAtlas.SpriteInfo(); sprite.region = new Rect(u, v, s, t); sprite.name = name; sprite.texture = texture; atlas.AddSprite(sprite); return(sprite.region); }
public static UITextureAtlas CreateAtlas(string[] names) { Texture2D[] sprites = ResourceUtils.Load <Texture2D>(names); UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); atlas.material = new Material(ResourceUtils.GetUIAtlasShader()); Texture2D texture = new Texture2D(0, 0); Rect[] rects = texture.PackTextures(sprites, 0); for (int i = 0; i < rects.Length; ++i) { Texture2D sprite = sprites[i]; Rect rect = rects[i]; UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo(); spriteInfo.name = sprite.name; spriteInfo.texture = sprite; spriteInfo.region = rect; spriteInfo.border = new RectOffset(); atlas.AddSprite(spriteInfo); } atlas.material.mainTexture = texture; return(atlas); }
public static UITextureAtlas CreateTextureAtlas(string atlasName, string[] spriteNames, FileDirectory dir) { int maxSize = 1024; Texture2D texture2D = new Texture2D(maxSize, maxSize, TextureFormat.ARGB32, false); Texture2D[] textures = new Texture2D[spriteNames.Length]; Rect[] regions = new Rect[spriteNames.Length]; for (int i = 0; i < spriteNames.Length; i++) { textures[i] = TextureLoader.LoadTextureFromFile(GetFilePath(dir, spriteNames[i] + ".png")); } regions = texture2D.PackTextures(textures, 2, maxSize); UITextureAtlas textureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Material material = UnityEngine.Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); material.mainTexture = texture2D; textureAtlas.material = material; textureAtlas.name = atlasName; for (int i = 0; i < spriteNames.Length; i++) { UITextureAtlas.SpriteInfo item = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = textures[i], region = regions[i], }; textureAtlas.AddSprite(item); } return(textureAtlas); }
private void CreateAtlas() { UITextureAtlas textureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Texture2D[] textures = new Texture2D[_spriteNames.Length]; Texture2D texture2D = new Texture2D(1, 1, TextureFormat.ARGB32, false); for (int i = 0; i < _spriteNames.Length; i++) { textures[i] = GetTextureFromAssemblyManifest(_spriteNames[i] + ".png"); } int maxSize = 1024; Rect[] regions = texture2D.PackTextures(textures, 2, maxSize); Material material = Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); material.mainTexture = texture2D; textureAtlas.material = material; textureAtlas.name = "NetworkSkinsAtlas"; for (int i = 0; i < _spriteNames.Length; i++) { UITextureAtlas.SpriteInfo item = new UITextureAtlas.SpriteInfo { name = _spriteNames[i], texture = textures[i], region = regions[i], }; textureAtlas.AddSprite(item); } UITextureAtlas = textureAtlas; }
public static UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, int spriteWidth, int spriteHeight, string[] spriteNames, RectOffset border = null, int space = 0) { Texture2D texture2D = LoadTextureFromAssembly(textureFile, spriteWidth * spriteNames.Length + space * (spriteNames.Length + 1), spriteHeight + 2 * space); UITextureAtlas uitextureAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); Material material = Object.Instantiate(UIView.GetAView().defaultAtlas.material); material.mainTexture = texture2D; uitextureAtlas.material = material; uitextureAtlas.name = atlasName; var heightRatio = spriteHeight / (float)texture2D.height; var widthRatio = spriteWidth / (float)texture2D.width; var spaceHeightRatio = space / (float)texture2D.height; var spaceWidthRatio = space / (float)texture2D.width; for (int i = 0; i < spriteNames.Length; i += 1) { UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = texture2D, region = new Rect(i * widthRatio + (i + 1) * spaceWidthRatio, spaceHeightRatio, widthRatio, heightRatio), border = border ?? new RectOffset() }; uitextureAtlas.AddSprite(spriteInfo); } return(uitextureAtlas); }
/// <summary> /// Loads a four-sprite texture atlas from a given .png file. /// </summary> /// <param name="atlasName">Atlas name (".png" will be appended fto make the filename)</param> /// <returns>New texture atlas</returns> internal static UITextureAtlas LoadSpriteAtlas(string atlasName) { // Create new texture atlas for button. UITextureAtlas newAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); newAtlas.name = atlasName; newAtlas.material = UnityEngine.Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); // Load texture from file. Texture2D newTexture = LoadTexture(atlasName + ".png"); newAtlas.material.mainTexture = newTexture; // Setup sprites. string[] spriteNames = new string[] { "disabled", "normal", "pressed", "hovered" }; int numSprites = spriteNames.Length; float spriteWidth = 1f / spriteNames.Length; // Iterate through each sprite (counter increment is in region setup). for (int i = 0; i < numSprites; ++i) { UITextureAtlas.SpriteInfo sprite = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = newTexture, // Sprite regions are horizontally arranged, evenly spaced. region = new Rect(i * spriteWidth, 0f, spriteWidth, 1f) }; newAtlas.AddSprite(sprite); } return(newAtlas); }
private static UITextureAtlas PackTextures(string atlasName, IntVector2 atlasSizeHint, List<Texture2D> loadedTextures, List<string> loadedSpriteNames) { Texture2D texture2D = new Texture2D( width: atlasSizeHint.x, height: atlasSizeHint.y, format: TextureFormat.ARGB32, mipmap: false); Rect[] regions = texture2D.PackTextures( textures: loadedTextures.ToArray(), padding: 2, maximumAtlasSize: Math.Max(atlasSizeHint.x, atlasSizeHint.y)); // Now using loaded and packed textures, create the atlas with sprites UITextureAtlas newAtlas = ScriptableObject.CreateInstance<UITextureAtlas>(); var uiView = UIView.GetAView(); Material material = UnityEngine.Object.Instantiate(uiView.defaultAtlas.material); material.mainTexture = texture2D; newAtlas.material = material; newAtlas.name = atlasName; for (int i = 0; i < loadedTextures.Count; i++) { var item = new UITextureAtlas.SpriteInfo { name = loadedSpriteNames[i], texture = loadedTextures[i], region = regions[i], }; newAtlas.AddSprite(item); } return newAtlas; }
public static void AddTextureSpriteInfo(UITextureAtlas.SpriteInfo spriteInfo) { if (spriteInfo == null || spriteInfo.name == "" || spriteInfo.texture == null || (m_atlas[spriteInfo.name] != null)) { return; } m_atlas.AddSprite(spriteInfo); }
/*********** custom icons *************/ public static UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, Material baseMaterial, int spriteWidth, int spriteHeight, int spriteCountHor) { Texture2D tex = new Texture2D(spriteWidth * spriteCountHor, spriteHeight, TextureFormat.ARGB32, false); tex.filterMode = FilterMode.Bilinear; try { // LoadTexture System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); System.IO.Stream textureStream = assembly.GetManifestResourceStream("ResilientOwners." + textureFile); byte[] buf = new byte[textureStream.Length]; //declare arraysize textureStream.Read(buf, 0, buf.Length); // read from stream to byte array tex.LoadImage(buf); tex.Apply(true, true); } catch { CODebug.Log(LogChannel.Modding, "error opening texture file"); } UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); try { // Setup atlas Material material = (Material)Material.Instantiate(baseMaterial); material.mainTexture = tex; atlas.material = material; atlas.name = atlasName; } catch { CODebug.Log(LogChannel.Modding, "error setting texture"); } // Add sprites for (int i = 0; i < spriteCountHor; ++i) { float uw = 1.0f / spriteCountHor; var spriteInfo = new UITextureAtlas.SpriteInfo() { name = atlasName + "_" + i, texture = tex, region = new Rect(i * uw, 0, uw, 1), }; atlas.AddSprite(spriteInfo); } return(atlas); }
static void AddSprite(string name, Rect region, RectOffset border = null) { UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo { name = name, texture = Atlas.material.mainTexture as Texture2D, region = region, border = border ?? new RectOffset() }; Atlas.AddSprite(spriteInfo); }
// Generates a texture atlas for a standard prefab button from a single image file. public static UITextureAtlas GenerateAtlas(string name, Texture2D texture) { // Generate Sprite Names string[] spriteStates = new[] { "", "Disabled", "Focused", "Hovered", "Pressed" }; string[] spriteNames = new string[5]; for (int i = 0; i < spriteNames.Length; i++) { spriteNames[i] = name + spriteStates[i].ToString(); } // Create a new texture atlas UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); atlas.padding = 0; atlas.name = name; // Shader assignment var shader = Shader.Find("UI/Default UI Shader"); if (shader != null) { atlas.material = new Material(shader); } atlas.material.mainTexture = texture; // Assign regions and names for (int i = 0; i < 5; i++) { float y = 1; float x = (float)i / (float)5; // Create a new sprite UITextureAtlas.SpriteInfo sprite = new UITextureAtlas.SpriteInfo { name = spriteNames[i], region = new Rect(x, y, 0.2f, 1.0f) }; // Define sprite size and offsets int spriteWidth = 109; int spriteHeight = 100; int spriteOffsetY = 0; int spriteOffsetX = (int)((float)texture.width * sprite.region.x); // Create and fill a new texture Texture2D spriteTexture = new Texture2D(spriteWidth, spriteHeight); spriteTexture.SetPixels(texture.GetPixels(spriteOffsetX, spriteOffsetY, spriteWidth, spriteHeight)); sprite.texture = spriteTexture; // Add the finished sprite to the atlas atlas.AddSprite(sprite); } return(atlas); }
/// <summary> /// Creates a new <see cref="UITextureAtlas"/> from a PNG file. /// </summary> /// <param name="fileName">The PNG file name of the texture.</param> /// <param name="atlasName">The name of the <see cref="UITextureAtlas"/>.</param> /// <param name="shaderName">The name of the <see cref="Shader"/>.</param> /// <param name="spriteSize">The size of each sprite.</param> /// <param name="spriteGrid">The size of the sprite grid.</param> /// <param name="spriteNames">The names of the sprites.</param> /// <returns>The generated <see cref="UITextureAtlas"/>.</returns> public static UITextureAtlas CreateAtlas(string fileName, string atlasName, string shaderName, Vector2 spriteSize, Vector2 spriteGrid, string[][] spriteNames) { Shader shader = Shader.Find(shaderName); if (shader == null) { return(null); } // Setup Texture2D texture = new Texture2D((int)(spriteSize.x * spriteGrid.x), (int)(spriteSize.y * spriteGrid.y), TextureFormat.ARGB32, false); byte[] textureBytes = File.ReadAllBytes(fileName); texture.LoadImage(textureBytes); FixTransparency(texture); Material material = new Material(shader); material.mainTexture = texture; UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); atlas.name = atlasName; atlas.material = material; // Sprites for (int y = 0; y < spriteGrid.y; y++) { for (int x = 0; x < spriteGrid.x; x++) { if (y < spriteNames.Length && x < spriteNames[y].Length && !string.IsNullOrEmpty(spriteNames[y][x])) { Rect spriteRect = new Rect( x * spriteSize.x / texture.width, 1 - ((y + 1) * spriteSize.y / texture.height), spriteSize.x / texture.width, spriteSize.y / texture.height ); UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo() { name = spriteNames[y][x], texture = texture, region = spriteRect }; atlas.AddSprite(spriteInfo); } } } return(atlas); }
private static UITextureAtlas CreateTextureAtlas(string textureFile, string atlasName, Material baseMaterial, int spriteWidth, int spriteHeight, string[] spriteNames) { Texture2D texture = new Texture2D(spriteWidth * spriteNames.Length, spriteHeight, TextureFormat.ARGB32, false); texture.filterMode = FilterMode.Bilinear; { // LoadTexture System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); Stream textureStream = assembly.GetManifestResourceStream(textureFile); if (textureStream == null) { DebugLog("Failed loading image!!"); return(null); } byte[] buf = new byte[textureStream.Length]; //declare arraysize textureStream.Read(buf, 0, buf.Length); // read from stream to byte array texture.LoadImage(buf); texture.Apply(true, true); } UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); { // Setup atlas Material material = (Material)Material.Instantiate(baseMaterial); material.mainTexture = texture; atlas.material = material; atlas.name = atlasName; } // Add sprites for (int i = 0; i < spriteNames.Length; ++i) { float uw = 1.0f / spriteNames.Length; var spriteInfo = new UITextureAtlas.SpriteInfo() { name = spriteNames[i], texture = texture, region = new Rect(i * uw, 0, uw, 1), }; atlas.AddSprite(spriteInfo); } return(atlas); }
public static UITextureAtlas Generate2DAtlas(string name, Texture2D texture, int numX, int numY, string[] spriteNames) { if (spriteNames.Length != numX * numY) { throw new ArgumentException($"Number of sprite name does not match dimensions (expected {numX} x {numY}, was {spriteNames.Length})"); } UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); atlas.padding = 0; atlas.name = name; var shader = Shader.Find("UI/Default UI Shader"); if (shader != null) { atlas.material = new Material(shader); } atlas.material.mainTexture = texture; int spriteWidth = Mathf.RoundToInt((float)texture.width / (float)numX); int spriteHeight = Mathf.RoundToInt((float)texture.height / (float)numY); int k = 0; for (int i = 0; i < numX; ++i) { float x = (float)i / (float)numX; for (int j = 0; j < numY; ++j) { float y = (float)j / (float)numY; var sprite = new UITextureAtlas.SpriteInfo { name = spriteNames[k], region = new Rect(x, y, (float)spriteWidth / (float)texture.width, (float)spriteHeight / (float)texture.height) }; var spriteTexture = new Texture2D(spriteWidth, spriteHeight); spriteTexture.SetPixels(texture.GetPixels((int)((float)texture.width * sprite.region.x), (int)((float)texture.height * sprite.region.y), spriteWidth, spriteHeight)); sprite.texture = spriteTexture; atlas.AddSprite(sprite); ++k; } } return(atlas); }
public static UITextureAtlas CreateTextureAtlas(string modName, string textureFile, string atlasName, int spriteWidth, int spriteHeight, string[] spriteNames) { Shader shader = Shader.Find("UI/Default UI Shader"); if (shader == null) { throw new Exception("shader null"); } Material atlasMaterial = new Material(shader); if (atlasMaterial == null) { throw new Exception("atlasMaterial null"); } byte[] bytes; bytes = File.ReadAllBytes(Path.Combine(FindModPath(), textureFile)); Texture2D tex = new Texture2D(spriteWidth * spriteNames.Length, spriteHeight, TextureFormat.ARGB32, false); tex.LoadImage(bytes); FixTransparency(tex); UITextureAtlas atlas = ScriptableObject.CreateInstance <UITextureAtlas>(); { // Setup atlas Material material = (Material)Material.Instantiate(atlasMaterial); material.mainTexture = tex; atlas.material = material; atlas.name = atlasName; } // Add sprites for (int i = 0; i < spriteNames.Length; ++i) { float uw = 1.0f / spriteNames.Length; var spriteInfo = new UITextureAtlas.SpriteInfo() { name = spriteNames[i], texture = tex, region = new Rect(i * uw, 0, uw, 1), }; atlas.AddSprite(spriteInfo); } return(atlas); }
/// <summary> /// Loads a four-sprite texture atlas from a given .png file. /// </summary> /// <param name="atlasName">Atlas name (".png" will be appended fto make the filename)</param> /// <returns>New texture atlas</returns> internal static UITextureAtlas LoadSpriteAtlas(string atlasName) { try { // Check if we've already cached this atlas. if (textureCache.ContainsKey(atlasName)) { // Cached - return cached result. return(textureCache[atlasName]); } // Create new texture atlas for button. UITextureAtlas newAtlas = ScriptableObject.CreateInstance <UITextureAtlas>(); newAtlas.name = atlasName; newAtlas.material = UnityEngine.Object.Instantiate <Material>(UIView.GetAView().defaultAtlas.material); // Load texture from file. Texture2D newTexture = LoadTexture(atlasName + ".png"); newAtlas.material.mainTexture = newTexture; // Setup sprites. string[] spriteNames = new string[] { "disabled", "normal", "pressed", "hovered" }; int numSprites = spriteNames.Length; float spriteWidth = 1f / spriteNames.Length; // Iterate through each sprite (counter increment is in region setup). for (int i = 0; i < numSprites; ++i) { UITextureAtlas.SpriteInfo sprite = new UITextureAtlas.SpriteInfo { name = spriteNames[i], texture = newTexture, // Sprite regions are horizontally arranged, evenly spaced. region = new Rect(i * spriteWidth, 0f, spriteWidth, 1f) }; newAtlas.AddSprite(sprite); } // Add to cache and return. textureCache.Add(atlasName, newAtlas); return(newAtlas); } catch (Exception e) { Logging.LogException(e, "exception loading texture atlas from file ", atlasName); return(null); } }
public static void CopySprite(string originalName, string newName, UITextureAtlas destAtlas) { try { var spriteInfo = UIView.GetAView().defaultAtlas[originalName]; destAtlas.AddSprite(new UITextureAtlas.SpriteInfo { border = spriteInfo.border, name = newName, region = spriteInfo.region, texture = spriteInfo.texture }); } catch (Exception e) { Debug.LogException(e); } }
public static UITextureAtlas CreateAtlas(Texture2D[] sprites) { UITextureAtlas atlas = new UITextureAtlas(); atlas.material = new Material(GetUIAtlasShader()); Texture2D texture = new Texture2D(0, 0); Rect[] rects = texture.PackTextures(sprites, 0); for (int i = 0; i < rects.Length; ++i) { Texture2D sprite = sprites[i]; Rect rect = rects[i]; UITextureAtlas.SpriteInfo spriteInfo = new UITextureAtlas.SpriteInfo(); spriteInfo.name = sprite.name; spriteInfo.texture = sprite; spriteInfo.region = rect; spriteInfo.border = new RectOffset(); atlas.AddSprite(spriteInfo); } atlas.material.mainTexture = texture; return atlas; }
private static Rect CopySpriteToAtlas(UITextureAtlas atlas, int x, int y, string name, Texture2D texture) { var atlasTexture = atlas.material.mainTexture as Texture2D; for (int _x = 0; _x < texture.width; _x++) { for (int _y = 0; _y < texture.height; _y++) { if (atlasTexture != null) atlasTexture.SetPixel(x + _x, y + _y, texture.GetPixel(_x, _y)); } } if (atlasTexture != null) { float u = (float)x / atlasTexture.width; float v = (float)y / atlasTexture.height; float s = (float)(texture.width) / atlasTexture.width; float t = (float)(texture.height) / atlasTexture.height; var sprite = new UITextureAtlas.SpriteInfo {region = new Rect(u, v, s, t), name = name, texture = texture}; atlas.AddSprite(sprite); return sprite.region; } return new Rect(); }