Skip to content

Commit

Permalink
Add CopyComponents Function
Browse files Browse the repository at this point in the history
  • Loading branch information
HankunYu committed Jul 25, 2024
1 parent c7b9c3b commit 78a47bb
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 5 deletions.
13 changes: 13 additions & 0 deletions .idea/.idea.hankun_unity_packages/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.hankun_unity_packages/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.hankun_unity_packages/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/.idea.hankun_unity_packages/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 108 additions & 0 deletions Assets/Packages/Utilis/Editor/CopyComponents.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System.Collections.Generic;

public class CopyComponents : EditorWindow
{
private GameObject sourceObject;
private GameObject targetObject;
private List<Component> selectedComponents = new List<Component>();
private bool changeName = false;
private Vector2 scrollPos;

[MenuItem("HankunTools/Copy Components")]
public static void ShowWindow()
{
GetWindow<CopyComponents>("Copy Components");
}

private void OnGUI()
{
GUILayout.Label("Copy Components from one GameObject to another", EditorStyles.boldLabel);

sourceObject = (GameObject)EditorGUILayout.ObjectField("Source Object", sourceObject, typeof(GameObject), true);
targetObject = (GameObject)EditorGUILayout.ObjectField("Target Object", targetObject, typeof(GameObject), true);

if (sourceObject != null)
{
GUILayout.Label("Select Components to Copy", EditorStyles.boldLabel);

scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Height(200));
Component[] components = sourceObject.GetComponents<Component>();
foreach (Component component in components)
{
if (component is Transform)
continue;

bool isSelected = selectedComponents.Contains(component);
bool newIsSelected = EditorGUILayout.ToggleLeft(component.GetType().Name, isSelected);

if (newIsSelected && !isSelected)
{
selectedComponents.Add(component);
}
else if (!newIsSelected && isSelected)
{
selectedComponents.Remove(component);
}
}
EditorGUILayout.EndScrollView();
}

changeName = EditorGUILayout.Toggle("Change Name", changeName);

if (GUILayout.Button("Copy Components"))
{
if (sourceObject == null || targetObject == null)
{
EditorUtility.DisplayDialog("Error", "Please assign both Source and Target objects.", "OK");
return;
}

CopySelectedComponents(sourceObject, targetObject);
}
}

private void CopySelectedComponents(GameObject source, GameObject target)
{
var originalName = target.name;
foreach (Component component in selectedComponents)
{
Component newComponent = target.AddComponent(component.GetType());
CopyComponentValues(component, newComponent);
}
EditorUtility.DisplayDialog("Success", "Components copied successfully!", "OK");
// Change name
target.name = changeName ? source.name : originalName;
selectedComponents.Clear();
}

private void CopyComponentValues(Component source, Component destination)
{
System.Type type = source.GetType();
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default;
PropertyInfo[] properties = type.GetProperties(flags);

foreach (PropertyInfo property in properties)
{
if (property.CanWrite)
{
try
{
property.SetValue(destination, property.GetValue(source, null), null);
}
catch
{
// In case of NotImplementedException being thrown.
}
}
}

FieldInfo[] fields = type.GetFields(flags);
foreach (FieldInfo field in fields)
{
field.SetValue(destination, field.GetValue(source));
}
}
}
2 changes: 2 additions & 0 deletions Assets/Packages/Utilis/Editor/CopyComponents.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Assets/Packages/Utilis/Editor/com.hankun.utilis.editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "com.hankun.utilis.editor"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Settings/UniversalRenderPipelineGlobalSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ MonoBehaviour:
m_GeometryShadowShader: {fileID: 4800000, guid: 19349a0f9a7ed4c48a27445bcf92e5e1, type: 3}
m_GeometryUnshadowShader: {fileID: 4800000, guid: 77774d9009bb81447b048c907d4c6273, type: 3}
m_FallOffLookup: {fileID: 2800000, guid: 5688ab254e4c0634f8d6c8e0792331ca, type: 3}
m_CopyDepthPS: {fileID: 4800000, guid: d6dae50ee9e1bfa4db75f19f99355220, type: 3}
m_DefaultCustomMaterial: {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2}
m_DefaultLitMaterial: {fileID: 2100000, guid: a97c105638bdf8b4a8650670310a4cd3, type: 2}
m_DefaultUnlitMaterial: {fileID: 2100000, guid: 9dfc825aed78fcd4ba02077103263b40, type: 2}
Expand Down Expand Up @@ -247,6 +248,7 @@ MonoBehaviour:
data:
m_xrOcclusionMeshPS: {fileID: 4800000, guid: 4431b1f1f743fbf4eb310a967890cbea, type: 3}
m_xrMirrorViewPS: {fileID: 4800000, guid: d5a307c014552314b9f560906d708772, type: 3}
m_xrMotionVector: {fileID: 4800000, guid: f89aac1e4f84468418fe30e611dff395, type: 3}
- rid: 8573396250215055361
type: {class: RenderGraphUtilsResources, ns: UnityEngine.Rendering.RenderGraphModule.Util, asm: Unity.RenderPipelines.Core.Runtime}
data:
Expand Down
2 changes: 1 addition & 1 deletion Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "https://packages.unity.com"
},
"com.unity.burst": {
"version": "1.8.15",
"version": "1.8.16",
"depth": 2,
"source": "registry",
"dependencies": {
Expand Down
3 changes: 1 addition & 2 deletions ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ EditorBuildSettings:
path: Assets/Scenes/SampleScene.unity
guid: 99c9720ab356a0642a771bea13969a05
m_configObjects:
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b,
type: 3}
com.unity.input.settings.actions: {fileID: -944628639613478452, guid: 052faaac586de48259a63d0c4782560b, type: 3}
m_UseUCBPForAssetBundles: 0
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectVersion.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
m_EditorVersion: 6000.0.7f1
m_EditorVersionWithRevision: 6000.0.7f1 (7dd95c051e11)
m_EditorVersion: 6000.0.9f1
m_EditorVersionWithRevision: 6000.0.9f1 (1490908003ac)

0 comments on commit 78a47bb

Please sign in to comment.