Level Object Placement Limitation

Documentation for Unity Asset Store version v1.20

Summary

It is possible to limit the area in which objects can be placed. It is also possible to set other criteria, not only positon. For example it is possible to prevent houses from being built on a very steep terrain. To limit the object placement you need to register for the on drag event of the object editor. This event will be raised every frame when there is an object dragged over something in the level. A test if the current location suits the defined criteria should be performed in the event handler implementation. If the location is not suitable you can optionally display an explanatory message to the user, which will be shown near the object icon.

Step 1: Event Registration

Register for the 'LE_EventInterface.OnObjectDragged' event. This event will be called in every frame in which a user will try to place an object by dragging it to the right position. Keep in mind that you also should unregister the event handler when your script gets destroyed otherwise a memory leak could occur. using LE_LevelEditor.Events;

// Register for the load event, which is called when the level is loaded

LE_EventInterface.OnObjectDragged += OnObjectDragged;

Step 2: Event Handling

The event handler below will check if the current location is suitable for the object to be placed on. If this is not the case the 'p_args.IsObjectPlaceable' property will be set to false. Optionally, a message can be shown to the player so that he knows why the object cannot be placed at the certain location. private void OnObjectDragged(object p_sender, LE_ObjectDragEvent p_args)
{

    // in this example we will check if the cursor position (the place where the object will be placed)
    // is too far away from the center of the level (outside a square with side length 200)
    // you can replace this with the check that you need to perform
    // take a look at the other members of LE_ObjectDragEvent
    // for example the object prefab is also passed within the event args

    if (Mathf.Abs(p_args.CursorHitInfo.point.x) > 100 ||
        Mathf.Abs(p_args.CursorHitInfo.point.z) > 100)
    {

       // tell the level editor that this object cannot be placed at the current cursor position

       p_args.IsObjectPlaceable = false;

       // check if there is already a message that will be shown to the player
       // this can be the case if some other listener of this event has added a message
       // or if the instance count of this objects has reached its maximum

       if (p_args.Message.Length > 0)
       {

          // add a line break if the message is not empty

          p_args.Message += "\n";
       }

       // add your message here in this example the object is simply out of bounds

       p_args.Message += "Object is out of bounds!";
    }
};