diff --git a/EXILED/Exiled.API/Extensions/GameObjectExtensions.cs b/EXILED/Exiled.API/Extensions/GameObjectExtensions.cs new file mode 100644 index 0000000000..c935545b5d --- /dev/null +++ b/EXILED/Exiled.API/Extensions/GameObjectExtensions.cs @@ -0,0 +1,72 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) ExMod Team. All rights reserved. +// Licensed under the CC BY-SA 3.0 license. +// +// ----------------------------------------------------------------------- + +#nullable enable +namespace Exiled.API.Extensions +{ + using UnityEngine; + + /// + /// A set of extensions for . + /// + public static class GameObjectExtensions + { + /// + /// Gets the global scale of a GameObject. + /// + /// The to check. + /// The global scale of the provided . + public static Vector3 GetWorldScale(this GameObject gameObject) + { + Transform? parent = gameObject.transform.parent; + + if (!parent) + { + /*Features.Log.Info($"[GetWorldScale:{new StackFrame(1).GetMethod().Name}] LocalScale:{gameObject.transform.localScale} LossyScale:{gameObject.transform.lossyScale} GlobalScale:{gameObject.transform.localScale}");*/ + return gameObject.transform.localScale; + } + + gameObject.transform.SetParent(null, true); + Vector3 value = gameObject.transform.localScale; + gameObject.transform.SetParent(parent, true); + + /*Features.Log.Info($"[GetWorldScale:{new StackFrame(1).GetMethod().Name}] LocalScale:{gameObject.transform.localScale} LossyScale:{gameObject.transform.lossyScale} GlobalScale:{value}");*/ + return value; + } + + /// + /// Sets the global scale of a GameObject. + /// + /// The to modify. + /// The new scale. + public static void SetWorldScale(this GameObject gameObject, Vector3 scale) + { + Transform? parent = gameObject.transform.parent; + + if (!parent) + { + gameObject.transform.localScale = scale; + /*Features.Log.Info($"[SetWorldScale] No Parent {scale}");*/ + return; + } + + /* + gameObject.transform.SetParent(null, true); + Vector3 value = gameObject.transform.localScale; + gameObject.transform.SetParent(parent, true); + + Features.Log.Info($"[SetWorldScale] Before: LocalScale:{gameObject.transform.localScale} LossyScale:{gameObject.transform.lossyScale} GlobalScale:{value}"); + */ + gameObject.transform.SetParent(null, true); + gameObject.transform.localScale = scale; + gameObject.transform.SetParent(parent, true); + /* + Features.Log.Info($"[SetWorldScale] After: LocalScale:{gameObject.transform.localScale} LossyScale:{gameObject.transform.lossyScale} GlobalScale:{value}"); + */ + } + } +} \ No newline at end of file diff --git a/EXILED/Exiled.API/Features/Items/Item.cs b/EXILED/Exiled.API/Features/Items/Item.cs index c329b614ff..839585d0c9 100644 --- a/EXILED/Exiled.API/Features/Items/Item.cs +++ b/EXILED/Exiled.API/Features/Items/Item.cs @@ -10,6 +10,7 @@ namespace Exiled.API.Features.Items using System.Collections.Generic; using System.Linq; + using Exiled.API.Extensions; using Exiled.API.Features.Core; using Exiled.API.Features.Pickups; using Exiled.API.Interfaces; @@ -438,6 +439,8 @@ internal virtual void ChangeOwner(Player oldOwner, Player newOwner) Base.OnAdded(null); } + // TODO: remove use of GetWorldScale after NW fix WaypointToy. + /// /// Helper method for saving data between items and pickups. /// @@ -451,7 +454,7 @@ internal virtual void ReadPickupInfoBefore(Pickup pickup) { if (pickup is not null) { - Scale = pickup.Scale; + Scale = pickup.GameObject.GetWorldScale(); } } diff --git a/EXILED/Exiled.API/Features/Pickups/Pickup.cs b/EXILED/Exiled.API/Features/Pickups/Pickup.cs index 5297acae5d..ab635fa81b 100644 --- a/EXILED/Exiled.API/Features/Pickups/Pickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/Pickup.cs @@ -150,17 +150,17 @@ public ushort Serial /// public Vector3 Scale { - get => GameObject.transform.localScale; + get => GameObject.GetWorldScale(); set { if (!IsSpawned) { - GameObject.transform.localScale = value; + GameObject.SetWorldScale(value); return; } UnSpawn(); - GameObject.transform.localScale = value; + GameObject.SetWorldScale(value); Spawn(); } }