Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Help Wanted How can I check if UV channel exists in mesh?

Discussion in 'Scripting' started by Peter77, Jan 14, 2021.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,589
    I write an editor tool that gets the UV's of a particular mesh using the Mesh.GetUVs method. The tool allows to select a UV channel and this is then used to call GetUVs, like:
    Code (CSharp):
    1. mesh.GetUVs(selectedChannel, uvs);
    ... where
    selectedChannel
    can be 0, 1, 2 up to 7.

    If I call Mesh.GetUVs with a channel that doesn't exist in the mesh, Unity outputs an error. So far so good.

    The problem is I can't find a way to check if a specific UV channel exists, to avoid calling Mesh.GetUVs and triggering the error.

    How can I check if a specific UV channel exists in a mesh?
     
    PraetorBlue likes this.
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    688
    Are you sure it actually throws an error? I haven't done much mesh stuff lately. In the past Unity only supported up to 4 UV channels and back then you could only get them through individual properties. I know GetUVs has been added and lately they increased the max supported channels. Though I think it would be strange if they would throw an error if you try to read one that doesn't exist. In my UVViewer editor window I still use the old properties. Back then the returned array simply was empty when the channel did not exist.

    Are you sure that you actually created the list you pass in? What exact error do you get? Do you get an exception or just a message in the console (error or warning?).

    Actually I just checked my old general purpose mesh serializer which could serialize any mesh in a custom binary format including bone weights and blendshapes. I also used GetUVs, though back then only 4 channels were supported. I actually did check if the list got filled or not and afaik, this worked as it should.

    Currently I don't have Unity at hand so I can't perform any tests on this. However if you have issues with some code, it would help to see more context. So the concrete error you got and make sure you actually created the list before you call GetUVs
     
    Peter77 likes this.
  3. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    5,589
    Thank you for the help. You're right,
    GetUVs
    also just returns an empty list. I wasn't paying enough attention to the error message before. The error was caused by my own code surrounding the GetUVs call.
     
    Bunny83 likes this.
unityunity