using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
namespace GitMerge
{
public static class GameObjectExtensions
{
///
/// Adds the copy of a Component to a GameObject.
///
/// The GameObject that will get the new Component
/// The original component to copy
/// The reference to the newly added Component copy
public static Component AddComponent(this GameObject go, Component original)
{
var c = go.AddComponent(original.GetType());
var originalSerialized = new SerializedObject(original).GetIterator();
var nso = new SerializedObject(c);
var newSerialized = nso.GetIterator();
if(originalSerialized.Next(true))
{
newSerialized.Next(true);
while(originalSerialized.NextVisible(true))
{
newSerialized.NextVisible(true);
newSerialized.SetValue(originalSerialized.GetValue());
}
}
nso.ApplyModifiedProperties();
return c;
}
///
/// Activates/deactivates the GameObjct, and hides it when it is disabled.
/// This is used for "their" objects to hide them while merging.
///
/// The object do enable/disable
/// Enable or disable the object?
public static void SetActiveForMerging(this GameObject go, bool active)
{
go.SetActive(active);
go.hideFlags = active ? HideFlags.None : HideFlags.HideAndDontSave;
}
///
/// Ping the GameObject in the hierarchy, select it, and center it in the scene view.
///
/// The GameObject of interest
public static void Highlight(this GameObject go)
{
Selection.activeGameObject = go;
EditorGUIUtility.PingObject(go);
var view = SceneView.lastActiveSceneView;
if(view)
{
view.FrameSelected();
}
}
}
}