版本: Unity 6 (6000.0)
语言English
  • C#

TerrainPaintToolWithOverlays<T0>

UnityEditor.TerrainTools 中的类

/

继承自:TerrainTools.TerrainPaintToolWithOverlaysBase

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法采纳所有提交,但我们确实会阅读用户提出的每项更改建议,并在适用情况下进行更新。

关闭

提交失败

由于某些原因,您的更改建议无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

描述

地形绘画工具的基类,继承自编辑器工具。

从该类派生以实现您自己的地形绘画工具,这些工具也会显示在地形工具覆盖层中。

using UnityEngine;
using UnityEditor;
using UnityEditor.TerrainTools;

class CustomTerrainToolWithOverlays : TerrainPaintToolWithOverlays<CustomTerrainToolWithOverlays> { private float m_BrushRotation; // Return true for this property to show the brush selector overlay public override bool HasBrushMask => true;

// Return true for this property to show the tool settings overlay public override bool HasToolSettings => true; // Return true for this property to display the brush attributes overlay public override bool HasBrushAttributes => true; // File names of the light theme icons - prepending d_ to the file name generates dark theme variants. // public override string OnIcon => "Assets/Icon_on.png"; // public override string OffIcon => "Assets/Icon_off.png";

// The toolbar category the icon appears under public override TerrainCategory Category => TerrainCategory.CustomBrushes;

// Where in the icon list the icon appears public override int IconIndex => 100; // Name of the Terrain Tool. This appears in the tool UI public override string GetName() { return "Examples/Basic Custom Terrain Tool"; }

// Description for the Terrain Tool. This appears in the tool UI public override string GetDescription() { return "This Terrain Tool shows how to add custom UI to a tool and paint height."; }

// Override this function to add UI elements to the inspector public override void OnInspectorGUI(Terrain terrain, IOnInspectorGUI editContext) { EditorGUILayout.HelpBox("In Terrain Inspector", MessageType.None); editContext.ShowBrushesGUI(5, BrushGUIEditFlags.All); m_BrushRotation = EditorGUILayout.Slider("Rotation", m_BrushRotation, 0, 360); }

// Override this function to add UI elements to the tool settings overlay public override void OnToolSettingsGUI(Terrain terrain, IOnInspectorGUI editContext) { EditorGUILayout.HelpBox("In Overlays", MessageType.None); m_BrushRotation = EditorGUILayout.Slider("Rotation", m_BrushRotation, 0, 360); }

// Ease of use function for rendering modified Terrain Texture data into a PaintContext. Both OnRenderBrushPreview and OnPaint use this. private void RenderIntoPaintContext(UnityEngine.TerrainTools.PaintContext paintContext, Texture brushTexture, float brushOpacity, UnityEngine.TerrainTools.BrushTransform brushXform) { // Get the built-in painting Material reference Material mat = UnityEngine.TerrainTools.TerrainPaintUtility.GetBuiltinPaintMaterial(); // Bind the current brush texture mat.SetTexture("_BrushTex", brushTexture); // Bind the tool-specific shader properties var opacity = Event.current.control ? -brushOpacity : brushOpacity; mat.SetVector("_BrushParams", new Vector4(opacity, 0.0f, 0.0f, 0.0f)); // Set up the material for reading from/writing into the PaintContext texture data. This step is necessary to set up the correct shader properties for appropriately transforming UVs and sampling textures within the shader UnityEngine.TerrainTools.TerrainPaintUtility.SetupTerrainToolMaterialProperties(paintContext, brushXform, mat); // Render into the PaintContext's destinationRenderTexture using the built-in painting Material. The ID for the Raise/Lower pass is 0 Graphics.Blit(paintContext.sourceRenderTexture, paintContext.destinationRenderTexture, mat, 0); } // Render Tool previews in the Scene view public override void OnRenderBrushPreview(Terrain terrain, IOnSceneGUI editContext) { // Don't render preview if this isn't a Repaint if (Event.current.type != EventType.Repaint) return;

// Only do the rest if user mouse hits valid terrain if (!editContext.hitValidTerrain) return;

// Get the current BrushTransform under the mouse position relative to the Terrain UnityEngine.TerrainTools.BrushTransform brushXform = UnityEngine.TerrainTools.TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.raycastHit.textureCoord, editContext.brushSize, m_BrushRotation); // Get the PaintContext for the current BrushTransform. This has a sourceRenderTexture from which to read existing Terrain texture data. UnityEngine.TerrainTools.PaintContext paintContext = UnityEngine.TerrainTools.TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds(), 1); // Get the built-in Material for rendering Brush Previews Material previewMaterial = TerrainPaintUtilityEditor.GetDefaultBrushPreviewMaterial(); // Render the brush preview for the sourceRenderTexture. This shows up as a projected brush mesh rendered on top of the Terrain TerrainPaintUtilityEditor.DrawBrushPreview(paintContext, TerrainBrushPreviewMode.SourceRenderTexture, editContext.brushTexture, brushXform, previewMaterial, 0); // Render changes into the PaintContext destinationRenderTexture RenderIntoPaintContext(paintContext, editContext.brushTexture, editContext.brushStrength, brushXform); // Restore old render target RenderTexture.active = paintContext.oldRenderTexture; // Bind the sourceRenderTexture to the preview Material. This is used to compute deltas in height previewMaterial.SetTexture("_HeightmapOrig", paintContext.sourceRenderTexture); // Render a procedural mesh displaying the delta/displacement in height from the source Terrain texture data. When you modify Terrain height, this shows how much the next paint operation alters the Terrain height TerrainPaintUtilityEditor.DrawBrushPreview(paintContext, TerrainBrushPreviewMode.DestinationRenderTexture, editContext.brushTexture, brushXform, previewMaterial, 1); // Cleanup resources UnityEngine.TerrainTools.TerrainPaintUtility.ReleaseContextResources(paintContext); } // Perform painting operations that modify the Terrain texture data public override bool OnPaint(Terrain terrain, IOnPaint editContext) { // Get the current BrushTransform under the mouse position relative to the Terrain UnityEngine.TerrainTools.BrushTransform brushXform = UnityEngine.TerrainTools.TerrainPaintUtility.CalculateBrushTransform(terrain, editContext.uv, editContext.brushSize, m_BrushRotation); // Get the PaintContext for the current BrushTransform. This has a sourceRenderTexture from which to read existing Terrain texture data // and a destinationRenderTexture into which to write new Terrain texture data UnityEngine.TerrainTools.PaintContext paintContext = UnityEngine.TerrainTools.TerrainPaintUtility.BeginPaintHeightmap(terrain, brushXform.GetBrushXYBounds()); // Call the common rendering function that OnRenderBrushPreview and OnPaint use RenderIntoPaintContext(paintContext, editContext.brushTexture, editContext.brushStrength, brushXform); // Commit the modified PaintContext with a provided string for tracking Undo operations. This function handles Undo and resource cleanup for you UnityEngine.TerrainTools.TerrainPaintUtility.EndPaintHeightmap(paintContext, "Terrain Paint - Raise or Lower Height");

// Return whether Trees and Details should be hidden while you paint with this Terrain Tool return true; } }

继承的成员

属性

gridSnapEnabled使用此属性允许当前 EditorTool 启用/禁用网格捕捉。
target正在检查的对象。
targets正在检查的对象数组。
toolbarIcon此自定义编辑器工具的图标和工具提示。如果未实现此函数,则工具栏将显示目标类型的检查器图标。如果未定义目标类型,则工具栏将显示工具模式图标。
hideFlags对象是否应隐藏、与场景一起保存或用户可修改?
name对象的名称。
类别地形工具所属的 TerrainCategory。
HasBrushAttributes如果地形工具具有画笔属性,则为真,否则为假。
HasBrushMask如果地形工具具有画笔蒙版,则为真,否则为假。
HasToolSettings如果地形工具具有自定义设置,则为真,否则为假。
IconIndex您应在地形工具覆盖层中放置地形工具的索引。
OffIcon在地形工具未选中时,在地形工具覆盖层中显示的图标。
OnIcon在地形工具选中时,在地形工具覆盖层中显示的图标。
Terrain最后命中的地形或地形对象的最后一个活动实例。

公共方法

IsAvailable检查自定义编辑器工具是否根据编辑器状态可用。
OnActivated此 EditorTool 成为活动工具后调用。
OnToolGUI使用此方法实现自定义编辑器工具。
OnWillBeDeactivated此 EditorTool 停止成为活动工具之前调用。
PopulateMenu向场景视图上下文菜单添加菜单项。
GetInstanceID获取对象的实例 ID。
ToString返回对象的名称。
GetDescription地形工具的描述。
GetName地形工具的名称。
OnActivated激活工具时调用此函数。
OnDisable销毁工具时调用。
OnEnable创建工具时调用。
OnEnterToolMode激活地形工具时调用此函数。
OnExitToolMode地形工具变为非活动状态时调用此函数。
OnInspectorGUI自定义地形工具 OnInspectorGUI 回调。
OnPaint自定义地形工具绘制回调。
OnRenderBrushPreview使用此方法实现自定义工具预览和 UI 行为,这些行为仅在鼠标位于 SceneView 范围内或您正在积极使用此工具时才渲染。
OnSceneGUI自定义地形工具 OnSceneGUI 回调。
OnToolGUI此方法用于实现自定义地形编辑器绘画工具。
OnToolSettingsGUI包含超出常用设置的自定义设置的 IMGUI 代码。
OnWillBeDeactivated覆盖层地形绘画工具停止成为活动工具之前调用。

静态方法

Destroy移除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 obj。强烈建议您使用 Destroy 代替。
DontDestroyOnLoad加载新场景时不要销毁目标对象。
FindAnyObjectByType检索类型为 type 的任何活动加载对象。
FindFirstObjectByType检索类型为 type 的第一个活动加载对象。
FindObjectsByType检索类型为 type 的所有加载对象的列表。
Instantiate克隆对象 original 并返回克隆。
InstantiateAsync捕获原始对象(必须与某些 GameObject 相关)的快照并返回 AsyncInstantiateOperation。
CreateInstance创建可脚本化对象的实例。

运算符

bool对象是否存在?
operator !=比较两个对象是否引用不同的对象。
operator ==比较两个对象引用以查看它们是否引用同一个对象。

消息

Awake创建 ScriptableObject 的实例时调用。
OnDestroy此函数在可脚本化对象将被销毁时调用。
OnDisable此函数在可脚本化对象超出范围时调用。
OnEnable加载对象时调用此函数。
OnValidate当脚本加载或检查器中的值更改时,Unity 调用的仅编辑器函数。
Reset重置为默认值。