Esempio n. 1
0
		private void Load (TextReader reader, String imagesDir, TextureLoader textureLoader) {
			if (textureLoader == null) throw new ArgumentNullException("textureLoader cannot be null.");
			this.textureLoader = textureLoader;

			String[] tuple = new String[4];
			AtlasPage page = null;
			while (true) {
				String line = reader.ReadLine();
				if (line == null) break;
				if (line.Trim().Length == 0)
					page = null;
				else if (page == null) {
					page = new AtlasPage();
					page.name = line;

					if (readTuple(reader, tuple) == 2) { // size is only optional for an atlas packed with an old TexturePacker.
						page.width = int.Parse(tuple[0]);
						page.height = int.Parse(tuple[1]);
						readTuple(reader, tuple);
					}
					page.format = (Format)Enum.Parse(typeof(Format), tuple[0], false);

					readTuple(reader, tuple);
					page.minFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0], false);
					page.magFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1], false);

					String direction = readValue(reader);
					page.uWrap = TextureWrap.ClampToEdge;
					page.vWrap = TextureWrap.ClampToEdge;
					if (direction == "x")
						page.uWrap = TextureWrap.Repeat;
					else if (direction == "y")
						page.vWrap = TextureWrap.Repeat;
					else if (direction == "xy")
						page.uWrap = page.vWrap = TextureWrap.Repeat;

					textureLoader.Load(page, Path.Combine(imagesDir, line));

					pages.Add(page);

				} else {
					AtlasRegion region = new AtlasRegion();
					region.name = line;
					region.page = page;

					region.rotate = Boolean.Parse(readValue(reader));

					readTuple(reader, tuple);
					int x = int.Parse(tuple[0]);
					int y = int.Parse(tuple[1]);

					readTuple(reader, tuple);
					int width = int.Parse(tuple[0]);
					int height = int.Parse(tuple[1]);

					region.u = x / (float)page.width;
					region.v = y / (float)page.height;
					if (region.rotate) {
						region.u2 = (x + height) / (float)page.width;
						region.v2 = (y + width) / (float)page.height;
					} else {
						region.u2 = (x + width) / (float)page.width;
						region.v2 = (y + height) / (float)page.height;
					}
					region.x = x;
					region.y = y;
					region.width = Math.Abs(width);
					region.height = Math.Abs(height);

					if (readTuple(reader, tuple) == 4) { // split is optional
						region.splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
								int.Parse(tuple[2]), int.Parse(tuple[3])};

						if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits
							region.pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
									int.Parse(tuple[2]), int.Parse(tuple[3])};

							readTuple(reader, tuple);
						}
					}

					region.originalWidth = int.Parse(tuple[0]);
					region.originalHeight = int.Parse(tuple[1]);

					readTuple(reader, tuple);
					region.offsetX = int.Parse(tuple[0]);
					region.offsetY = int.Parse(tuple[1]);

					region.index = int.Parse(readValue(reader));

					regions.Add(region);
				}
			}
		}
Esempio n. 2
0
    public RegionAttachment NewRegionAttachment(Skin skin, string name, string path)
    {
        RegionAttachment attachment = new RegionAttachment(name);

        Texture2D tex = sprite.texture;
        int instanceId = tex.GetInstanceID();
        AtlasRegion atlasRegion;

        //check cache first
        if (atlasTable.ContainsKey(instanceId)) {
            atlasRegion = atlasTable[instanceId];
        } else {
            //Setup new material
            Material mat = new Material(shader);
            if (sprite.packed)
                mat.name = "Unity Packed Sprite Material";
            else
                mat.name = sprite.name + " Sprite Material";
            mat.mainTexture = tex;

            //create faux-region to play nice with SkeletonRenderer
            atlasRegion = new AtlasRegion();
            AtlasPage page = new AtlasPage();
            page.rendererObject = mat;
            atlasRegion.page = page;

            //cache it
            atlasTable[instanceId] = atlasRegion;
        }

        Rect texRect = sprite.textureRect;

        //normalize rect to UV space of packed atlas
        texRect.x = Mathf.InverseLerp(0, tex.width, texRect.x);
        texRect.y = Mathf.InverseLerp(0, tex.height, texRect.y);
        texRect.width = Mathf.InverseLerp(0, tex.width, texRect.width);
        texRect.height = Mathf.InverseLerp(0, tex.height, texRect.height);

        Bounds bounds = sprite.bounds;
        Vector3 size = bounds.size;

        //TODO: make sure this rotation thing actually works
        bool rotated = false;
        if (sprite.packed)
            rotated = sprite.packingRotation == SpritePackingRotation.Any;

        //do some math and assign UVs and sizes
        attachment.SetUVs(texRect.xMin, texRect.yMax, texRect.xMax, texRect.yMin, rotated);
        attachment.RendererObject = atlasRegion;
        attachment.SetColor(Color.white);
        attachment.ScaleX = 1;
        attachment.ScaleY = 1;
        attachment.RegionOffsetX = sprite.rect.width * (0.5f - Mathf.InverseLerp(bounds.min.x, bounds.max.x, 0)) / sprite.pixelsPerUnit;
        attachment.RegionOffsetY = sprite.rect.height * (0.5f - Mathf.InverseLerp(bounds.min.y, bounds.max.y, 0)) / sprite.pixelsPerUnit;
        attachment.Width = size.x;
        attachment.Height = size.y;
        attachment.RegionWidth = size.x;
        attachment.RegionHeight = size.y;
        attachment.RegionOriginalWidth = size.x;
        attachment.RegionOriginalHeight = size.y;
        attachment.UpdateOffset();

        return attachment;
    }
Esempio n. 3
0
	public static string GetPathSafeRegionName (AtlasRegion region) {
		return region.name.Replace("/", "_");
	}
Esempio n. 4
0
	public static GameObject BakeRegion (AtlasAsset atlasAsset, AtlasRegion region, bool autoSave = true) {
		Atlas atlas = atlasAsset.GetAtlas();
		string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset);
		string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath);
		string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name);
		string bakedPrefabPath = Path.Combine(bakedDirPath, GetPathSafeRegionName(region) + ".prefab").Replace("\\", "/");

		GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject));
		GameObject root;
		Mesh mesh;
		bool isNewPrefab = false;

		if (!Directory.Exists(bakedDirPath))
			Directory.CreateDirectory(bakedDirPath);

		if (prefab == null) {
			root = new GameObject("temp", typeof(MeshFilter), typeof(MeshRenderer));
			prefab = (GameObject)PrefabUtility.CreatePrefab(bakedPrefabPath, root);
			isNewPrefab = true;
			Object.DestroyImmediate(root);
		}

		mesh = (Mesh)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(Mesh));

		Material mat = null;
		mesh = atlasAsset.GenerateMesh(region.name, mesh, out mat);
		if (isNewPrefab) {
			AssetDatabase.AddObjectToAsset(mesh, prefab);
			prefab.GetComponent<MeshFilter>().sharedMesh = mesh;
		}

		EditorUtility.SetDirty(mesh);
		EditorUtility.SetDirty(prefab);

		if (autoSave) {
			AssetDatabase.SaveAssets();
			AssetDatabase.Refresh();
		}


		prefab.GetComponent<MeshRenderer>().sharedMaterial = mat;

		return prefab;
	}
Esempio n. 5
0
        public void load(StreamReader reader, String imagesDir)
        {
            String[] tuple = new String[4];
            AtlasPage page = null;
            while (true) {
                String line = reader.ReadLine();
                if (line == null) break;
                if (line.Trim().Length == 0)
                    page = null;
                else if (page == null) {
                    page = NewAtlasPage(Path.Combine(imagesDir, line));

                    page.Format = (Format)Enum.Parse(typeof(Format), readValue(reader), false);

                    readTuple(reader, tuple);
                    page.MinFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0]);
                    page.MagFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1]);

                    String direction = readValue(reader);
                    page.UWrap = TextureWrap.ClampToEdge;
                    page.VWrap = TextureWrap.ClampToEdge;
                    if (direction == "x")
                        page.UWrap = TextureWrap.Repeat;
                    else if (direction == "y")
                        page.VWrap = TextureWrap.Repeat;
                    else if (direction == "xy")
                        page.UWrap = page.VWrap = TextureWrap.Repeat;

                    pages.Add(page);

                } else {
                    AtlasRegion region = new AtlasRegion();
                    region.Name = line;
                    region.Page = page;

                    region.Rotate = Boolean.Parse(readValue(reader));

                    readTuple(reader, tuple);
                    int x = int.Parse(tuple[0]);
                    int y = int.Parse(tuple[1]);

                    readTuple(reader, tuple);
                    int width = int.Parse(tuple[0]);
                    int height = int.Parse(tuple[1]);

                    float invTexWidth = 1f / page.GetTextureWidth();
                    float invTexHeight = 1f / page.GetTextureHeight();
                    region.U = x * invTexWidth;
                    region.V = y * invTexHeight;
                    region.U2 = (x + width) * invTexWidth;
                    region.V2 = (y + height) * invTexHeight;
                    region.Width = Math.Abs(width);
                    region.Height = Math.Abs(height);

                    if (readTuple(reader, tuple) == 4) { // split is optional
                        region.Splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
                                int.Parse(tuple[2]), int.Parse(tuple[3])};

                        if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits
                            region.Pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
                                    int.Parse(tuple[2]), int.Parse(tuple[3])};

                            readTuple(reader, tuple);
                        }
                    }

                    region.OriginalWidth = int.Parse(tuple[0]);
                    region.OriginalHeight = int.Parse(tuple[1]);

                    readTuple(reader, tuple);
                    region.OffsetX = int.Parse(tuple[0]);
                    region.OffsetY = int.Parse(tuple[1]);

                    region.Index = int.Parse(readValue(reader));

                    regions.Add(region);
                }
            }
        }
Esempio n. 6
0
        private void initialize(TextReader reader, Object texture, int textureWidth, int textureHeight)
        {
            TextureWidth = textureWidth;
            TextureHeight = textureHeight;
            Texture = texture;

            Regions = new List<AtlasRegion>();
            float invTexWidth = 1f / textureWidth;
            float invTexHeight = 1f / textureHeight;
            String[] tuple = new String[4];

            // Skip past first page name.
            while (true) {
                String line = reader.ReadLine();
                if (line.Trim().Length != 0)
                    break;
            }

            Format = (Format)Enum.Parse(typeof(Format), readValue(reader), false);

            readTuple(reader, tuple);
            MinFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[0]);
            MagFilter = (TextureFilter)Enum.Parse(typeof(TextureFilter), tuple[1]);

            String direction = readValue(reader);
            UWrap = TextureWrap.ClampToEdge;
            VWrap = TextureWrap.ClampToEdge;
            if (direction == "x")
                UWrap = TextureWrap.Repeat;
            else if (direction == "y")
                VWrap = TextureWrap.Repeat;
            else if (direction == "xy")
                UWrap = VWrap = TextureWrap.Repeat;

            while (true) {
                String line = reader.ReadLine();
                if (line == null || line.Trim().Length == 0) break;

                AtlasRegion region = new AtlasRegion();
                region.Atlas = this;
                region.Name = line;

                region.Rotate = Boolean.Parse(readValue(reader));

                readTuple(reader, tuple);
                int x = int.Parse(tuple[0]);
                int y = int.Parse(tuple[1]);

                readTuple(reader, tuple);
                int width = int.Parse(tuple[0]);
                int height = int.Parse(tuple[1]);

                region.U = x * invTexWidth;
                region.V = y * invTexHeight;
                region.U2 = (x + width) * invTexWidth;
                region.V2 = (y + height) * invTexHeight;
                region.Width = Math.Abs(width);
                region.Height = Math.Abs(height);

                if (readTuple(reader, tuple) == 4) { // split is optional
                    region.Splits = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
                                int.Parse(tuple[2]), int.Parse(tuple[3])};

                    if (readTuple(reader, tuple) == 4) { // pad is optional, but only present with splits
                        region.Pads = new int[] {int.Parse(tuple[0]), int.Parse(tuple[1]),
                                    int.Parse(tuple[2]), int.Parse(tuple[3])};

                        readTuple(reader, tuple);
                    }
                }

                region.OriginalWidth = int.Parse(tuple[0]);
                region.OriginalHeight = int.Parse(tuple[1]);

                readTuple(reader, tuple);
                region.OffsetX = int.Parse(tuple[0]);
                region.OffsetY = int.Parse(tuple[1]);

                region.Index = int.Parse(readValue(reader));

                Regions.Add(region);
            }

            while (true) {
                String line = reader.ReadLine();
                if (line == null)
                    break;
                if (line.Trim().Length != 0) throw new Exception("An atlas with multiple images is not supported.");
            }
        }
Esempio n. 7
0
		public RegionAttachment NewRegionAttachment (Skin skin, string name, string path) {
			RegionAttachment attachment = new RegionAttachment(name);

			Texture2D tex = sprite.texture;
			int instanceId = tex.GetInstanceID();
			AtlasRegion atlasRegion;
			bool cachedMaterialExists = atlasTable.TryGetValue(instanceId, out atlasRegion);

			if (!cachedMaterialExists) {
				// Setup new material.
				var material = new Material(shader);
				if (sprite.packed)
					material.name = "Unity Packed Sprite Material";
				else
					material.name = sprite.name + " Sprite Material";
				material.mainTexture = tex;

				// Create faux-region to play nice with SkeletonRenderer.
				atlasRegion = new AtlasRegion();
				var page = new AtlasPage();
				page.rendererObject = material;
				atlasRegion.page = page;

				// Cache it.
				atlasTable[instanceId] = atlasRegion;
			}

			Rect texRect = sprite.textureRect;

			// Normalize rect to UV space of packed atlas
			texRect.x = Mathf.InverseLerp(0, tex.width, texRect.x);
			texRect.y = Mathf.InverseLerp(0, tex.height, texRect.y);
			texRect.width = Mathf.InverseLerp(0, tex.width, texRect.width);
			texRect.height = Mathf.InverseLerp(0, tex.height, texRect.height);

			Bounds bounds = sprite.bounds;
			Vector2 boundsMin = bounds.min, boundsMax = bounds.max;
			Vector2 size = bounds.size;
			float spriteUnitsPerPixel = 1f / sprite.pixelsPerUnit;

			bool rotated = false;
			if (sprite.packed)
				rotated = sprite.packingRotation == SpritePackingRotation.Any;

			attachment.SetUVs(texRect.xMin, texRect.yMax, texRect.xMax, texRect.yMin, rotated);
			attachment.RendererObject = atlasRegion;
			attachment.SetColor(Color.white);
			attachment.ScaleX = 1;
			attachment.ScaleY = 1;
			attachment.RegionOffsetX = sprite.rect.width * (0.5f - InverseLerp(boundsMin.x, boundsMax.x, 0)) * spriteUnitsPerPixel;
			attachment.RegionOffsetY = sprite.rect.height * (0.5f - InverseLerp(boundsMin.y, boundsMax.y, 0)) * spriteUnitsPerPixel;
			attachment.Width = size.x;
			attachment.Height = size.y;
			attachment.RegionWidth = size.x;
			attachment.RegionHeight = size.y;
			attachment.RegionOriginalWidth = size.x;
			attachment.RegionOriginalHeight = size.y;
			attachment.UpdateOffset();

			return attachment;
		}