Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Need to change AutoUnwrapSettings at Run-Time

Discussion in 'World Building' started by Ritchie-J, May 4, 2019.

  1. Ritchie-J

    Ritchie-J

    Joined:
    Feb 26, 2018
    Posts:
    26
    So this is the code which should help implement what I require :-
    Code (CSharp):
    1. AutoUnwrapSettings autoUnWrapUL = new AutoUnwrapSettings();
    2. autoUnWrapUL.anchor = AutoUnwrapSettings.Anchor.UpperLeft;
    3. autoUnWrapUL.fill = AutoUnwrapSettings.Fill.Fit;
    4. autoUnWrapUL.scale = new Vector2(0.33f, 0.33f);
    But due to brain fade I can’t figure out how to let ShapeGenerator use it.
    Answer must be staring me in the ‘Face’. Any help would be appreciated.
     
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    548
    Unwrap settings have to be applied after creation. So you'd do something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.ProBuilder;
    3.  
    4. public class CreateCubeWithUnwrapSettings : MonoBehaviour
    5. {
    6.     void Start()
    7.     {
    8.         var mesh = ShapeGenerator.CreateShape(ShapeType.Cube);
    9.  
    10.         foreach (var face in mesh.faces)
    11.         {
    12.             face.uv = new AutoUnwrapSettings()
    13.             {
    14.                 anchor = AutoUnwrapSettings.Anchor.UpperLeft,
    15.                 fill = AutoUnwrapSettings.Fill.Fit,
    16.                 scale = new Vector2(0.33f, 0.33f)
    17.             };
    18.         }
    19.  
    20.         // Just need to rebuild the UVs, and since we didn't modify vertex count or materials we can skip the `ToMesh`
    21.         // invocation.
    22.         mesh.Refresh(RefreshMask.UV);
    23.     }
    24. }
    25.  
     
    jeremedia and LoneSurvivor82 like this.
  3. Ritchie-J

    Ritchie-J

    Joined:
    Feb 26, 2018
    Posts:
    26
    Many thanks! Works just fine plugging in my presets.
     
    kaarrrllll likes this.
  4. LoneSurvivor82

    LoneSurvivor82

    Joined:
    Aug 17, 2019
    Posts:
    41
    Thank you very very much, this is exactly what I was looking for. Great work! Thank you so much for your time to help users here, @kaarrrllll :)