订阅此事件以处理场景视图中的对象放置。
当从层次结构和项目视图拖放对象时,PlaceObject会调用此委托。
using UnityEditor; using UnityEngine;
static class PlaceObjectExample { static readonly Plane s_GroundPlane = new Plane(Vector3.up, Vector3.zero);
[InitializeOnLoadMethod] static void InitPlaceObjectHandler() { HandleUtility.placeObjectCustomPasses += PlaneRaycast; }
// In this example, we register a plane at the scene origin to test for object placement. static bool PlaneRaycast(Vector2 mousePosition, out Vector3 position, out Vector3 normal) { Ray worldRay = HandleUtility.GUIPointToWorldRay(mousePosition); float distance;
if (s_GroundPlane.Raycast(worldRay, out distance)) { position = worldRay.GetPoint(distance); normal = s_GroundPlane.normal; return true; }
position = Vector3.zero; normal = Vector3.up; return false; } }