quantum-space-buddies/QSB/Utility/Extensions.cs

65 lines
1.8 KiB
C#
Raw Normal View History

2021-02-06 22:03:10 +00:00
using OWML.Common;
2021-04-11 16:05:02 +00:00
using QSB.Player.TransformSync;
2021-01-26 14:07:17 +00:00
using QuantumUNET;
2021-02-06 22:03:10 +00:00
using System;
2021-02-09 10:03:13 +00:00
using System.Reflection;
2021-01-26 14:07:17 +00:00
using UnityEngine;
namespace QSB.Utility
{
2021-01-26 14:07:17 +00:00
public static class Extensions
2020-12-02 21:23:01 +00:00
{
2021-01-26 14:07:17 +00:00
// GAMEOBJECT
2020-12-02 21:23:01 +00:00
public static void Show(this GameObject gameObject) => SetVisibility(gameObject, true);
2020-12-02 21:23:01 +00:00
public static void Hide(this GameObject gameObject) => SetVisibility(gameObject, false);
2020-12-02 21:23:01 +00:00
private static void SetVisibility(GameObject gameObject, bool isVisible)
{
var renderers = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
foreach (var renderer in renderers)
{
renderer.enabled = isVisible;
}
}
2020-08-23 07:54:55 +00:00
2020-12-02 21:23:01 +00:00
public static GameObject InstantiateInactive(this GameObject original)
{
original.SetActive(false);
2021-02-06 22:03:10 +00:00
var copy = UnityEngine.Object.Instantiate(original);
2020-12-02 21:23:01 +00:00
original.SetActive(true);
return copy;
}
2020-08-23 07:54:55 +00:00
2020-12-14 21:20:53 +00:00
public static Transform InstantiateInactive(this Transform original) =>
original.gameObject.InstantiateInactive().transform;
2021-01-26 14:07:17 +00:00
// QNET
2021-03-09 16:43:41 +00:00
public static uint GetPlayerId(this QNetworkConnection connection)
2021-01-26 14:07:17 +00:00
{
var go = connection.PlayerControllers[0].Gameobject;
var controller = go.GetComponent<PlayerTransformSync>();
2021-03-09 16:43:41 +00:00
return controller.NetId.Value;
2021-01-26 14:07:17 +00:00
}
2021-02-06 22:03:10 +00:00
// C#
public static void SafeInvoke(this MulticastDelegate multicast, params object[] args)
{
foreach (var del in multicast.GetInvocationList())
{
try
{
del.DynamicInvoke(args);
}
2021-02-09 10:03:13 +00:00
catch (TargetInvocationException ex)
2021-02-06 22:03:10 +00:00
{
2021-02-09 10:03:13 +00:00
DebugLog.ToConsole($"Error invoking delegate! Message : {ex.InnerException.Message} Stack Trace : {ex.InnerException.StackTrace}", MessageType.Error);
2021-02-06 22:03:10 +00:00
}
}
}
2021-03-25 23:07:53 +00:00
2021-03-25 23:11:19 +00:00
public static float Map(this float value, float inputFrom, float inputTo, float outputFrom, float outputTo)
2021-03-25 23:07:53 +00:00
=> ((value - inputFrom) / (inputTo - inputFrom) * (outputTo - outputFrom)) + outputFrom;
2020-12-14 21:20:53 +00:00
}
2021-01-26 14:07:17 +00:00
}