Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rect Tool snap editor extension

Discussion in 'Scripting' started by diegzumillo, Aug 16, 2019.

  1. diegzumillo

    diegzumillo

    Joined:
    Jul 26, 2010
    Posts:
    418
    Hey there

    I'm trying to get some snap functionality in rect tool so I can use with sprites using that 9-slice thing. The tricky thing about rect tool is that it's not just snapping the transform position, the snap is at the borders. I made a simple editor extension of sprite renderer that snaps height and width to nearest integers, and with a Grid component in a parent gameobject snapping the position of the sprite the whole thing almost works perfectly. The only inconvenience is that after changing dimensions of the sprite with the rect tool (which now snaps to integer values) it loses its alignment with the grid until I move the sprite a little manually.

    Ideally there would be some way to flag the transform as just moved, so the Grid component does its job as soon as I change the dimensions. But how? I scoured the docs searching for anything that could achieve that in the Grid class, in the Move Tool etc. If that's not possible, can anyone recommend an alternative?
     
  2. diegzumillo

    diegzumillo

    Joined:
    Jul 26, 2010
    Posts:
    418
    Nevermind, I was being mentally lazy to realize the position snap is trivial and I don't need a grid. If anyone's interested in a rect snap functionality for your sprites here is my ugly ass code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(SpriteRenderer))]
    7. public class SnapForSpriteRenderer : Editor
    8. {
    9.     SpriteRenderer t;
    10.     Event e;
    11.     void OnEnable()
    12.     {
    13.         t = target as SpriteRenderer;
    14.     }
    15.  
    16.     private void OnSceneGUI(){
    17.         e = Event.current;
    18.                
    19.         if(e.type == EventType.MouseUp){
    20.            
    21.             t.size = new Vector2(Mathf.Round(t.size.x), Mathf.Round(t.size.y));
    22.            
    23.             t.transform.position = new Vector3( Mathf.Round(2*t.transform.position.x)/2,
    24.                                                 Mathf.Round(2*t.transform.position.y)/2, t.transform.position.z);
    25.             EditorUtility.SetDirty(t.transform); //don't know if I need this
    26.         }
    27.     }
    28.  
    29.     public override void OnInspectorGUI()
    30.     {
    31.         DrawDefaultInspector();
    32.         //maybe add some editable parameters to improve snap
    33.     }
    34. }
    35.  
    This is mega simple, but could be expanded for different grid sizes and all that. I am keeping my project as simple as possible by using a grid of size one everywhere.