Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 simon56 · Oct 03, 2021 at 10:46 PM · uimeshvertex

Make Vertices selectable and shown as UI

Hi,
I have a setup where I have a mesh which has editable vertices directly in game. Each vertex's position on screen is calculated in OnGUI() to display a GUI.Button that makes it selectable. Selection can also be made through a marquee selection, but the rest does not matter.

The real problem is - you guessed it - that the GUI is painstakingly slow and makes the whole thing super laggy. I wondered how I could get it done more efficiently - maybe through the more recent UI system, though I'd prefer not to for legitimate and uselessly precise reasons - or otherwise.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Oct 04, 2021 at 06:12 AM

Creating thousands of UI elements to represent all the mesh vertices just to make them selectable is a bad idea. Full stop.

Drop it and move forward with something much more simple, reliable and performant. The very first thing you should try is to just calculate vertex selection 100% manually.

How to do that? Just iterate over all vertices and look for Ray & sphere or screen point & Rect intersections on mouse click. If performance starts to be the issue for some reason - no worries, there is plenty of space for optimizations here (AABB sets, hierarchies etc).


link text


 using UnityEngine;
 
 [RequireComponent( typeof(MeshFilter) , typeof(MeshRenderer) )]
 public class LetsSelectMeshVertices : MonoBehaviour
 {
     
     [SerializeField] Camera _camera;
     [SerializeField] float _hotSpotSize = 30f;
     [SerializeField] int _selectedVertexIndex = -1;
     Vector3[] _vertices;
 
     void Start ()
     {
         _vertices = GetComponent<MeshFilter>().sharedMesh.vertices;
     }
     void OnBecameVisible () => enabled = true;
     void OnBecameInvisible() => enabled = false;
     void Update ()
     {
         if( Input.GetMouseButtonDown(0) )
         {
             Vector2 mousePosition = Input.mousePosition;
             Matrix4x4 ltw = transform.localToWorldMatrix;
             for( int vertexIndex=_vertices.Length-1 ; vertexIndex!=-1 ; vertexIndex-- )
             {
                 Vector3 vertexWorldPos = ltw.MultiplyPoint3x4( _vertices[vertexIndex] );
                 Vector3 vertexScreenPos = _camera.WorldToScreenPoint( vertexWorldPos );
                 Rect vertexScreenRect = new Rect( vertexScreenPos.x-_hotSpotSize*0.5f , vertexScreenPos.y-_hotSpotSize*0.5f , _hotSpotSize , _hotSpotSize );
                 if( vertexScreenRect.Contains(mousePosition) )
                 {
                     _selectedVertexIndex = vertexIndex;
                     return;
                 }
             }
             _selectedVertexIndex = -1;
         }
     }
     #if UNITY_EDITOR
     void OnDrawGizmos ()
     {
         Matrix4x4 ltw = transform.localToWorldMatrix;
         
         if( _vertices!=null )
         for( int vertexIndex=_vertices.Length-1 ; vertexIndex!=-1 ; vertexIndex-- )
         {
             Vector3 vertexWorldPos = ltw.MultiplyPoint3x4( _vertices[vertexIndex] );
             Gizmos.color = Color.blue;
             Gizmos.DrawSphere( vertexWorldPos , 0.05f );
         }
 
         if( _selectedVertexIndex!=-1 )
         {
             Gizmos.color = Color.red;
             Gizmos.DrawSphere( ltw.MultiplyPoint3x4(_vertices[_selectedVertexIndex]) , 0.05f );
         }
     }
     #endif
 
 }
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

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

UnityEngine.UI.Text characters mesh 0 Answers

How to resize mesh to fbx model gameobject? 1 Answer

Positioning of Text vertices when text is alligned 0 Answers

Vertex colours not blending across triangle face 1 Answer

How to create a 2D mesh from a vertex array? 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