- Home /
How to track position/rotation/scale changes in selection groups?
Hi,
I have a simple Editor script that tracks single objects at OnInspectorGUI. Basically I am forcing to move these specific type of GameObjects in a grid based in their Renderer bounds size (grid alignment).
Regardless of the effect/behaviour I want to achieve. My problems start at the moment of moving a selection of these GameObjects... When I try to move several objects (with the same Monobehaviour component attached) my Editor Inspector script fails to keep track of these changes! Only after selecting one by one the aligner script does it's magic as inteded.
Screenshot of me moving a single selected object with desired alignment results:
Screenshot of me moving a selection group without the editor alignment script working:
Editor Script:
using UnityEditor;
[CustomEditor(typeof(StageBlock))]
public class StageBlockInspector : Editor
{
private StageBlock block;
public override void OnInspectorGUI()
{
block = target as StageBlock;
block.SnapCoords();
block.ForceScale();
block.ForceRotation();
}
}
Monobehaviour for individual block GameObjects:
using UnityEngine;
public class StageBlock : MonoBehaviour
{
private Vector3 _coords;
private float _xVal;
private float _zVal;
private float _w;
private float _h;
public void SnapCoords()
{
// Getting renderer bounds' size. Get info once
if (_w == 0f && _h == 0f)
{
Renderer renderer = GetComponent<Renderer>();
_w = renderer.bounds.extents.x * 2f;
_h = renderer.bounds.extents.z * 2f;
}
_coords = transform.position;
_xVal = _coords.x;
_xVal = Mathf.Round(_xVal / _w) * _w;
_zVal = _coords.z;
_zVal = Mathf.Round(_zVal / _h) * _h;
_coords.x = _xVal;
_coords.y = 0f;
_coords.z = _zVal;
transform.position = _coords;
}
public void ForceScale()
{
transform.localScale = new Vector3(1f, 1f, 1f);
}
public void ForceRotation()
{
transform.rotation = Quaternion.identity;
}
}
In case you want to try it:
block.aligner.unitypackage
Please, is there a way to have individual Editor OnInspectorGUI events while modifying selection groups?
....Please please! :(
Answer by Smireles · Dec 04, 2017 at 06:56 PM
After asking help in person to a friend of mine. He tweaked a little the Editor script.
He added CanEditMultipleObjects attribute to the class; and changed target to targets to support multi object selection.
using UnityEditor;
[CustomEditor(typeof(StageBlock))]
[CanEditMultipleObjects]
public class StageBlockInspector : Editor
{
private StageBlock block;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
for (var i = 0; i < targets.Length; i++)
{
block = targets[i] as StageBlock;
block.SnapCoords();
block.ForceScale();
block.ForceRotation();
}
}
}
And that was it. Happy ending! I hope this helps someone else in the future...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Sprite Fade In/Out Not Working 1 Answer
After compiling game lags 0 Answers
Programmmed Animation (making transform move to vectors with code) 0 Answers