当前位置:网站首页>Unity one key replacement of objects in the scene
Unity one key replacement of objects in the scene
2022-07-28 11:57:00 【Walking on the top of the clouds】
Realize the editor function of batch replacement of prefabricated bodies in the scene .
One 、 The key api
PrefabUtility.InstantiatePrefab Instantiate the preset into the scene , And GameObject.Instantiate Different , It is based on preset instances .
Two 、 Tool screenshot

3、 ... and 、 Usage method
stay Project Drag the preset into the tool
Open the scene that needs operation , You can choose more objects to replace , Or enter the name you want to replace
Save the scene after modification
Four 、 Code
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
public class ObjectReplaceEditorWindow : EditorWindow
{
[MenuItem("Tools/ Replace the objects in the scene ")]
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(" Choose a new object ");
newPrefab = (GameObject) EditorGUILayout.ObjectField(newPrefab, typeof(GameObject),true, GUILayout.MinWidth(100f));
tonewPrefab = newPrefab;
//isChangeName = EditorGUILayout.ToggleLeft(" Do not change the name of the object in the scene ", isChangeName);
if (GUILayout.Button(" Replace the selected object "))
{
ReplaceObjects();
}
EditorGUILayout.LabelField("----------------------");
replaceName = EditorGUILayout.TextField(" Name of the object to be replaced ", replaceName);
if (GUILayout.Button(" Replace with the same name "))
{
ReplaceObjectsByName(replaceName, false);
}
if (GUILayout.Button(" Replace with Use with caution "))
{
ReplaceObjectsByName(replaceName, true);
}
EditorGUILayout.LabelField("----------------------");
if (GUILayout.Button(" Save changes "))
{
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();
}
}
边栏推荐
- 中国业务型CDP白皮书 | 爱分析报告
- Hcip day 6 (OSPF related knowledge)
- String function (Part 2)
- 直接插入排序与希尔排序
- Solutions to slow start of MATLAB
- 如何让照片中的人物笑起来?HMS Core视频编辑服务一键微笑功能,让人物笑容更自然
- 业务可视化-让你的流程图'Run'起来(4.实际业务场景测试)
- R language uses dplyr package group_ By function and summarize function calculate the mean value of all covariates involved in the analysis based on grouped variables (difference in means of covariate
- Alexnet - paper analysis and reproduction
- Application of mobile face stylization Technology
猜你喜欢

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

保障邮箱安全,验证码四个优势

Develop your own NPM package from 0
![[geek challenge 2019] babysql-1 | SQL injection](/img/21/b5b4727178a585e610d743e92248f7.png)
[geek challenge 2019] babysql-1 | SQL injection

boost官网搜索引擎项目详解

Unity遇坑记之 ab包卸载失败

Introduction to the usage of SAP ui5 image display control avatar trial version

Modify the running container port mapping

移动端人脸风格化技术的应用

Application of mobile face stylization Technology
随机推荐
简单选择排序与堆排序
DNS series (III): how to avoid DNS spoofing
Advanced database technology learning notes 1 -- Oracle deployment and pl/sql overview
【补题日记】[2022牛客暑期多校2]H-Take the Elevator
R language uses oneway The test function performs one-way ANOVA
String function (Part 2)
【补题日记】[2022牛客暑期多校2]L-Link with Level Editor I
Reflect 机制获取Class 的属性和方法信息
Solutions to the disappearance of Jupiter, spyder, Anaconda prompt and navigator shortcut keys
Simple selection sort and heap sort
15、用户web层服务(三)
Guys, ask me, this can't be checkpoint, because there is a JDBC task whose task status is finished,
从零开始Blazor Server(2)--整合数据库
Develop your own NPM package from 0
Docker runs MySQL service
Service workers let the website dynamically load webp pictures
Zotero document manager and its use posture (updated from time to time)
Software testing and quality learning notes 1 --- black box testing
ES6知识点补充
P5472 [noi2019] douzhudi (expectation, Mathematics)