Advertisement
Staggart

Create Lightmap UV

May 9th, 2024 (edited)
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3.  
  4. namespace sc.modeling.splines.runtime.auxiliary
  5. {
  6.     [ExecuteAlways]
  7.     public class SplineMesherLightmapUV : MonoBehaviour
  8.     {
  9.         [Tooltip("Let Unity automatically create a new UV (slow but accurate)")]
  10.         public bool autoUnwrap = true;
  11.         public SplineMesher splineMesher;
  12.        
  13.         private void Reset()
  14.         {
  15.             splineMesher = GetComponent<SplineMesher>();
  16.         }
  17.        
  18.         private void OnEnable()
  19.         {
  20.             //Subscribe
  21.             SplineMesher.onPostRebuildMesh += OnPostRebuild;
  22.         }
  23.  
  24.         private void OnPostRebuild(SplineMesher instance)
  25.         {
  26.             //Is the instance being rebuild the one we want to work with
  27.             if (instance == splineMesher)
  28.             {
  29.                 MeshFilter mf = splineMesher.GetComponent<MeshFilter>();
  30.  
  31.                 if (mf)
  32.                 {
  33.                     #if UNITY_EDITOR
  34.                     if (autoUnwrap)
  35.                     {
  36.                         UnityEditor.Unwrapping.GenerateSecondaryUVSet(mf.sharedMesh);
  37.                     }
  38.                     else
  39.                     {
  40.                         mf.sharedMesh.uv2 = mf.sharedMesh.uv;
  41.                     }
  42.                     #endif
  43.                 }
  44.             }
  45.         }
  46.  
  47.         private void OnDisable()
  48.         {
  49.             //Unsubscribe
  50.             SplineMesher.onPostRebuildMesh -= OnPostRebuild;
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement