Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to set/use "StaticEditorFlags"? Can't seem to set them from script

Discussion in 'Scripting' started by yahodahan, May 22, 2012.

  1. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
    Just looking for help on how to set, via script, static flags. ie, these: http://unity3d.com/support/documentation/ScriptReference/StaticEditorFlags.html

    I need to enable/disable certain flags individually, so simple setting "isStatic" doesn't work as I need.

    **EDIT**
    I have had partial success using this:

    GameObjectUtility.SetStaticEditorFlags(theObject, (StaticEditorFlags.BatchingStatic));

    However, I do not see how to set multiple flags. Any help on that?
    Thanks much!
     
    Last edited: May 22, 2012
  2. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
    *(edited for incorrect content)* :)

    While I'm at it- would there also be a way to set multiple "HideFlags"? That would be lovely!

    Thanks!
     
    Last edited: May 22, 2012
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  4. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
    Yep, I realize that- will edit question. I have had partial success using this:

    GameObjectUtility.SetStaticEditorFlags(theObject, (StaticEditorFlags.BatchingStatic));

    However, I do not see how to set multiple flags. Any help on that?

    About the documentation, thank you for the link, however I did already link to just about the same page- honestly, I've dug all through the documentation, and there isn't a single example, or other needed item, to be found.

    I printed "GetStaticEditorFlags" on an object with multiple flags and got "OccluderStatic, LightMapStatic", so it seems I should be able to set multiple at once via...a string array? Multiple trial and error, no luck yet.

    Using JS, also, if that helps clarify.

    Thanks again.
     
    Peter77, Ali-Nagori and ocimum like this.
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    ahhh... flags... are binary

    so do a binary and, and you get multiple results

    var a = flag1 flag2; // == flag1 with flag 2
     
  6. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
    Erm... unfortunately you've lost me there :(

    "do a binary" and "//==", I'm not sure what either of those are. Are you saying "var a" is an array, and then add flag1 and flag2 to it?

    Could you expand on that a bit? I feel I'm so close...but can't quite get this working correctly.
     
  7. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
  8. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
    Still no luck- "NullReference..." error on the first line every time:

    Code (csharp):
    1. var newFlags = StaticEditorFlags.BatchingStatic  StaticEditorFlags.LightMapStatic;
    2. GameObjectUtility.SetStaticEditorFlags(thePlane, (newFlags));
    What am I messing up here?
     
  9. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
    ok, further testing, I have this:
    Code (csharp):
    1. var newFlags = StaticEditorFlags.BatchingStatic  StaticEditorFlags.LightmapStatic;
    2. Debug.Log(newFlags)
    ...which prints "0". That's got to be wrong, since:

    Code (csharp):
    1. var iResult = 25  3;
    2. Debug.Log(iResult);
    ...prints "1"

    Arg. Any help? Someone must have used these before...yes?
     
  10. yahodahan

    yahodahan

    Joined:
    Apr 26, 2009
    Posts:
    1,380
    I got it I GOT IT I GOT IT!

    Yall have no idea how happy this makes me. And now I shall share for others!

    Code (csharp):
    1. var newFlags = StaticEditorFlags.BatchingStatic | StaticEditorFlags.LightmapStatic;
    2. GameObjectUtility.SetStaticEditorFlags(theObject, newFlags);
    You must use the | symbol, not

    and then set directly :)

    Hope this helps some others!
     
  11. GabeF

    GabeF

    Joined:
    Sep 28, 2012
    Posts:
    38
    you make me happy to. you put that up 5 years ago, yet I'm still making use of it today. Much obliged
     
    yahodahan likes this.
  12. HanzaRu

    HanzaRu

    Joined:
    Aug 10, 2012
    Posts:
    11
    You can eliminate a flag on a object making a binary exclusion of that flag
    Code (CSharp):
    1. StaticEditorFlags flags = GameObjectUtility.GetStaticEditorFlags(mesh.gameObject);
    2.                 flags = flags & ~(StaticEditorFlags.NavigationStatic);
    3.                 GameObjectUtility.SetStaticEditorFlags(mesh.gameObject,flags);
    -----------------------------------------------------------------------------------------------------------
    this is a bitwise operation;
    First you need to isolate the bit you need to change, by invert it:

    so, you can operate a & bitwise:

    No changes, means the flag already is 0
    Case 1 - is Active
    ???????1
    & 11111110
    ----------------
    ???????0
    Case 2 - isn't Active
    ???????0
    & 11111110
    ----------------
    ???????0

    this solves the problem, but only in editor -> you can't use it while playing the game. Is for "fast apply multiple flags in multiple objects"
     
    CoughE, liam_unity628, StenCG and 8 others like this.
  13. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    Here is a convenient method:

    Code (CSharp):
    1.         public static void SetStaticEditorFlag(GameObject obj, StaticEditorFlags flag,  bool shouldEnable)
    2.         {
    3.             var currentFlags = GameObjectUtility.GetStaticEditorFlags(obj);
    4.  
    5.             if (shouldEnable)
    6.             {
    7.                 currentFlags |= flag;
    8.             }
    9.             else
    10.             {
    11.                 currentFlags &= ~flag;
    12.             }
    13.            
    14.             GameObjectUtility.SetStaticEditorFlags(obj, currentFlags);
    15.         }
    Use like:

    Code (CSharp):
    1. SetStaticEditorFlag(gameObject, StaticEditorFlags.LightmapStatic, true)
     
  14. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    606
    10 years later this bit of code makes more sense than everything after it.

    Totally didn't understand Unity's API documents
    (This is not helpful :confused: ... they should use your code)
    https://docs.unity3d.com/ScriptReference/StaticEditorFlags.OccluderStatic.html

    Your bit of code was clean and easy to understand. Thanks for following up with it. :)

    For anyone else from the future you'll need to also include
    Code (CSharp):
    1. using UnityEditor;
     
    Last edited: Jan 5, 2023