Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 swin7984 · Jun 02, 2014 at 06:14 AM · gameobjecttexturesdynamically

Is there a way to dynamically populate game objects via texture maps?

I am still very new to Unity but really enjoying its interface. I was curious if there was a way to dynamically populate gameObjects using texture maps. For example, we have a plane that has a black/white checkered board texture. At runtime can every black square populate a cube.

If its possible, can you do it with a hidden texture?

Comment
Add comment · Show 1
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
avatar image calmcarrots · Jun 02, 2014 at 06:30 AM 0
Share

Doing it by texture requires some hard work. Everything is possible in Unity btw. I would just do a for loop maybe and add a cube every X units.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jun 02, 2014 at 06:34 AM

So you want to know the color of the texture, you can use

http://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html

knowing the position of your squares, you can check for color.

Comment
Add comment · Share
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
avatar image
0

Answer by MikeNewall · Jun 02, 2014 at 08:00 AM

Providing you use a power of 2 texture you can calculate the number of squares in the texture and their colours. You then instantiate the cubes for each square which matches the colour.

If you get a row of pixels just one pixel high you can calculate the number of columns by looping over them and checking when the colour changes. A different colour would mean the start of a new column. This only works for a checker board of course.

Once you have the row count you can calculate the width in pxels of each square by dividing the texture width by the column count. Knowing the size of each square will allow you to step through the textures pixels at offsets which matchthe width and height of your squares. You can check the colour of each one and see if it matches the one you want to instantiate stuff.

     public Texture2D texture;
 
     private int columnCount = 0;
     private Color[] colours = new Color[2];
 
     private void Start(){
 
         if(texture){
 
             // Calculate number of columns and save their individual colours
             Color[] row = texture.GetPixels(0,0,texture.width, 1);
             Color prevPixel = new Color(1000,1000,1000);
 
             // loop through row and figure out how many columns there are
             for (int i = 0; i < row.Length; i++) {
                 
                 // Save each colour
                 if(columnCount == 0){
                     colours[0] = row[i];
                 }
                 else if(columnCount == 1) {
                     colours[1] = row[i];
                 }
                 
                 // Check if same colour as previous pixel. inc rowcount if not
                 if(row[i] != prevPixel){
                     columnCount ++;
                 }
                 
                 // store previous pixel
                 prevPixel = row[i];
             }
 
             // calculate width of column
             int columnWidth = texture.width / columnCount;
 
             // Loop through texture pixels stepping by the column width in X and Y
             // Check the colour of each square
 
             for (int x = 0; x < texture.width; x += columnWidth) {
 
                 for (int y = 0; y < texture.height; y += columnWidth) {
                     if(texture.GetPixel(x, y) == colours[0]){
 
                         // prevent divide by 0 error
                         float cubeX = 0;
                         float cubeY = 0;
 
                         if(x > 0){
                             cubeX = x / columnWidth;
                         }
                         if(y > 0){
                             cubeY = y / columnWidth;
                         }
 
                         Instantiate(GameObject.CreatePrimitive (PrimitiveType.Cube), new Vector3(cubeX, cubeY, 0), Quaternion.identity);
                     }
                     else if(texture.GetPixel(x, y) == colours[1]){
                         // do something with the other colour if you want
                     }
                 }
             }
         }
     }
Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to assign a gameobject to a script that is added dynamically 1 Answer

Instantiate flexible amount of objects to prevent frame rate drop 0 Answers

Switch to New Shader and Back to Original object's Shader in C# Script 0 Answers

Find a variable using a string 1 Answer

Is there any way I can delete the part of a GameObject that is inside another? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges