Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 DanRad1287 · Mar 26, 2014 at 08:17 PM · issue

How do you add a mesh to a LOD Level in Code?

This is probably a noob question but I thought I should ask it anyway since I can't figure it out. Does anyone know how to add a mesh into a LOD:0 level in Code? I create a empty game Object and put a LODGroup Component on with just a LOD:0 level and a Culled Level. I want to put a certain mesh in the LOD:0 level but I really don't know how to do that in code.

void OnGUI () {

         if (!selectionDetected)
         {
             OnSelectionChange();
             
             selectionDetected = true;
         }
         
         if (meshes.Count > 0)
         {
             var selectedMeshesText = string.Format("{0} selected mesh{1}: {2}{3}",
                                                    meshes.Count,
                                                    meshes.Count > 1 ? "es" : "",
                                                    string.Join (", ", meshes.Take(25).Select(x => x.gameObject.name).ToArray()),
                                                    meshes.Count > 25 ? "..." : "");
             
             GUILayout.Label(selectedMeshesText, GetExpandingTextStyle(), GUILayout.ExpandWidth(true));
             
             distanceFromTerrain = EditorGUILayout.Slider ("Distance From Terrain", distanceFromTerrain, distanceMin, distanceMax);
 
             GUILayout.Label("New Building LOD Model:", new GUILayoutOption[]{});
             prefab = (GameObject)EditorGUILayout.ObjectField(prefab, typeof(GameObject), false, new GUILayoutOption[]{});
 
 
             if (GUILayout.Button(string.Format("Create Building LOD{0}", meshes.Count > 1 ? "s" : "")))
             {
                 processedMeshes = 0;
                 
                 try
                 {
                     foreach (var mesh in meshes)
                     {
                         //Bring Old Models down to Terrain
                         ConformMeshToTerrain(mesh);
                         ++processedMeshes;
 
                         //store gameObject reference
                         GameObject objToSpawn;
 
                         //Gather Selected Building's Name
                         var objName = mesh.name;
 
                         //Gather Selected Building's Position XYZ Values
                         var objPOS = mesh.transform.position;
 
                         //Gather Selected Building's Rotation XYZ Values
                         var objROT = mesh.transform.localEulerAngles;
 
                         //Gather Selected Building's Scale XYZ Values
                         var objSCAL = mesh.transform.localScale;
 
                         //spawn object
                         objToSpawn = new GameObject(objName);
 
                         //Add Components
                         group = objToSpawn.AddComponent<LODGroup>();
 
                         //Grabbing the LOD Component on New Empty Game Object
                         LODGroup objLodCmp = objToSpawn.GetComponent<LODGroup>();
 
                         //Testing to see if the New Game Object has a LODGroup Component
                         if (objLodCmp != null)
                         {
                             SerializedObject obj = new SerializedObject(objLodCmp);
 
                             //Creating a Variable that will allow me to access the data properties of the LODGroup Component
                             SerializedProperty prop2 = obj.FindProperty("m_LODs");
 
                             //Deleting the LOD:1 & LOD:2 from the Array
                             prop2.DeleteArrayElementAtIndex(1);
                             prop2.DeleteArrayElementAtIndex(0);
 
                             //This is Crticial for the above changes to take effect.  
                             obj.ApplyModifiedProperties();
                         }
 
                         //Apply Position, Rotation and Scale to new GameObject
                         objToSpawn.transform.position = new Vector3(objPOS.x,objPOS.y,objPOS.z);
                         objToSpawn.transform.localEulerAngles = new Vector3(objROT.x,objROT.y,objROT.z);
                         objToSpawn.transform.localScale = new Vector3(objSCAL.x,objSCAL.y,objSCAL.z);
 
                         if(prefab != null)
                         {
                             Debug.Log("This is working!");
                         }
 
                         //DestroyImmediate(mesh.gameObject);
                     }
                 }
                 finally
                 {
                     EditorUtility.ClearProgressBar();
                 }
             }
         }
         else
         {
             GUILayout.Label("No segments selected.", GetExpandingTextStyle(), GUILayout.ExpandWidth(true));
         }
 
         Repaint();
     }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

20 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script Efficiency 1 Answer

Does Scripting Jump exist? (How To use/write goto) 3 Answers

Adjust Cone in a Shuriken Particle System 1 Answer

Get Variable From A Script Attached To a Prefab 3 Answers


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