当前位置:网站首页>Unity 一键替换场景中的物体
Unity 一键替换场景中的物体
2022-07-28 11:15:00 【漫步云巅】
实现场景中批量替换预制体的编辑器功能。
一、关键api
PrefabUtility.InstantiatePrefab 将预设实例化到场景中,与GameObject.Instantiate不同,它是以预设实例的。
二、工具截图

三、使用方法
在Project中拖拽预设到工具中
打开需要操作的场景,可以多选需要替换的物体,或者输入需要替换的名字
修改完成后保存场景
四、代码
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public class ObjectReplaceEditorWindow : EditorWindow
{
[MenuItem("Tools/替换场景中的物体")]
public static void Open()
{
EditorWindow.GetWindow(typeof(ObjectReplaceEditorWindow));
}
public GameObject newPrefab;
static GameObject tonewPrefab;
private string replaceName = "";
private bool isChangeName = false;
public class ReplaceData
{
public GameObject old;
public GameObject replace;
public int index = 0;
}
void OnGUI()
{
EditorGUILayout.LabelField("选择一个新的物体");
newPrefab = (GameObject) EditorGUILayout.ObjectField(newPrefab, typeof(GameObject),true, GUILayout.MinWidth(100f));
tonewPrefab = newPrefab;
//isChangeName = EditorGUILayout.ToggleLeft("不改变场景中物体的名字", isChangeName);
if (GUILayout.Button("替换选中的物体"))
{
ReplaceObjects();
}
EditorGUILayout.LabelField("----------------------");
replaceName = EditorGUILayout.TextField("需要替换的物体名字", replaceName);
if (GUILayout.Button("替换相同名字的"))
{
ReplaceObjectsByName(replaceName, false);
}
if (GUILayout.Button("替换包含名字的 慎用"))
{
ReplaceObjectsByName(replaceName, true);
}
EditorGUILayout.LabelField("----------------------");
if (GUILayout.Button("保存修改"))
{
EditorSceneManager.SaveScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene());
}
}
void ReplaceObjects()
{
if (tonewPrefab == null) return;
Object[] objects = Selection.objects;
List<ReplaceData> replaceDatas = new List<ReplaceData>();
foreach (Object item in objects)
{
GameObject temp = (GameObject)item;
ReplaceData replaceData = new ReplaceData();
replaceData.old = temp;
ReplaceOne(replaceData);
replaceDatas.Add(replaceData);
}
HandleReplaceData(replaceDatas);
}
void HandleReplaceData(List<ReplaceData> replaceDatas)
{
foreach (var replaceData in replaceDatas)
{
replaceData.replace.transform.SetSiblingIndex(replaceData.index);
if(null != replaceData.old && null != replaceData.old.gameObject)
DestroyImmediate(replaceData.old.gameObject);
}
}
void ReplaceObjectsByName(string name, bool isContain)
{
if (string.IsNullOrEmpty(name)) return;
List<ReplaceData> replaceDatas = new List<ReplaceData>();
Transform[] all = Object.FindObjectsOfType<Transform>();
foreach (var item in all)
{
//Debug.LogError(item.name);
ReplaceData replaceData = new ReplaceData();
replaceData.old = item.gameObject;
if (!isContain && item.gameObject.name == name)
{
ReplaceOne(replaceData);
replaceDatas.Add(replaceData);
}
else if (isContain && item.gameObject.name.Contains(name))
{
ReplaceOne(replaceData);
replaceDatas.Add(replaceData);
}
}
HandleReplaceData(replaceDatas);
}
public void ReplaceOne(ReplaceData replaceData)
{
GameObject replace = (GameObject)PrefabUtility.InstantiatePrefab(tonewPrefab);
replace.transform.SetParent(replaceData.old.transform.parent);
replace.transform.localPosition = replaceData.old.transform.localPosition;
replace.transform.localRotation = replaceData.old.transform.localRotation;
replace.transform.localScale = replaceData.old.transform.localScale;
replaceData.replace = replace;
replaceData.index = replaceData.old.transform.GetSiblingIndex();
}
}
边栏推荐
- Consumer installation and configuration
- [极客大挑战 2019]BabySQL-1|SQL注入
- WPF layout controls are scaled up and down with the window, which is suitable for multi-resolution full screen filling applications
- How to use JWT for authentication and authorization
- Solutions to slow start of MATLAB
- 15. User web layer services (III)
- js代码如何被浏览器引擎编译执行的?
- 如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
- Object to object mapping -automapper
- 拥抱开源指南
猜你喜欢

Boutique scheme | Haitai Fangyuan full stack data security management scheme sets a "security lock" for data

Router firmware decryption idea

强缓存、协商缓存具体过程

一种比读写锁更快的锁,还不赶紧认识一下

可视化大型时间序列的技巧。

A new mode of one-stop fixed asset management

一文看懂设备指纹如何防篡改、防劫持

Server online speed measurement system source code

Unity鼠标带动物体运动的三种方法

In order to ensure the normal operation of fire-fighting equipment in large buildings, the power supply monitoring system of fire-fighting equipment plays a key role
随机推荐
ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
强缓存、协商缓存具体过程
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
Byte side: how to realize reliable transmission with UDP?
P5472 [NOI2019] 斗主地(期望、数学)
SkiaSharp 之 WPF 自绘 拖曳小球(案例版)
14、用户web层服务(二)
ES6知识点补充
Why does acid food hurt teeth + early periodontitis
Can dynamic partitions be configured when MySQL is offline synchronized to ODPs
LabVIEW AI视觉工具包(非NI Vision)下载与安装教程
Four advantages of verification code to ensure mailbox security
Service Workers让网站动态加载Webp图片
LabVIEW AI visual Toolkit (non Ni vision) download and installation tutorial
R language uses LM function to build regression model, uses the augmented function of bloom package to store the model results in dataframe, and uses ggplot2 to visualize the regression residual diagr
How to effectively implement a rapid and reasonable safety evacuation system in hospitals
一些多参数函数的具体作用
Go deadlock - when the channel meets mutex
The reflect mechanism obtains the attribute and method information of class
Quickly deploy mqtt clusters on AWS using terraform