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
2
Question by boingyman · Dec 16, 2014 at 10:10 AM · meshuvoffset

Mesh UVs won't change positions

So I am trying to change the UVs of a mesh inside the editor and I can't seem to be able to make it work. When I try to assign new UV coordinates to a Mesh variable, the UV coordinates in the variable does not change.

 theRect.y += theRect.height / 2;
 if (GUI.Button(theRect, "Offset")){
     for (i = 0; i < 4; i++){
         selectedUVs[i] = Vector2(selectedUVs[i].x + 0.25, selectedUVs[i].y + 0.25);
     }
 }
 
 //Update the UVs here
 for (i = 0; i < 4; i++){
     theMesh.uv[(faceNum * 4) - (4 - i)] = selectedUVs[i];
 }
 
 hitInfo.transform.gameObject.GetComponent(MeshFilter).sharedMesh.uv = theMesh.uv;

The line with theMesh.uv[(faceNum * 4) - (4 - i)] = selectedUVs[i]; should be assigning the values properly (to my knowledge anyways). Am I missing something here?

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

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by CHPedersen · Dec 16, 2014 at 10:16 AM

This is because you're setting uvs on the array returned by the .uv property of class Mesh. But this property is not actually a reference directly to the Mesh's own uv array - it is instead a deep copy of all the data it contains. So you're telling Unity to give you a deep copy of the uv array for each iteration of the for-loop here:

 theMesh.uv[(faceNum * 4) - (4 - i)] = selectedUVs[i];

And then you set the value at an index in each deep copy, and then it gets thrown away again afterwards. So your changes never truly make it into the Mesh's actual uv array.

For the changes to take effect, you need to copy out the uv array ONCE, make changes to the copied array, then ASSIGN IT BACK to the target mesh. Then it'll work. :) Here's an example:

     // Grab a reference to the target mesh
     Mesh meshYouWantToChange = GetComponent<MeshFilter>().mesh;

     // Copy out the contents of its uv array (the property returns a deep copy of the data)
     Vector2[] itsUVs = meshYouWantToChange.uv;

     // Make changes to that copy
     itsUVs[0] = new Vector2(0.5f, 0.5f);

     // Assign it back to the target mesh.
     meshYouWantToChange.uv = itsUVs;
Comment
Add comment · Show 3 · 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 boingyman · Dec 16, 2014 at 04:04 PM 0
Share

It works, I'm not entirely sure how it works, but it works! Thank you! :)

I don't understand what a "deep copy" is, so could you explain that please?

And oddly enough I actually did it the way you described in a previous script I made, but thought it would be better if I did it the way I showed in the question.

avatar image CHPedersen · Dec 16, 2014 at 10:53 PM 0
Share

You're welcome. :)

A 'deep copy' is when a full copy of all the contents of an array is made and returned ins$$anonymous$$d of a copy of the reference to the array.

The difference is that if you receive a copy of the reference, then that will point to the same actual object and thus, modifications to it will affect the original object. That would be a shallow copy. If, like in this case, a property returns a deep copy, you are ins$$anonymous$$d receiving a reference to a whole new, cloned object (the array) that just contains the same data. $$anonymous$$odifications to that copy do not affect the original.

See this Wikipedia article:

http://en.wikipedia.org/wiki/Object_copy

It has a neat little diagram that explains a deep copy. In that diagram, if it was a shallow copy, then there would be only one row of X1, X2, X3,..., and A and B would both be pointing to the same first element.

avatar image boingyman · Dec 16, 2014 at 11:16 PM 0
Share

Okay, that makes more sense now. Thanks for the help! :)

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

2 People are following this question.

avatar image avatar image

Related Questions

Moving Mesh Instance UVs in Editor? (code provided) 3 Answers

Remove annoying texture offset on custom mesh 0 Answers

Combine Mesh but Keep UV Offset & Scale 0 Answers

How do I convert a Vector2 from a UV coordinate to an absolute pixel position? 1 Answer

OnMouseDown() with a Mesh Collider? 2 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