Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions EXILED/Exiled.API/Extensions/GameObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// -----------------------------------------------------------------------
// <copyright file="GameObjectExtensions.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

#nullable enable
namespace Exiled.API.Extensions
{
using UnityEngine;

/// <summary>
/// A set of extensions for <see cref="GameObject"/>.
/// </summary>
public static class GameObjectExtensions
{
/// <summary>
/// Gets the global scale of a GameObject.
/// </summary>
/// <param name="gameObject">The <see cref="GameObject"/> to check.</param>
/// <returns>The global scale of the provided <see cref="GameObject"/>.</returns>
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;
}

/// <summary>
/// Sets the global scale of a GameObject.
/// </summary>
/// <param name="gameObject">The <see cref="GameObject"/> to modify.</param>
/// <param name="scale">The new scale.</param>
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}");
*/
}
}
}
5 changes: 4 additions & 1 deletion EXILED/Exiled.API/Features/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -438,6 +439,8 @@ internal virtual void ChangeOwner(Player oldOwner, Player newOwner)
Base.OnAdded(null);
}

// TODO: remove use of GetWorldScale after NW fix WaypointToy.

/// <summary>
/// Helper method for saving data between items and pickups.
/// </summary>
Expand All @@ -451,7 +454,7 @@ internal virtual void ReadPickupInfoBefore(Pickup pickup)
{
if (pickup is not null)
{
Scale = pickup.Scale;
Scale = pickup.GameObject.GetWorldScale();
}
}

Expand Down
6 changes: 3 additions & 3 deletions EXILED/Exiled.API/Features/Pickups/Pickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ public ushort Serial
/// </summary>
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();
}
}
Expand Down
Loading