Script to Convert Terrain Trees/Grass to Game Objects in Unity – Unity地形转换为物件

In Unity development, there are times when you may need to convert assets within a terrain, such as trees or grass, into game objects. This can be particularly useful for more precise control over these assets, whether for gameplay mechanics or optimization purposes. Fortunately, members @lex24 and @HuskyPanda213 from the Unity community provided a handy script that can automate this process.

在Unity开发中,有时您可能需要将地形中的资产(例如树木或草)转换为游戏对象。这在需要更精确地控制这些资产时非常有用,无论是出于游戏机制还是优化的目的。幸运的是,来自Unity社区的@lex24和@HuskyPanda213提供了一个方便的脚本,可以自动化这个过程。


The script – 脚本内容

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
// Replaces Unity terrain trees with prefab GameObject.
// http://answers.unity3d.com/questions/723266/converting-all-terrain-trees-to-gameobjects.html
[ExecuteInEditMode]
public class TreeReplacerS : EditorWindow {
[Header("References")]
public Terrain _terrain;
//============================================
[MenuItem("Window/My/TreeReplacer")]
static void Init()
{
TreeReplacerS window = (TreeReplacerS)GetWindow(typeof(TreeReplacerS));
}
void OnGUI()
{
_terrain = (Terrain)EditorGUILayout.ObjectField(_terrain, typeof(Terrain), true);
if (GUILayout.Button("Convert to objects"))
{
Convert();
}
if (GUILayout.Button("Clear generated trees"))
{
Clear();
}
}
//============================================
public void Convert()
{
TerrainData data = _terrain.terrainData;
float width = data.size.x;
float height = data.size.z;
float y = data.size.y;
// Create parent
GameObject parent = GameObject.Find("TREES_GENERATED");
if (parent == null)
{
parent = new GameObject("TREES_GENERATED");
}
// Create trees
foreach (TreeInstance tree in data.treeInstances)
{
if (tree.prototypeIndex>=data.treePrototypes.Length)
continue;
var _tree = data.treePrototypes[tree.prototypeIndex].prefab;
Vector3 position = new Vector3(
tree.position.x * width,
tree.position.y*y,
tree.position.z * height) + _terrain.transform.position;
Vector3 scale = new Vector3(tree.widthScale,tree.heightScale,tree.widthScale);
GameObject go = Instantiate(_tree, position, Quaternion.Euler(0f,Mathf.Rad2Deg*tree.rotation,0f), parent.transform) as GameObject;
go.transform.localScale = scale;
}
}
public void Clear()
{
DestroyImmediate(GameObject.Find("TREES_GENERATED"));
}
}

How to Use the Script – 如何使用脚本

  1. Save the Script 保存脚本
    First, save the script provided by @lex24 & @HuskyPanda213 in the following path: Assets/Editor/TreeReplacerS.cs. This is necessary for the script to function properly within the Unity Editor.
    首先,将@lex24和@HuskyPanda213提供的脚本保存在以下路径中:Assets/Editor/TreeReplacerS.cs。这是为了使脚本在Unity编辑器中正常工作。
  2. Open the Tool 打开工具
    Once the script is saved in the correct location, you can access the tool by navigating to the Unity menu and selecting Window > My > TreeReplacer. This will open the tool interface, allowing you to use the script’s functionality.
    脚本保存到正确的位置后,您可以通过导航到Unity菜单并选择Window > My > TreeReplacer来访问工具。这将打开工具界面,允许您使用脚本的功能。
  3. Prepare Your Terrain 准备您的地形
    Make sure your terrain has the assets (trees, grass, etc.) that you wish to convert into game objects. This script will iterate through these assets and convert them accordingly.
    确保您的地形中包含您希望转换为游戏对象的资产(树木、草等)。这个脚本将遍历这些资产并相应地进行转换。
  4. Run the Script 运行脚本
    With the script attached and the tool open, run the scene. The script will automatically convert all the specified terrain assets into individual game objects, which you can then manipulate as needed.
    脚本附加后,运行场景。脚本将自动将所有指定的地形资产转换为单独的游戏对象,您可以根据需要进行操作。
  5. Verify the Conversion 验证转换
    After running the script, check the scene hierarchy to ensure that the assets have been converted into game objects correctly. You can now customize or manage these objects just like any other in your scene.
    运行脚本后,检查场景层次结构以确保资产已正确转换为游戏对象。现在,您可以像管理场景中的其他对象一样自定义或管理这些对象。

Final Thoughts

This script is a simple yet powerful tool for converting terrain assets into game objects, allowing for greater flexibility in how you handle these elements within your Unity projects. For more detailed discussions and to access the original script, refer to the original post on the Unity forums here.

最后总结

这个脚本是将地形资产转换为游戏对象的简单但功能强大的工具,允许您在Unity项目中更灵活地处理这些元素。有关更详细的讨论以及访问原始脚本,请参阅Unity论坛上的原始帖子链接

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇

本文最后更新于 222 天前,其中的信息可能已经有所发展或是发生改变。