• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by prodigee · Feb 09, 2017 at 03:32 PM · texturestexture atlassciptingwww.loadimageintotexture

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 () {
         
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Where are all my assets and textures? 1 Answer

How do I apply Multiple textures to a surface? 1 Answer

C4D R11.5 -Unity iPhone - Can I Use the animate Textures feature in Unity? 3 Answers

Textures look horrible. 2 Answers

Textures weird transparency problem -- black lines on edges -- 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges