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 /
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

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

rotate an image by modifying Texture2D.GetPixels32() array 2 Answers

getPixel/setPixels or stencil eraser brush 2 Answers

How to get ARGB values from Texture object? 1 Answer

How to change origin of GetPixels 0 Answers

Baking decals into a texture 3 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