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. Dismiss Notice

Editor script - Find all objects set to lightmap static

Discussion in 'Scripting' started by Gnimmel, Sep 7, 2015.

  1. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    358
    I'm trying to select every object in the scene that has the lightmap static flag set. If this makes a difference, I am still using unity pro 4.

    I have a export obj script which exports the selected objects with their lightmap UVs so I can paint over lightmaps in substance painter, but I'd like to find a way to get the selection automatically instead of manually selecting all my lightmapped objects.

    Can anyone point me in the right direction?
     
  2. SvSe

    SvSe

    Joined:
    Nov 4, 2014
    Posts:
    1
    Any luck with this? I am trying to do something similar, but I could also work with all objects that are reflection probe static.
     
  3. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    358
    It ended up being easy enough to work out. Below is the script I ended up writing.

    Just so you know, I am still using unity 4, so its never been tested on 5 yet.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections.Generic;
    4.  
    5. public class SelectLightmapStatic : EditorWindow
    6. {
    7.     [MenuItem("Custom/Select Lightmap Static")]
    8.     static void Init()
    9.     {
    10.  
    11.         // Find all the lightmap static objects
    12.  
    13.         Object[] All_GOs = FindObjectsOfType(typeof(GameObject));
    14.  
    15.         GameObject[] GOArray;
    16.         GOArray = new GameObject[All_GOs.Length];
    17.  
    18.         int ArrayPointer = 0;
    19.  
    20.  
    21.         foreach(GameObject EachGO in All_GOs)
    22.         {
    23.             StaticEditorFlags flags = GameObjectUtility.GetStaticEditorFlags( EachGO );
    24.  
    25.             if ((flags & StaticEditorFlags.LightmapStatic)!=0)
    26.             {
    27.                 if( EachGO.GetComponent<MeshFilter>() != null )
    28.                 {
    29.                     GOArray[ArrayPointer] = EachGO;
    30.                     ArrayPointer += 1;
    31.                 }
    32.             }
    33.  
    34.         }
    35.  
    36.         Selection.objects = GOArray;
    37.  
    38.     }
    39.  
    40. }
    41.  
     
  4. DarthDisembowel

    DarthDisembowel

    Joined:
    Jun 11, 2014
    Posts:
    54
    Works like a dream on Unity 5. Thanks!
     
  5. davidhaley

    davidhaley

    Joined:
    Jul 30, 2016
    Posts:
    8
    Works perfectly. Cheers!
     
  6. UnityCoach

    UnityCoach

    Joined:
    Mar 9, 2015
    Posts:
    5
    I was trying to do something similar with Navigation Static objects when I found this post.
    Thanks a lot @Gnimmel you saved me time here.

    And to contribute to this, here's my adapted version, using Linq, and the probably new GameObjectUtility.AreStaticEditorFlagsSet method.

    Code (CSharp):
    1. using System.Linq;
    2.     static void SelectStaticsByFlags (StaticEditorFlags flagsFilter)
    3.     {
    4.         var allGameObjects = FindObjectsOfType<GameObject>();
    5.  
    6.         var statics = (from item in allGameObjects
    7.             where GameObjectUtility.AreStaticEditorFlagsSet(item.gameObject, flagsFilter)
    8.             select item).ToArray();
    9.  
    10.         Selection.objects = statics;
    11.     }
     
  7. Gnimmel

    Gnimmel

    Joined:
    Apr 21, 2010
    Posts:
    358
    Wow, this is a old thread but I'm glad it helped :)
     
    twobob likes this.