diff --git a/X10D.Unity.Tests/Assets/Tests/ComponentTests.cs b/X10D.Unity.Tests/Assets/Tests/ComponentTests.cs index c1b30f5..56c8dee 100644 --- a/X10D.Unity.Tests/Assets/Tests/ComponentTests.cs +++ b/X10D.Unity.Tests/Assets/Tests/ComponentTests.cs @@ -1,6 +1,5 @@ #nullable enable -using System; using System.Collections; using NUnit.Framework; using UnityEngine; @@ -11,67 +10,6 @@ namespace X10D.Unity.Tests { public class ComponentTests { - [UnityTest] - public IEnumerator CopyTo_ShouldCopyComponent_GivenComponent() - { - var source = new GameObject(); - var sourceComponent = source.AddComponent(); - sourceComponent.mass = 10.0f; - sourceComponent.useGravity = false; - - var target = new GameObject(); - sourceComponent.CopyTo(target); - - Assert.That(target.TryGetComponent(out Rigidbody targetComponent)); - Assert.That(targetComponent.mass, Is.EqualTo(10.0f)); - Assert.That(targetComponent.useGravity, Is.False); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator CopyTo_ShouldThrowArgumentNullException_GivenNullComponent() - { - var target = new GameObject(); - Rigidbody rigidbody = null!; - - Assert.Throws(() => rigidbody.CopyTo(target)); - - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator CopyTo_ShouldThrowArgumentNullException_GivenNullTarget() - { - var source = new GameObject(); - var rigidbody = source.AddComponent(); - GameObject target = null!; - - Assert.Throws(() => rigidbody.CopyTo(target)); - - Object.Destroy(source); - yield break; - } - - [UnityTest] - public IEnumerator CopyTo_ShouldThrowInvalidOperationException_GivenDuplicate() - { - var source = new GameObject(); - var rigidbody = source.AddComponent(); - - var target = new GameObject(); - target.AddComponent(); - - Assert.Throws(() => rigidbody.CopyTo(target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - [UnityTest] public IEnumerator GetComponentsInChildrenOnly_ShouldIgnoreParent() { @@ -90,70 +28,5 @@ namespace X10D.Unity.Tests Object.Destroy(parent); Object.Destroy(child); } - - [UnityTest] - public IEnumerator MoveTo_ShouldCopyComponent_GivenComponent() - { - var source = new GameObject(); - var sourceComponent = source.AddComponent(); - sourceComponent.mass = 10f; - sourceComponent.useGravity = false; - - var target = new GameObject(); - sourceComponent.MoveTo(target); - - // effects of Destroy only take place at end of frame - yield return null; - - Assert.That(sourceComponent == null); - Assert.That(source.TryGetComponent(out Rigidbody _), Is.False); - Assert.That(target.TryGetComponent(out Rigidbody targetComponent)); - Assert.That(targetComponent.mass, Is.EqualTo(10.0f)); - Assert.That(targetComponent.useGravity, Is.False); - - Object.Destroy(source); - Object.Destroy(target); - } - - [UnityTest] - public IEnumerator MoveTo_ShouldThrowArgumentNullException_GivenNullComponent() - { - var target = new GameObject(); - Rigidbody rigidbody = null!; - - Assert.Throws(() => rigidbody.MoveTo(target)); - - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator MoveTo_ShouldThrowArgumentNullException_GivenNullTarget() - { - var source = new GameObject(); - var rigidbody = source.AddComponent(); - GameObject target = null!; - - Assert.Throws(() => rigidbody.MoveTo(target)); - - Object.Destroy(source); - yield break; - } - - [UnityTest] - public IEnumerator MoveTo_ShouldThrowInvalidOperationException_GivenDuplicate() - { - var source = new GameObject(); - var rigidbody = source.AddComponent(); - - var target = new GameObject(); - target.AddComponent(); - - Assert.Throws(() => rigidbody.MoveTo(target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } } } diff --git a/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs b/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs index c555174..dca113b 100644 --- a/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs +++ b/X10D.Unity.Tests/Assets/Tests/GameObjectTests.cs @@ -1,6 +1,5 @@ #nullable enable -using System; using System.Collections; using System.Diagnostics.CodeAnalysis; using NUnit.Framework; @@ -12,97 +11,6 @@ namespace X10D.Unity.Tests { public class GameObjectTests { - [UnityTest] - public IEnumerator CopyComponent_ShouldCopyComponent_GivenComponent() - { - var source = new GameObject(); - var sourceComponent = source.AddComponent(); - sourceComponent.mass = 10.0f; - sourceComponent.useGravity = false; - - var target = new GameObject(); - source.CopyComponent(target); - - Assert.That(target.TryGetComponent(out Rigidbody targetComponent)); - Assert.That(targetComponent.mass, Is.EqualTo(10.0f)); - Assert.That(targetComponent.useGravity, Is.False); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator CopyComponent_ShouldThrowArgumentNullException_GivenNullComponentType() - { - var source = new GameObject(); - var target = new GameObject(); - Type componentType = null!; - - Assert.Throws(() => source.CopyComponent(componentType, target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator CopyComponent_ShouldThrowArgumentNullException_GivenNullGameObject() - { - var target = new GameObject(); - GameObject source = null!; - - Assert.Throws(() => source.CopyComponent(target)); - Assert.Throws(() => source.CopyComponent(typeof(Rigidbody), target)); - - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator CopyComponent_ShouldThrowArgumentNullException_GivenNullTarget() - { - var source = new GameObject(); - GameObject target = null!; - - Assert.Throws(() => source.CopyComponent(target)); - Assert.Throws(() => source.CopyComponent(typeof(Rigidbody), target)); - - Object.Destroy(source); - yield break; - } - - [UnityTest] - public IEnumerator CopyComponent_ShouldThrowInvalidOperationException_GivenInvalidComponent() - { - var source = new GameObject(); - var target = new GameObject(); - - Assert.Throws(() => source.CopyComponent(target)); - Assert.Throws(() => source.CopyComponent(typeof(Rigidbody), target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator CopyComponent_ShouldThrowInvalidOperationException_GivenDuplicate() - { - var source = new GameObject(); - source.AddComponent(); - - var target = new GameObject(); - target.AddComponent(); - - Assert.Throws(() => source.CopyComponent(target)); - Assert.Throws(() => source.CopyComponent(typeof(Rigidbody), target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - [UnityTest] public IEnumerator GetComponentsInChildrenOnly_ShouldIgnoreParent() { @@ -160,101 +68,6 @@ namespace X10D.Unity.Tests yield break; } - [UnityTest] - public IEnumerator MoveComponent_ShouldCopyComponent_GivenComponent() - { - var source = new GameObject(); - var sourceComponent = source.AddComponent(); - sourceComponent.mass = 10.0f; - sourceComponent.useGravity = false; - - var target = new GameObject(); - source.MoveComponent(target); - - // effects of Destroy only take place at end of frame - yield return null; - - Assert.That(sourceComponent == null); - Assert.That(source.TryGetComponent(out Rigidbody _), Is.False); - Assert.That(target.TryGetComponent(out Rigidbody targetComponent)); - Assert.That(targetComponent.mass, Is.EqualTo(10.0f)); - Assert.That(targetComponent.useGravity, Is.False); - - Object.Destroy(source); - Object.Destroy(target); - } - - [UnityTest] - public IEnumerator MoveComponent_ShouldThrowArgumentNullException_GivenNullComponentType() - { - var source = new GameObject(); - var target = new GameObject(); - Type componentType = null!; - - Assert.Throws(() => source.MoveComponent(componentType, target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator MoveComponent_ShouldThrowArgumentNullException_GivenNullGameObject() - { - var target = new GameObject(); - GameObject source = null!; - - Assert.Throws(() => source.MoveComponent(target)); - Assert.Throws(() => source.MoveComponent(typeof(Rigidbody), target)); - - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator MoveComponent_ShouldThrowArgumentNullException_GivenNullTarget() - { - var source = new GameObject(); - GameObject target = null!; - - Assert.Throws(() => source.MoveComponent(target)); - Assert.Throws(() => source.MoveComponent(typeof(Rigidbody), target)); - - Object.Destroy(source); - yield break; - } - - [UnityTest] - public IEnumerator MoveComponent_ShouldThrowInvalidOperationException_GivenInvalidComponent() - { - var source = new GameObject(); - var target = new GameObject(); - - Assert.Throws(() => source.MoveComponent(target)); - Assert.Throws(() => source.MoveComponent(typeof(Rigidbody), target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - - [UnityTest] - public IEnumerator MoveComponent_ShouldThrowInvalidOperationException_GivenDuplicate() - { - var source = new GameObject(); - source.AddComponent(); - - var target = new GameObject(); - target.AddComponent(); - - Assert.Throws(() => source.MoveComponent(target)); - Assert.Throws(() => source.MoveComponent(typeof(Rigidbody), target)); - - Object.Destroy(source); - Object.Destroy(target); - yield break; - } - [UnityTest] public IEnumerator SetLayerRecursively_ShouldSetLayerRecursively() { diff --git a/X10D.Unity/src/ComponentExtensions.cs b/X10D.Unity/src/ComponentExtensions.cs index 9a7dafc..6ad7388 100644 --- a/X10D.Unity/src/ComponentExtensions.cs +++ b/X10D.Unity/src/ComponentExtensions.cs @@ -1,8 +1,4 @@ -using System.Globalization; -using System.Reflection; -using UnityEngine; -using X10D.Reflection; -using Object = UnityEngine.Object; +using UnityEngine; namespace X10D.Unity; @@ -11,93 +7,6 @@ namespace X10D.Unity; /// public static class ComponentExtensions { - /// - /// Copies the component to another game object. - /// - /// The component to copy. - /// The game object to which the component will be copied. - /// The type of the component to copy. - /// - /// is . - /// -or- - /// is . - /// - /// - /// already has a component of type . - /// - /// - /// This method will destroy the component on the source game object, creating a new instance on the target. Use with - /// caution. - /// - public static void CopyTo(this T component, GameObject target) - where T : Component - { - if (component == null) - { - throw new ArgumentNullException(nameof(component)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - if (target.TryGetComponent(out T targetComponent)) - { - string message = ExceptionMessages.ComponentAlreadyExists; - message = string.Format(CultureInfo.CurrentCulture, message, target.name, typeof(T).Name); - throw new InvalidOperationException(message); - } - - targetComponent = target.AddComponent(); - - var typeInfo = typeof(T).GetTypeInfo(); - CopyFields(typeInfo, component, targetComponent); - } - - /// - /// Copies the component to another game object. - /// - /// The component to copy. - /// The game object to which the component will be copied. - /// - /// is . - /// -or- - /// is . - /// - /// - /// already has a component of the same type. - /// - /// - /// This method will destroy the component on the source game object, creating a new instance on the target. Use with - /// caution. - /// - public static void CopyTo(this Component component, GameObject target) - { - if (component == null) - { - throw new ArgumentNullException(nameof(component)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - var componentType = component.GetType(); - if (target.TryGetComponent(componentType, out Component targetComponent)) - { - string message = ExceptionMessages.ComponentAlreadyExists; - message = string.Format(CultureInfo.CurrentCulture, message, target.name, componentType.Name); - throw new InvalidOperationException(message); - } - - targetComponent = target.AddComponent(componentType); - - var typeInfo = componentType.GetTypeInfo(); - CopyFields(typeInfo, component, targetComponent); - } - /// /// Returns an array of components of the specified type, excluding components that live on the object to which this /// component is attached. @@ -109,107 +18,4 @@ public static class ComponentExtensions { return component.gameObject.GetComponentsInChildrenOnly(); } - - /// - /// Moves the component to another game object. - /// - /// The component to move. - /// The game object to which the component will be moved. - /// The type of the component to move. - /// - /// is . - /// -or- - /// is . - /// - /// - /// already has a component of type . - /// - /// - /// This method will destroy the component on the source game object, creating a new instance on the target. Use with - /// caution. - /// - public static void MoveTo(this T component, GameObject target) - where T : Component - { - if (component == null) - { - throw new ArgumentNullException(nameof(component)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - component.CopyTo(target); - Object.Destroy(component); - } - - /// - /// Moves the component to another game object. - /// - /// The component to move. - /// The game object to which the component will be moved. - /// - /// is . - /// -or- - /// is . - /// - /// - /// already has a component of the same type. - /// - /// - /// This method will destroy the component on the source game object, creating a new instance on the target. Use with - /// caution. - /// - public static void MoveTo(this Component component, GameObject target) - { - if (component == null) - { - throw new ArgumentNullException(nameof(component)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - component.CopyTo(target); - Object.Destroy(component); - } - - private static void CopyFields(TypeInfo typeInfo, T component, T targetComponent) - where T : Component - { - foreach (FieldInfo field in typeInfo.DeclaredFields) - { - if (field.IsStatic || !field.IsPublic && !field.HasCustomAttribute()) - { - continue; - } - - if (field.HasCustomAttribute()) - { - continue; - } - - object fieldValue = GetNewReferences(component, targetComponent, field.GetValue(component)); - field.SetValue(targetComponent, fieldValue); - } - } - - private static object GetNewReferences(T component, T targetComponent, object value) - where T : Component - { - if (ReferenceEquals(value, component)) - { - value = targetComponent; - } - else if (ReferenceEquals(value, component.gameObject)) - { - value = targetComponent.gameObject; - } - - return value; - } } diff --git a/X10D.Unity/src/GameObjectExtensions.cs b/X10D.Unity/src/GameObjectExtensions.cs index 1ede330..97d03d3 100644 --- a/X10D.Unity/src/GameObjectExtensions.cs +++ b/X10D.Unity/src/GameObjectExtensions.cs @@ -1,6 +1,4 @@ -using System.Globalization; using UnityEngine; -using Object = UnityEngine.Object; namespace X10D.Unity; @@ -9,90 +7,6 @@ namespace X10D.Unity; /// public static class GameObjectExtensions { - /// - /// Copies the component of the specified type from one game object to another. - /// - /// The game object from which to copy the component. - /// The game object to which the component will be copied. - /// The type of the component to copy. - /// - /// is . - /// -or- - /// is . - /// - /// - /// does not have a component of type . - /// -or- - /// already has a component of type . - /// - public static void CopyComponent(this GameObject gameObject, GameObject target) - where T : Component - { - if (gameObject == null) - { - throw new ArgumentNullException(nameof(gameObject)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - if (!gameObject.TryGetComponent(out T sourceComponent)) - { - string message = ExceptionMessages.ComponentDoesNotExist; - message = string.Format(CultureInfo.CurrentCulture, message, gameObject.name, typeof(T).Name); - throw new InvalidOperationException(message); - } - - sourceComponent.CopyTo(target); - } - - /// - /// Copies the component of the specified type from one game object to another. - /// - /// The game object from which to copy the component. - /// The type of the component to copy. - /// The game object to which the component will be copied. - /// - /// is . - /// -or- - /// is . - /// -or- - /// is . - /// - /// - /// does not have a component of type . - /// -or- - /// already has a component of type . - /// - public static void CopyComponent(this GameObject gameObject, Type componentType, GameObject target) - { - if (gameObject == null) - { - throw new ArgumentNullException(nameof(gameObject)); - } - - if (componentType is null) - { - throw new ArgumentNullException(nameof(componentType)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - if (!gameObject.TryGetComponent(componentType, out Component sourceComponent)) - { - string message = ExceptionMessages.ComponentDoesNotExist; - message = string.Format(CultureInfo.CurrentCulture, message, gameObject.name, componentType.Name); - throw new InvalidOperationException(message); - } - - sourceComponent.CopyTo(target); - } - /// /// Returns an array of components of the specified type, excluding components that live on this game object. /// @@ -257,100 +171,6 @@ public static class GameObjectExtensions gameObject.transform.LookAt(target, worldUp); } - /// - /// Moves the component of the specified type from one game object to another. - /// - /// The game object from which to move the component. - /// The game object to which the component will be moved. - /// The type of the component to copy. - /// - /// is . - /// -or- - /// is . - /// - /// - /// does not have a component of type . - /// -or- - /// already has a component of type . - /// - /// - /// This method will destroy the component on the source game object, creating a new instance on the target. Use with - /// caution. - /// - public static void MoveComponent(this GameObject gameObject, GameObject target) - where T : Component - { - if (gameObject == null) - { - throw new ArgumentNullException(nameof(gameObject)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - if (!gameObject.TryGetComponent(out T sourceComponent)) - { - string message = ExceptionMessages.ComponentDoesNotExist; - message = string.Format(CultureInfo.CurrentCulture, message, gameObject.name, typeof(T).Name); - throw new InvalidOperationException(message); - } - - sourceComponent.MoveTo(target); - Object.Destroy(sourceComponent); - } - - /// - /// Moves the component of the specified type from one game object to another. - /// - /// The game object from which to move the component. - /// The type of the component to copy. - /// The game object to which the component will be moved. - /// - /// is . - /// -or- - /// is . - /// -or- - /// is . - /// - /// - /// does not have a component of type . - /// -or- - /// already has a component of type . - /// - /// - /// This method will destroy the component on the source game object, creating a new instance on the target. Use with - /// caution. - /// - public static void MoveComponent(this GameObject gameObject, Type componentType, GameObject target) - { - if (gameObject == null) - { - throw new ArgumentNullException(nameof(gameObject)); - } - - if (componentType is null) - { - throw new ArgumentNullException(nameof(componentType)); - } - - if (target == null) - { - throw new ArgumentNullException(nameof(target)); - } - - if (!gameObject.TryGetComponent(componentType, out Component sourceComponent)) - { - string message = ExceptionMessages.ComponentDoesNotExist; - message = string.Format(CultureInfo.CurrentCulture, message, gameObject.name, componentType.Name); - throw new InvalidOperationException(message); - } - - sourceComponent.MoveTo(target); - Object.Destroy(sourceComponent); - } - /// /// Sets the new layer of this game object and its children, recursively. ///