-
-
Save AngryAnt/3806123 to your computer and use it in GitHub Desktop.
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{} |
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
Scary. I just did a quick fix and it should be working again.
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?
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.
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
Dictionary
1' could not be found. Are you missing a using directive or an assembly reference?