• 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 /
This question was closed Aug 13, 2012 at 07:56 AM by AlwaysSunny for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by AlwaysSunny · Aug 13, 2012 at 07:21 AM · texture2dclonesgetpixelsinstantiating

instantiate a texture2d so changes don't affect clones

What I'm doing:
Exposing a public variable for a Texture2D
Using Get/SetPixels to create a new Texture2D by 'cropping' from the source

Why (the heck) I'm doing this:
Projectors can't use sprite sheets, so I'm providing a sprite sheet to a script on the projector and using code to create a new Texture2D representing a single cell of the spritesheet.

Works just fine... until I have more than one of these in the scene, created with GameObject.Instantiate(); In that case, each new clone changes the textures used by every other clone. Quite confused by why this is happening. Advice would be awesome.

Code, stripped of nonessential info...

  public Texture2D spriteMap;  // sheet with rows/columns of individual sprites
  public int rows;             // how many rows/ colums represented on the sheet
  public int columns; 
  private Projector proj;      // projector component of this object 
  
  void Start()
  { 
 
  proj =  GetComponent(); // UnityAnswers won't let me use gt, lt signs...
 
  Material myCustomMaterial = Instantiate(proj.material) as Material;
 
  // -----------------------------------
  // info I need to randomly grab a cell of the spritesheet...
  int x = CHelp.GetRandomInt(0, columns);
  int y = CHelp.GetRandomInt(0, rows);
  int sourceSizeX = spriteMap.width;
  int sourceSizeY = spriteMap.height;
  int cellSizeX = sourceSizeX / columns;
  int cellSizeY = sourceSizeY / rows;
     
  //Trying to copy the texture rather than reference it...??
  Texture2D mySourceTexture = Instantiate(spriteMap) as Texture2D;
 
  Color[] cropping = 
  mySourceTexture.GetPixels(
  x * cellSizeX,
  y * cellSizeY,
  cellSizeX,
  cellSizeY);
  
  mySourceTexture = new Texture2D(cellSizeX, cellSizeY); 
  mySourceTexture.SetPixels(cropping); 
  mySourceTexture.wrapMode = TextureWrapMode.Clamp;
  mySourceTexture.Apply(); 
  // -----------------------------------
  proj.material = myCustomMaterial;
  proj.material.color = tint;
  proj.material.SetTexture("_ShadowTex", mySourceTexture); 
  }
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

1 Reply

  • Sort: 
avatar image
1

Answer by AlwaysSunny · Aug 13, 2012 at 07:57 AM

Duh. Wasn't cloning the material, so all clones shared it. For anyone who wants this script in the future, it's now been edited.

Comment
Add comment · Show 1 · 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 yashesh · Apr 11, 2014 at 12:01 PM 0
Share

i am facing the same issue. can you please help me ? thanks

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Can't seem to use GetPixels on a square texture in Unity 3.0 Iphone 0 Answers

Painting stencil on a surface. 6 Answers

Setting pixels on a texture? 2 Answers

How to get the pixel colors of a sprite when there is a color added? 0 Answers

How can I show my SetPixels() texture changes without having to zoom my orthographic camera? 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