Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 FarCryFromHuman · Feb 03, 2014 at 06:12 PM · terrainspritetexture2dspriterenderer

Instantiate a gameObject with a dynamically generated sprite?

Hi, I'm trying to create terrain using randomized sinusoidal functions (NYI) but I can't seem to get any dynamically generated sprites to draw. Here's my current code:

 public class GenerateTerrain : MonoBehaviour {
 
     public int initialCount;
     List<GameObject> terrains;
     public GameObject prefab;
 
     // Use this for initialization
     void Start()
     {
         terrains = new List<GameObject>();
         for (int i = 0; i < initialCount; i++)
         {
             AddTerrain();  
         }
     }
 
     public void AddTerrain()
     {
         // create texture
         // placeholder: 800 x 600, all white
         Texture2D texture = new Texture2D(800, 600);
         Color[] pixels = new Color[texture.width * texture.height];
         for (int i = 0; i < pixels.Length; i++)
         {
              pixels[i] = Color.red;
         }
         texture.SetPixels(pixels);
         texture.Apply();
 
         // instantiate new terrain
         GameObject terrain = Instantiate(prefab) as GameObject;
         
         // add components
         // create new sprite from generated texture, pivot in middle
         Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(texture.width * 0.5f, texture.height * 0.5f));
 
         // create new renderer and assign it the sprite
         SpriteRenderer spriteRenderer = terrain.AddComponent<SpriteRenderer>();
         spriteRenderer.sprite = sprite; 
 
         // generate collider
         terrain.AddComponent<PolygonCollider2D>();
 
         // add to list for later removal (memory management)
         terrains.Add(terrain);
     }
 }

The initialCount variable is set to 1 and the prefab is an empty gameObject (just a transform with default values). The inspector shows a single terrain being created and the preview window shows an 800x600 red sprite, but nothing appears on screen. Any suggestions are welcome!

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.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Terrain generation 0 Answers

How to programmatically change image properties? 1 Answer

Can I create a sprite at runtime? 3 Answers

Slicing sliced sprite via script 0 Answers

How do I make sure my sprites are fitted properly to my textures? 1 Answer


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