Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Last active October 11, 2015 04:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AngryAnt/3806123 to your computer and use it in GitHub Desktop.
Save AngryAnt/3806123 to your computer and use it in GitHub Desktop.
Example code for "CopyInspector" blog post on AngryAnt.com
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
public class CopyInspector : Editor
{
static System.Type m_OriginalType;
static Dictionary <PropertyInfo, object> m_Values;
private List GetProperties (Component component)
{
List <string> ignoredProperties;
List properties;
properties = new List ();
ignoredProperties = new List <string> ();
foreach (PropertyInfo propertyInfo in typeof (Component).GetProperties ())
{
ignoredProperties.Add (propertyInfo.Name);
}
foreach (PropertyInfo propertyInfo in component.GetType ().GetProperties ())
{
if (ignoredProperties.Contains (propertyInfo.Name))
{
continue;
}
properties.Add (propertyInfo);
}
return properties;
}
public override void OnInspectorGUI ()
{
DrawDefaultInspector ();
OnCopyInspectorGUI ();
}
public void OnCopyInspectorGUI ()
{
bool enabled;
List properties;
Component component;
component = target as Component;
if (component == null)
{
return;
}
GUILayout.Space (10.0f);
Color backgroundColor = GUI.backgroundColor;
GUI.backgroundColor = new Color (0.8f, 0.8f, 0.8f);
GUILayout.BeginVertical ("Toolbar");
GUI.backgroundColor = backgroundColor;
GUILayout.BeginHorizontal ();
GUILayout.Space (10.0f);
GUILayout.Label ("Copied: " + (m_OriginalType != null ? m_OriginalType.Name : "Nothing"), "MiniLabel");
GUILayout.FlexibleSpace ();
if (GUILayout.Button (new GUIContent ("Copy", "Copy component values"), "MiniLabel"))
{
m_OriginalType = target.GetType ();
properties = GetProperties (component);
m_Values = new Dictionary <PropertyInfo, object> ();
foreach (PropertyInfo property in properties)
{
m_Values [property] = property.GetValue (component, null);
}
}
enabled = GUI.enabled;
GUI.enabled = target.GetType () == m_OriginalType;
GUILayout.Space (10.0f);
if (GUILayout.Button (new GUIContent ("Paste", "Paste component values"), "MiniLabel"))
{
properties = GetProperties (component);
foreach (PropertyInfo property in properties)
{
if (!property.CanWrite)
{
continue;
}
property.SetValue (component, m_Values [property], null);
}
}
GUILayout.Space (10.0f);
GUI.enabled = enabled;
GUILayout.EndHorizontal ();
GUILayout.EndVertical ();
GUILayout.Space (-2.0f);
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor (typeof (Transform))]
public class CopyTransformInspector : CopyInspector
{
public override void OnInspectorGUI ()
{
Transform transform;
Vector3 localPosition, localScale;
Quaternion localRotation;
transform = target as Transform;
localPosition = EditorGUILayout.Vector3Field ("Position", transform.localPosition);
localRotation = Quaternion.Euler (EditorGUILayout.Vector3Field ("Rotation", transform.localRotation.eulerAngles));
localScale = EditorGUILayout.Vector3Field ("Scale", transform.localScale);
if (GUI.changed)
{
transform.localPosition = localPosition;
transform.localRotation = localRotation;
transform.localScale = localScale;
}
OnCopyInspectorGUI ();
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor (typeof (YourComponentType))]
public class CopyYourComponentTypeInspector : CopyInspector{}
@CountZer0
Copy link

I get errors for line 10:
Dictionary object>

I also get this error if I change it to Dictionary :

Assets/Editor/CopyInspector.cs(10,16): error CS0246: The type or namespace name Dictionary1' could not be found. Are you missing a using directive or an assembly reference?

@PhilippeChabot
Copy link

You can find (older) fixed version here : http://web.archive.org/web/20120521131717/http://eej.dk/angryant/general/tipsandtricks/copyinspector/

It was just some typo errors I think. :)

GitHub seems to eat < > sometimes :S

@AngryAnt
Copy link
Author

Scary. I just did a quick fix and it should be working again.

@col000r
Copy link

col000r commented Aug 12, 2013

Assets/Editor/CopyInspector.cs(14,17): error CS0246: The type or namespace name `List' could not be found. Are you missing a using directive or an assembly reference?

@mushutehk
Copy link

Hello.
I've forked this code => https://gist.github.com/mushutehk/6636510
and modified it so that it works 100%
it can also now handle the copying of multiple components (though currently only the Transform component is implemented).

@AngryAnt Thanks for sharing the original code. I'm new and hence don't yet know the general conduct of Git so if it's not considered "nice" to go around forking everyones code, let me know and I can take it down.

Otherwise I hope people can find this as useful as I have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment