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

Bug Handles.PositionHandle not storing position when snapping

Discussion in 'Editor & General Support' started by CoolJosh3k, Jan 15, 2021.

  1. CoolJosh3k

    CoolJosh3k

    Joined:
    Dec 3, 2016
    Posts:
    145
    I've come accross what appear to be an inconsistant bug with position handles.

    When using snapping (either grid or action key), entering play mode reverts the position handle back to its previous serialized value.

    If values are entered manually or handle is moved without snapping, then there is no issue.

    I have tested on a game object with no other components attached and no parents, but the issue still occasionally occurs.

    Here is my code I am using:


    Under Assets/Editor

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. [CustomEditor(typeof(PositionHandle)), CanEditMultipleObjects]
    7. public class PositionHandleEditor : Editor
    8. {
    9.     protected virtual void OnSceneGUI()
    10.     {
    11.         //if (Application.isPlaying) {
    12.         //    return;
    13.         //}
    14.      
    15.         PositionHandle positionHandle = (PositionHandle)target;
    16.      
    17.         EditorGUI.BeginChangeCheck();
    18.      
    19.         Vector3 offset = positionHandle.transform.position;
    20.      
    21.         if (positionHandle.OptionalReferenceGameObject != null) {
    22.             offset = positionHandle.OptionalReferenceGameObject.transform.position;
    23.         }
    24.      
    25.         if (positionHandle.OptionalReferencePositionHandle != null) {
    26.             offset = positionHandle.OptionalReferencePositionHandle.TargetPosition;
    27.         }
    28.      
    29.         Vector3 position = Handles.PositionHandle(positionHandle.TargetLocalPosition + offset, Quaternion.identity) - offset;
    30.      
    31.         if (EditorGUI.EndChangeCheck())
    32.         {
    33.             Undo.RecordObject(positionHandle, "Change Position");
    34.          
    35.             positionHandle.TargetLocalPosition = position;
    36.         }
    37.     }
    38. }
    39.  

    Under Assets/Scripts

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [ExecuteInEditMode]
    6. public class PositionHandle : MonoBehaviour
    7. {
    8.     public GameObject OptionalReferenceGameObject = null;
    9.     public PositionHandle OptionalReferencePositionHandle = null;
    10.  
    11.     [SerializeField] private Vector3 targetLocalPosition = new Vector3(1f, 1f, 0f);
    12.  
    13.     public Vector3 TargetLocalPosition {
    14.         get {
    15.             return targetLocalPosition;
    16.         }
    17.         set {
    18.             //if (!Application.isPlaying) {
    19.                 targetLocalPosition = value;
    20.             //}
    21.         }
    22.     }
    23.  
    24.     public Vector3 TargetPosition {
    25.         get {
    26.             return transform.TransformPoint(targetLocalPosition);
    27.         }
    28.         set {
    29.             //if (!Application.isPlaying) {
    30.                 targetLocalPosition = transform.InverseTransformPoint(value);
    31.             //}
    32.         }
    33.     }
    34. }
    35.  
    Tested with Unity versions 2019.4.12f1 and 2019.4.17f1.

    Any help or advice is appreciated.