Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Meceka · Mar 25, 2016 at 04:48 PM · lodlodgrouponbecameinvisibleonbecamevisible

OnBecameVisible and OnBecameInvisible with LOD group.

I am wishing to use OnBecameVisible and OnBecameInvisible for some gameobjects. For ordinary objects with a mesh renderer, my script works fine. But for objects with a LOD group, these functions doesn't get called on the parent object. It only works if script is added for child gameobjects (i.e. *_LOD1 gameobject).

This causes wrong calls of OnBecameVisible/OnBecameInvisible when lod group switches between lod groups.

Is there a better way to understand that a gameobject with lod group is or isn't visible on screen?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by twobob · Apr 15, 2017 at 03:57 AM

Setup some refs

     private Transform PolledForVisibilty;
 
     private Renderer RendererPolledForVisibilty;

     public float TEST_PERIOD = .3f;

Shove some stuff in start

 if (gameObject.GetComponent<LODGroup>() != null) {

             PolledForVisibilty = gameObject.GetComponent<LODGroup> ().transform;
             InvokeRepeating ("SlowlyPollTheVisibilityOfLOD", 1f, TEST_PERIOD);
 
         } else
         
             if (gameObject.GetComponent<SkinnedMeshRenderer>() != null) {

             // Do nothing. We handle this with OnBecomeVisible etc.
             
             } else
         {

             RendererPolledForVisibilty = gameObject.transform.GetComponentInChildren<Renderer> ();  
                 InvokeRepeating ("SlowlyPollTheVisibilityOfSingleRenderer", 1f, TEST_PERIOD);
         }

and they call in the main body

     void OnBecameInvisible()
     {
         //Debug.Log (gameObject.name + " invisible");
     }
     void OnBecameVisible()
     {  
         //Debug.Log (gameObject.name + " visible");
     }

via the invokes

     void SlowlyPollTheVisibilityOfLOD()
     {
         bool vis = false;
 
         foreach (Transform child in PolledForVisibilty)
             {
                 var renderer = child.GetComponent<Renderer> ();
                 if (renderer != null && renderer.isVisible)
                 { 
                     vis = true;
                 }
             }
 
             if (vis) {OnBecameVisible ();} else {OnBecameInvisible ();}
 
     }
 
 
     void SlowlyPollTheVisibilityOfSingleRenderer()
     {

         bool vis = false;  
 
         if (RendererPolledForVisibilty != null && RendererPolledForVisibilty.isVisible)
             { 
                 vis = true;
             }
   
         if (vis) {OnBecameVisible ();} else {OnBecameInvisible ();}
 
     }

Tweak period of .3f to your needs. I came up with that mess to get around the various possible things I might end up attaching this to.

Seems to work fine. Hope it helps

Comment
Add comment · Show 1 · 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 twobob · Apr 15, 2017 at 04:09 AM 0
Share

an obvious improvement would be to take a a copy of the 'vis' value and simply do a compare to not keep triggering the events needlessly when no change has occurred.

Another would be to find a way to attach to the existent OnVisibilty methods of the correct subrenderer from this scrip based on deriving the taxonomy (via a similar method to this - or this - first) - that seems doable but beyond my knowledge.

avatar image
0

Answer by JimPD · Sep 12, 2017 at 10:48 AM

Don't do the above. You can get individual renderers from an array

 /// <summary>
     /// Determines if a renderer that is part of a LOD Group is visible, if so place UI object over it
     /// </summary>
     /// <param name="UIObject"></param>
     /// <param name="_gameObject"></param>
     public void UIFollowGameObject(RectTransform UIObject, GameObject _gameObject)
     {
         // Get LOD Group on your gameObject
         LODGroup lodGroup = _gameObject.transform.root.GetComponent<LODGroup>();
         // If the GameObject has a LODGroup
         if (lodGroup != null)
         {
             // Get an array of LODS from that group
             LOD[] _lods = lodGroup.GetLODs();
             // If the LODGroup has LODS
             if (_lods != null)
             {
                 // Get the first Renderer within that LOD group
                 Renderer _lodRenderer = _lods[0].renderers[0];
                 // if the LOD has a renderer
                 if (_lodRenderer != null)
                 {
                     if (_lodRenderer.isVisible)
                     {
                         // The renderer is visible
                         // I set a UI element to be placed over the top of it
                         Vector3 screenPoint = RectTransformUtility.WorldToScreenPoint(player.GetComponent<Camera>(), _gameObject.transform.position);
                         UIObject.gameObject.SetActive(true);
                         UIObject.position = screenPoint;
                     }
                     else
                     {
                         // The renderer is not visible 
                         UIObject.gameObject.SetActive(false);
                         return;
                     }
                 }
                 else
                 {
                     // The LOD Does not has a renderer assigned
                     return;
                 }
             }
             else
             {
                 // The LOD Group has not had any LODs assigned
                 return;
             }
         }
         else
         {
             // The GameObject hasnt a LOD Group assigned
             return;
         }
     }

https://docs.unity3d.com/ScriptReference/LOD-renderers.html

Comment
Add comment · 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

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

39 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 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

Terrain Trees LOD working in reverse 1 Answer

Implement a LOD System similar to the Unity LOD 0 Answers

Search and remove LODs from LODGroup through script 0 Answers

Creating LOD Group for character 4 Answers

Terrain LOD? 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