• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by a7BiT-psycho · Nov 20, 2015 at 06:41 PM · exceptionlodlodgroup

Editing a LODGroup from script

Hello Unity Answers!

My current script is raising t$$anonymous$$s exception whenever I try to set LOD levels: "SetLODs: Attempting to set LOD where the screen relative size is greater then or equal to a $$anonymous$$gher detail LOD level."

Before I post the actual script, what is supposed to happen, and what is actually happening?

The script is supposed to create a new LODGroup out of a selection of GameObjects. The GameObjects have to be named MyGameObjectName_LOD0, MyGameObjectName_LOD1, etc. T$$anonymous$$s works 100% fine as long as you only have one GameObject selected. When selecting a 2nd or more GameObjects the exception above is raised.

     private void CreateLodGroup()
     {
         GameObject[] sel = Selection.gameObjects;
         if (sel == null || sel.Length == 0)
         {
             Debug.LogError("FAILED -> You must have at least 1 GameObject selected!");
             return;
         }
         var lods = new SortedList<int, GameObject>();
         string mainName = null;
         foreach (GameObject o in sel)
         {
             string oName = o.name;
             Match regexMatch = Regex.Match(oName, @"(.*)_LOD([0-9])");
             if (regexMatch.Success)
             {
                 string oMainName = regexMatch.Groups[1].ToString();
                 int oIndex = Convert.ToInt32(regexMatch.Groups[2].ToString());
                 if (mainName == null || oMainName == mainName)
                 {
                     mainName = oMainName;
                 }
                 else
                 {
                     Debug.LogError("FAILED -> The GameObject main names aren't matc$$anonymous$$ng! Expected main name \"" +
                                    mainName + "\", got \"" + oMainName + "\"!");
                     return;
                 }
                 if (!lods.ContainsKey(oIndex))
                 {
                     lods.Add(oIndex, o);
                 }
                 else
                 {
                     Debug.LogError("FAILED -> The LOD object with the ID " + oIndex +
                                    " exists twice! Make sure to avoid duplicates!");
                     return;
                 }
             }
             else
             {
                 Debug.LogError("FAILED -> GameObject \"" + oName +
                                "\" was in the wrong name format to be added as a LOD object!");
                 return;
             }
         }
         int lfIndex = lods.Count - 1;
         w$$anonymous$$le (lfIndex >= 0)
         {
             if (lods.ContainsKey(lfIndex))
             {
                 lfIndex--;
             }
             else
             {
                 Debug.LogError("FAILED -> LOD level " + lfIndex + " could not be found!");
                 return;
             }
         }
         var lodObject = new GameObject(mainName);
         var lodGroup = lodObject.AddComponent<LODGroup>();
         var lodLevels = new LOD[lods.Count];
         foreach (var lod in lods)
         {
             float lodSize = (float)Math.Round(1d / lods.Count, 1);
             Debug.Log("LOG -> Size=" + lodSize);
             Renderer[] renderers = lod.Value.GetComponentsInC$$anonymous$$ldren<Renderer>();
             if (renderers == null || renderers.Length == 0)
             {
                 Debug.LogWarning("WARNING -> No Renderers for LOD level " + lod.Key + " found!");
             }
             lod.Value.transform.SetParent(lodObject.transform);
             LOD lodLevel = new LOD(lodSize, renderers);
             lodLevels[lod.Key] = lodLevel;
         }
         lodGroup.SetLODs(lodLevels); // <!!> The exception is raised from calling t$$anonymous$$s method! <!!>
     }

I suspect that the exception is raised by a "bad" screenRelativeTransitionHeight (Documentation: http://docs.unity3d.com/). However I have tried out multiple values, all without success. I am kinda stuck here, so Id really appreciate any help possible c:

Thanks, 7BiT.psycho

Btw: The script is being executed in the editor.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jmgek · Oct 16, 2017 at 02:57 AM 0
Share

Did you ever get an answer?

avatar image theLittleSettler · Oct 16, 2017 at 07:39 AM 0
Share

Perhaps you could just simulate the editor's script for creating them? or have a look through the script to see https://github.com/MattRix/UnityDecompiled/blob/master/UnityEditor/UnityEditor/LODGroupEditor.cs (but you should probably decompile the current version of UnityEditor.dll, rather than whatever version that is)

And if you do have it set up, please update as I'd find it pretty convenient myself...

Edit: in order to actually do that, you might have to copy quite a bit of unity's codebase, though

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CatyRaiu · May 21, 2020 at 10:05 AM

It worked for me!

 LODGroup group = LOD_Parent.GetComponent<LODGroup>();
 
         LOD[] lod_s_exi = group.GetLODs();
         for ( int i = 0; i < lod_s.Count; i++ )
         {
             MeshRenderer[] renderers = lod_s[i].GetComponentsInC$$anonymous$$ldren<MeshRenderer>();
             if ( renderers == null || renderers.Length == 0 )
             {
                 print("No rendererFinded");
             }
             LOD lod = new LOD(lod_s_exi[i].screenRelativeTransitionHeight, renderers);
             lod_s_exi[i] = lod;
 
         }
         
         group.SetLODs(lod_s_exi);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CatyRaiu · May 20, 2020 at 06:17 PM 0
Share

And BTW ! Thanks for your post! Helped me a lot because in this matter i was like a tree....Tkx again!

avatar image ZFMartinGebske · Mar 21, 2022 at 01:09 PM 0
Share

Hey @CatyRaiu ,

sorry I don't get this here. (Maybe I'm overseeing the elephant in the room). I'm not THAT famliar with setting LDS programmaticly but here's my Code (extracted from a plugin). Could you please tell me what's wrong with it because I get the exact same error, like @a7BiT-psycho

     void SetupLods(GameObject gameObject)
     {
         // Set up LODs
         foreach (MathWorks.LodImportData lodImportData in gameObject.GetComponentsInChildren<MathWorks.LodImportData>())
         {
             GameObject lodGameObject = lodImportData.gameObject;
             int numThresholds = lodImportData.LodThresholds.Count;
 
             LODGroup lodGroup = lodGameObject.AddComponent<LODGroup>();
             LOD[] lods = new LOD[numThresholds];
 
             for (int j = 0; j < numThresholds; j++)
             {
                 float threshold = lodImportData.LodThresholds[j] / 100.0f; // Convert from percentage
                 Renderer[] renderers = lodGameObject.transform.GetChild(j).GetComponentsInChildren<Renderer>();
 
                 lods[j] = new LOD(threshold, renderers);
             }
 
             lodGroup.SetLODs(lods);
             lodGroup.RecalculateBounds();
 
             // Delete temp data
             UnityEngine.Object.DestroyImmediate(lodImportData);
         }
     }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to switch animation's avatar by active LOD? 0 Answers

LOD Popping in when objects placed via terrain painter 0 Answers

LOD only working in scene view 1 Answer

Is it normal to have one drawcall PER levels of detail when using LODGroup? 0 Answers

Assign/add gameobject to LOD via script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges