Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question 3D object inside scroll rect/view?

Discussion in 'UGUI & TextMesh Pro' started by peq42, Oct 11, 2022.

  1. peq42

    peq42

    Joined:
    Mar 4, 2022
    Posts:
    63
    Is it possible to add a 3D object and have it not show when outside of its area just like the rest of the 2D UI? Because by the looks of it, anything 3D will simply ignore it



     
  2. peq42

    peq42

    Joined:
    Mar 4, 2022
    Posts:
    63
    Found a workaround for now, but its not exactly what I wanted


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class hidemesh : MonoBehaviour
    7. {
    8.     public RectTransform scrollViewViewport; //Invisible 2D object that would already hide inside scroll rect/view
    9.     public GameObject go3dMesh; //Mesh I want to hide
    10.  
    11.    
    12.     public static float GetGUIElementOffset(RectTransform rect) //checks if 2D object is hidden
    13.     {
    14.         Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height);
    15.         Vector3[] objectCorners = new Vector3[4];
    16.         rect.GetWorldCorners(objectCorners);
    17.  
    18.         var ynew = 0f;
    19.         for (int i = 0; i < objectCorners.Length; i++)
    20.         {
    21.          
    22.             if (objectCorners[i].y-1.2f < screenBounds.yMin)
    23.             {
    24.                 ynew = screenBounds.yMin - objectCorners[i].y+1.2f;
    25.             }
    26.             if (objectCorners[i].y+1.2f > screenBounds.yMax)
    27.             {
    28.                 ynew = screenBounds.yMax - objectCorners[i].y-1.2f;
    29.             }
    30.         }
    31.         return ynew;
    32.     }
    33.     private void Update()
    34.     {
    35.      
    36.             float temp=GetGUIElementOffset(scrollViewViewport);
    37.             go3dMesh.SetActive((temp>0 && temp < 6.5f)); //sets mesh invisible if 2D object is supposed to be hidden
    38.          
    39.          
    40.     }
    41. }