- Home /
How to apply texture to gameobject with texture atlas?
I'm trying to download a series of map tiles from an internet source, put them into a texture atlas, and then apply the "stitched" image as a single texture to an object. Because of the nature of the tiles, I can't simply use and use www.LoadImageIntoTexture - this will only work for a single image (I do implement this in my script, however). I read on some other forum answer that the best way to stitch images together in a collage-like fashion was to use a texture atlas. However, I've been running into issues that I believe may be due to a lack of understanding the texture atlas technique, or the C# language. I'm new to Unity.
I'm also using a coroutine within a for loop in order to populate my texture2d array. Not sure if this is the proper use of a coroutine, but I know I had to use one when experimenting with downloading a single image (waiting for the image to finish downloading in the start function) and applying it to a texture. I've been working in steps.
Each image is 256 x 256, which makes an 8x8 atlas of 2048 by 2048 pixels. I don't understand how to use the rects variable, but it was on the scripting documentation for the Texture2D.PackTextures page, so I figured I should probably include it?
Any help would be much appreciated! Here's my script for reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TextureEditor : MonoBehaviour {
public Texture2D[] atlasTextures;
public Rect[] rects;
// Use this for initialization
IEnumerator Start () {
int idx = 0;
for(int i = 0; i < 8; i++) //8 cols of tiled images
{
for(int j = 0; j < 8; j++) //8 rows
{
string URL = System.String.Format("a.maps.owm.io:8099/589a02cf24cec8000135acd1/3/{a}/{b}?hash=0135e466b47705745ffa9e6a98379755", i, j); //URL is a custom map on OpenWeatherMap.com, you may not be able to test it, but I'm not sure
WWW www = new WWW("http://" + URL);
yield return www;
www.LoadImageIntoTexture(atlasTextures[idx]);
idx++;
}
}
Texture2D atlas = new Texture2D(2048, 2048);
rects = atlas.PackTextures(atlasTextures, 0, 2048); //what do I do with this?
GetComponent<Renderer>().material.mainTexture = atlas; //this was honestly a total guess of what to do. I just know atlas is a texture object, thought maybe it would work.
}
// Update is called once per frame
void Update () {
}
}
Your answer
Follow this Question
Related Questions
Pink texture with Resources.Load 0 Answers
Sprites compression question (their size in build) 2 Answers
Texture lighting and seam issues 1 Answer
(C#) change texture and shadow resolution 0 Answers
Monocolor texture problem 1 Answer