It is possible to establish restrictions in terms of what or how objects can be transformed by the gizmos. We have alredy discussed one way of preventing objects from being transformed (see IRTTransformGizmoListener), but the following sections will discuss some other ways in which restrictions can be applied.

The ideas discussed in the following sections apply to the following gizmos:

  • Move Gizmo;
  • Rotation Gizmo;
  • Scale Gizmo;
  • Universal Gizmo;

The ObjectTransformGizmoSettings Class

The RTObjectSelectionGizmos class has 4 properties that return an instance of the ObjectTransformGizmoSettings class: one for each gizmos mlisted above.

Those properties can be accessed like so:

ObjectTransformGizmoSettings moveGizmoSettings = RTObjectSelectionGizmos.Get.ObjectMoveGizmoSettings;
ObjectTransformGizmoSettings rotationGizmoSettings = RTObjectSelectionGizmos.Get.ObjectRotationGizmoSettings;
ObjectTransformGizmoSettings scaleGizmoSettings = RTObjectSelectionGizmos.Get.ObjectScaleGizmoSettings;
ObjectTransformGizmoSettings universalGizmoSettings = RTObjectSelectionGizmos.Get.ObjectUniversalGizmoSettings;

The public interface for this class looks like so:

public class ObjectTransformGizmoSettings
{
    public int TransformableLayers { get; set; }

    public bool IsLayerTransformable(int objectLayer);
    public void SetLayerTransformable(int objectLayer, bool isTransformable);
    public bool IsObjectTransformable(GameObject gameObject);
    public void SetObjectTransformable(GameObject gameObject, bool isTransformable);
    public void SetObjectCollectionTransformable(List<GameObject> gameObjectCollection, bool areTransformable);
}

So let's assume that you would like to have the move gizmo transform objects that belong ONLY to the Default layer. All other layers should be ignored. You could write the following code to accomplish this:

ObjectTransformGizmoSettings moveGizmoSettings = RTObjectSelectionGizmos.Get.ObjectMoveGizmoSettings;
moveGizmoSettings.TransformableLayers = 0x1;    // Set only first bit, clear the rest 

Or you could just specify the layers that you want to be transformed or ignored like so:

// Transform the Default layer, but ignore the Water layer
moveGizmoSettings.SetLayerTransformable(LayerMask.NameToLayer("Default"), true);
moveGizmoSettings.SetLayerTransformable(LayerMask.NameToLayer("Water"), false);

You can also prevent specific objects from being transformed:

// Tell the move gizmo that we don't want to transform the game object 'cubeObject'. Same for
// all the sphere objects which are stored in the 'sphereObjects' collection.
moveGizmoSettings.SetObjectTransformable(cubeObject, false);
moveGizmoSettings.SetObjectCollectionTransformable(sphereObjects, false);

The ObjectTransformGizmo.ObjectRestrictions Class

The ObjectTransformGizmo class defines a nested class called ObjectRestrictions. This can be used to regtister different types of restrictions that apply to a specific object. For example, you can use this class to prevent an obejct from being moved along the Y axis. Or you can use it to make an object immune to different gizmo handles such that when those handles are dragged, the output produced by them will not be applied to the object in question.

For example, let's assume that you have a game object in the scene that you never want to be moved along the Z axis. Maybe it is a sprite and you always want it to sit in the XY plane. You could write the following code to accomplish this:

// Prevent movement along the Z axis
ObjectTransformGizmo.ObjectRestrictions restrictions = new ObjectTransformGizmo.ObjectRestrictions();
restrictions.SetCanMoveAlongAxis(2, false);     

// Register restrictions
Gizmo moveGizmo = RTObjectSelectionGizmos.Get.GetGizmoById(ObjectSelectionGizmoId.MoveGizmo);
moveGizmo.ObjectTransformGizmo.RegisterObjectRestrictions(spriteGameObject, restrictions);

From this point on, the Z coordinate of the sprite object will not be touched by the move gizmo. So for example, if the move gizmo drag output is an offset vector that looks like this: <1.0f, 2.0f, 5.0f>, the sprite object position will only be affected by the X and Y coordinates: <1.0f, 2.0f, 0.0f>.

Note

The SetCanMoveAlongAxis function accepts 2 parameters. The first one is the index of the axis. This is 0 for X, 1 for Y and 2 for Z. The second parameter is true or false depending on whether or not the object is allowed to be moved along that axis.

You could do the same for the scale gizmo:

// Prevent scaling along the Y axis
ObjectTransformGizmo.ObjectRestrictions restrictions = new ObjectTransformGizmo.ObjectRestrictions();
restrictions.SetCanScaleAlongAxis(1, false);    

// Register restrictions
Gizmo scaleGizmo = RTObjectSelectionGizmos.Get.GetGizmoById(ObjectSelectionGizmoId.ScaleGizmo);
scaleGizmo.ObjectTransformGizmo.RegisterObjectRestrictions(someGameObject, restrictions);

Note

In this case the SetCanScaleAlongAxis function was used.

You can also use an overload for the RegisterObjectRestrictions function to register the same restriction for multiple objects at once:

Gizmo scaleGizmo = RTObjectSelectionGizmos.Get.GetGizmoById(ObjectSelectionGizmoId.ScaleGizmo);

// Build the list of obejcts which are subject to the restrictions and call RegisterObjectRestrictions
List<GameObject> objectCollection = /* ... */;
scaleGizmo.ObjectTransformGizmo.RegisterObjectRestrictions(objectCollection, restrictions);

With rotation, things are a bit different. There is no SetCanRotateAroundAxis function. Instead, you have to specify a handle that is not allowed to affect the object. For example, let's say that we have an object in the scene and we never want to allow it to be rotated by the rotation gizmo's X axis slider and its middle cap (i.e. the middle sphere that rotates around the camera X and Y axes):

// Prevent object from being rotated by the 2 handles
ObjectTransformGizmo.ObjectRestrictions restrictions = new ObjectTransformGizmo.ObjectRestrictions();
restrictions.SetIsAffectedByHandle(GizmoHandleId.XRotationSlider, false);
restrictions.SetIsAffectedByHandle(GizmoHandleId.CamXYRotation, false);

// Register restrictions
Gizmo rotationGizmo = RTObjectSelectionGizmos.Get.GetGizmoById(ObjectSelectionGizmoId.RotationGizmo);
rotationGizmo.ObjectTransformGizmo.RegisterObjectRestrictions(someGameObject, restrictions);

Please see Gizmo Handle Ids for more info about gizmo handle ids.