Author Topic: Code to check if sprite is within screen bounds (3D UI)  (Read 4205 times)

toyob

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 7
    • View Profile
Code to check if sprite is within screen bounds (3D UI)
« on: July 18, 2017, 07:58:27 AM »
Hi ArenMook,

I have to keep a lot of live UISprites during the game, and most of them are situated off-screen.

I am looking for a way to improve performance, by putting a script onto each UISprite containing object. Idea is to disable the UISprite when it's off-screen (completely) and re-enable when some portion of a sprite becomes visible within the screen bounds. (Maybe there is a way to do it through the parent panel? I haven't found)

I tried many solutions but none of them worked. Please help! I am dumb and looking forward to some working code. I know it's a piece of cake for you. Thanks.

This is what I currently have (doesn't work, sets Visible all times)

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CheckSpriteVisibility : MonoBehaviour {
  6.  
  7.         public UISprite thisSprite;
  8.        
  9.         void Start () {
  10.                
  11.                 thisSprite = this.GetComponent<UISprite>();
  12.                
  13.                 cam = Camera.main;
  14.                 planes = GeometryUtility.CalculateFrustumPlanes(cam);
  15.                
  16.         }
  17.        
  18.         public const bool t = true;
  19.         public const bool f = false;
  20.        
  21.         public Bounds b;
  22.        
  23.         Camera cam;
  24.         Plane[] planes;
  25.        
  26.         public bool Visible;
  27.        
  28.         void Update () {
  29.                
  30.                 b = NGUIMath.CalculateRelativeWidgetBounds(this.transform);
  31.                
  32.          if (GeometryUtility.TestPlanesAABB(planes, b))
  33.                  Visible = true;
  34.          else
  35.                  Visible = false;
  36.        
  37.                
  38.         }
  39. }
  40.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Code to check if sprite is within screen bounds (3D UI)
« Reply #1 on: July 22, 2017, 02:41:52 PM »
NGUI has this built in with clipped panels (used for scroll views). Enable clipping on the panel and set the panel's dimensions to the entire screen's dimensions. That said, I don't really see what you will get out of this. Things that are off-screen don't do anything to affect the fillrate as the triangles get culled by the GPU.

Last but not least, the easiest thing to do is to handle it manually -- you know when you move something off-screen (usually an entire window = panel), when doing so you can just disable its game object until you need it. The free Starlink UI kit on the asset store has a window management system built-in that handles showing one window at a time and keeps track of navigation for things like "go back" functionality that you may also find useful.