extensions

This commit is contained in:
Sveske Juice 2024-02-02 12:03:21 -08:00
parent 7b8222da45
commit 692cbf4d10
11 changed files with 663 additions and 0 deletions

View File

@ -0,0 +1,271 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!29 &1
OcclusionCullingSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_OcclusionBakeSettings:
smallestOccluder: 5
smallestHole: 0.25
backfaceThreshold: 100
m_SceneGUID: 00000000000000000000000000000000
m_OcclusionCullingData: {fileID: 0}
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 9
m_Fog: 0
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
m_FogDensity: 0.01
m_LinearFogStart: 0
m_LinearFogEnd: 300
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
m_AmbientIntensity: 1
m_AmbientMode: 3
m_SubtractiveShadowColor: {r: 0.42, g: 0.478, b: 0.627, a: 1}
m_SkyboxMaterial: {fileID: 0}
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
m_HaloTexture: {fileID: 0}
m_SpotCookie: {fileID: 10001, guid: 0000000000000000e000000000000000, type: 0}
m_DefaultReflectionMode: 0
m_DefaultReflectionResolution: 128
m_ReflectionBounces: 1
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 12
m_GIWorkflowMode: 1
m_GISettings:
serializedVersion: 2
m_BounceScale: 1
m_IndirectOutputScale: 1
m_AlbedoBoost: 1
m_EnvironmentLightingMode: 0
m_EnableBakedLightmaps: 0
m_EnableRealtimeLightmaps: 0
m_LightmapEditorSettings:
serializedVersion: 12
m_Resolution: 2
m_BakeResolution: 40
m_AtlasSize: 1024
m_AO: 0
m_AOMaxDistance: 1
m_CompAOExponent: 1
m_CompAOExponentDirect: 0
m_ExtractAmbientOcclusion: 0
m_Padding: 2
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1
m_FinalGatherRayCount: 256
m_ReflectionCompression: 2
m_MixedBakeMode: 2
m_BakeBackend: 1
m_PVRSampling: 1
m_PVRDirectSampleCount: 32
m_PVRSampleCount: 512
m_PVRBounces: 2
m_PVREnvironmentSampleCount: 256
m_PVREnvironmentReferencePointCount: 2048
m_PVRFilteringMode: 1
m_PVRDenoiserTypeDirect: 1
m_PVRDenoiserTypeIndirect: 1
m_PVRDenoiserTypeAO: 1
m_PVRFilterTypeDirect: 0
m_PVRFilterTypeIndirect: 0
m_PVRFilterTypeAO: 0
m_PVREnvironmentMIS: 1
m_PVRCulling: 1
m_PVRFilteringGaussRadiusDirect: 1
m_PVRFilteringGaussRadiusIndirect: 5
m_PVRFilteringGaussRadiusAO: 2
m_PVRFilteringAtrousPositionSigmaDirect: 0.5
m_PVRFilteringAtrousPositionSigmaIndirect: 2
m_PVRFilteringAtrousPositionSigmaAO: 1
m_ExportTrainingData: 0
m_TrainingDataDestination: TrainingData
m_LightProbeSampleCountMultiplier: 4
m_LightingDataAsset: {fileID: 0}
m_LightingSettings: {fileID: 0}
--- !u!196 &4
NavMeshSettings:
serializedVersion: 2
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 3
agentTypeID: 0
agentRadius: 0.5
agentHeight: 2
agentSlope: 45
agentClimb: 0.4
ledgeDropHeight: 0
maxJumpAcrossDistance: 0
minRegionArea: 2
manualCellSize: 0
cellSize: 0.16666667
manualTileSize: 0
tileSize: 256
buildHeightMesh: 0
maxJobWorkers: 0
preserveTilesOutsideBounds: 0
debug:
m_Flags: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &22879098
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 22879099}
- component: {fileID: 22879100}
m_Layer: 0
m_Name: RopeSImulator
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &22879099
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 22879098}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &22879100
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 22879098}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 27ac133d9e10e544ba603e07122e3359, type: 3}
m_Name:
m_EditorClassIdentifier:
gravity: 5
solveIterations: 10
constrainStickMinLength: 0.1
testPos: {x: 0, y: 0}
--- !u!1 &1993214202
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1993214205}
- component: {fileID: 1993214204}
- component: {fileID: 1993214203}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!81 &1993214203
AudioListener:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1993214202}
m_Enabled: 1
--- !u!20 &1993214204
Camera:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1993214202}
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
m_Iso: 200
m_ShutterSpeed: 0.005
m_Aperture: 16
m_FocusDistance: 10
m_FocalLength: 50
m_BladeCount: 5
m_Curvature: {x: 2, y: 11}
m_BarrelClipping: 0.25
m_Anamorphism: 0
m_SensorSize: {x: 36, y: 24}
m_LensShift: {x: 0, y: 0}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: 0.3
far clip plane: 1000
field of view: 60
orthographic: 1
orthographic size: 5
m_Depth: -1
m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_RenderingPath: -1
m_TargetTexture: {fileID: 0}
m_TargetDisplay: 0
m_TargetEye: 3
m_HDR: 1
m_AllowMSAA: 1
m_AllowDynamicResolution: 0
m_ForceIntoRT: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: 0.022
--- !u!4 &1993214205
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1993214202}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: -10}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1660057539 &9223372036854775807
SceneRoots:
m_ObjectHideFlags: 0
m_Roots:
- {fileID: 1993214205}
- {fileID: 22879099}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: cfc0abf7f9f0d774f918832b04042f61
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,131 @@
using UnityEngine;
using System.Linq;
namespace UnityUtils
{
public static class GameObjectExtensions
{
/// <summary>
/// This method is used to hide the GameObject in the Hierarchy view.
/// </summary>
/// <param name="gameObject"></param>
public static void HideInHierarchy(this GameObject gameObject)
{
gameObject.hideFlags = HideFlags.HideInHierarchy;
}
/// <summary>
/// Gets a component of the given type attached to the GameObject. If that type of component does not exist, it adds one.
/// </summary>
/// <remarks>
/// This method is useful when you don't know if a GameObject has a specific type of component,
/// but you want to work with that component regardless. Instead of checking and adding the component manually,
/// you can use this method to do both operations in one line.
/// </remarks>
/// <typeparam name="T">The type of the component to get or add.</typeparam>
/// <param name="gameObject">The GameObject to get the component from or add the component to.</param>
/// <returns>The existing component of the given type, or a new one if no such component exists.</returns>
public static T GetOrAdd<T>(this GameObject gameObject) where T : Component
{
T component = gameObject.GetComponent<T>();
if (!component) component = gameObject.AddComponent<T>();
return component;
}
/// <summary>
/// Returns the object itself if it exists, null otherwise.
/// </summary>
/// <remarks>
/// This method helps differentiate between a null reference and a destroyed Unity object. Unity's "== null" check
/// can incorrectly return true for destroyed objects, leading to misleading behaviour. The OrNull method use
/// Unity's "null check", and if the object has been marked for destruction, it ensures an actual null reference is returned,
/// aiding in correctly chaining operations and preventing NullReferenceExceptions.
/// </remarks>
/// <typeparam name="T">The type of the object.</typeparam>
/// <param name="obj">The object being checked.</param>
/// <returns>The object itself if it exists and not destroyed, null otherwise.</returns>
public static T OrNull<T>(this T obj) where T : Object => obj ? obj : null;
/// <summary>
/// Destroys all children of the game object
/// </summary>
/// <param name="gameObject">GameObject whose children are to be destroyed.</param>
public static void DestroyChildren(this GameObject gameObject)
{
gameObject.transform.DestroyChildren();
}
/// <summary>
/// Immediately destroys all children of the given GameObject.
/// </summary>
/// <param name="gameObject">GameObject whose children are to be destroyed.</param>
public static void DestroyChildrenImmediate(this GameObject gameObject)
{
gameObject.transform.DestroyChildrenImmediate();
}
/// <summary>
/// Enables all child GameObjects associated with the given GameObject.
/// </summary>
/// <param name="gameObject">GameObject whose child GameObjects are to be enabled.</param>
public static void EnableChildren(this GameObject gameObject)
{
gameObject.transform.EnableChildren();
}
/// <summary>
/// Disables all child GameObjects associated with the given GameObject.
/// </summary>
/// <param name="gameObject">GameObject whose child GameObjects are to be disabled.</param>
public static void DisableChildren(this GameObject gameObject)
{
gameObject.transform.DisableChildren();
}
/// <summary>
/// Resets the GameObject's transform's position, rotation, and scale to their default values.
/// </summary>
/// <param name="gameObject">GameObject whose transformation is to be reset.</param>
public static void ResetTransformation(this GameObject gameObject)
{
gameObject.transform.Reset();
}
/// <summary>
/// Returns the hierarchical path in the Unity scene hierarchy for this GameObject.
/// </summary>
/// <param name="gameObject">The GameObject to get the path for.</param>
/// <returns>A string representing the full hierarchical path of this GameObject in the Unity scene.
/// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending
/// with the name of the specified GameObjects parent.</returns>
public static string Path(this GameObject gameObject)
{
return "/" + string.Join("/",
gameObject.GetComponentsInParent<Transform>().Select(t => t.name).Reverse().ToArray());
}
/// <summary>
/// Returns the full hierarchical path in the Unity scene hierarchy for this GameObject.
/// </summary>
/// <param name="gameObject">The GameObject to get the path for.</param>
/// <returns>A string representing the full hierarchical path of this GameObject in the Unity scene.
/// This is a '/'-separated string where each part is the name of a parent, starting from the root parent and ending
/// with the name of the specified GameObject itself.</returns>
public static string PathFull(this GameObject gameObject)
{
return gameObject.Path() + "/" + gameObject.name;
}
/// <summary>
/// Recursively sets the provided layer for this GameObject and all of its descendants in the Unity scene hierarchy.
/// </summary>
/// <param name="gameObject">The GameObject to set layers for.</param>
/// <param name="layer">The layer number to set for GameObject and all of its descendants.</param>
public static void SetLayersRecursively(this GameObject gameObject, int layer)
{
gameObject.layer = layer;
gameObject.transform.ForEveryChild(child => child.gameObject.SetLayersRecursively(layer));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1fc386696b14b914cbae26f3c3a51a5a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UnityUtils
{
public static class TransformExtensions
{
/// <summary>
/// Retrieves all the children of a given Transform.
/// </summary>
/// <remarks>
/// This method can be used with LINQ to perform operations on all child Transforms. For example,
/// you could use it to find all children with a specific tag, to disable all children, etc.
/// Transform implements IEnumerable and the GetEnumerator method which returns an IEnumerator of all its children.
/// </remarks>
/// <param name="parent">The Transform to retrieve children from.</param>
/// <returns>An IEnumerable&lt;Transform&gt; containing all the child Transforms of the parent.</returns>
public static IEnumerable<Transform> Children(this Transform parent)
{
foreach (Transform child in parent)
{
yield return child;
}
}
/// <summary>
/// Resets transform's position, scale and rotation
/// </summary>
/// <param name="transform">Transform to use</param>
public static void Reset(this Transform transform)
{
transform.position = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.localScale = Vector3.one;
}
/// <summary>
/// Destroys all child game objects of the given transform.
/// </summary>
/// <param name="parent">The Transform whose child game objects are to be destroyed.</param>
public static void DestroyChildren(this Transform parent)
{
parent.ForEveryChild(child => Object.Destroy(child.gameObject));
}
/// <summary>
/// Immediately destroys all child game objects of the given transform.
/// </summary>
/// <param name="parent">The Transform whose child game objects are to be immediately destroyed.</param>
public static void DestroyChildrenImmediate(this Transform parent)
{
parent.ForEveryChild(child => Object.DestroyImmediate(child.gameObject));
}
/// <summary>
/// Enables all child game objects of the given transform.
/// </summary>
/// <param name="parent">The Transform whose child game objects are to be enabled.</param>
public static void EnableChildren(this Transform parent)
{
parent.ForEveryChild(child => child.gameObject.SetActive(true));
}
/// <summary>
/// Disables all child game objects of the given transform.
/// </summary>
/// <param name="parent">The Transform whose child game objects are to be disabled.</param>
public static void DisableChildren(this Transform parent)
{
parent.ForEveryChild(child => child.gameObject.SetActive(false));
}
/// <summary>
/// Executes a specified action for each child of a given transform.
/// </summary>
/// <param name="parent">The parent transform.</param>
/// <param name="action">The action to be performed on each child.</param>
/// <remarks>
/// This method iterates over all child transforms in reverse order and executes a given action on them.
/// The action is a delegate that takes a Transform as parameter.
/// </remarks>
public static void ForEveryChild(this Transform parent, System.Action<Transform> action)
{
for (var i = parent.childCount - 1; i >= 0; i--)
{
action(parent.GetChild(i));
}
}
[Obsolete("Renamed to ForEveryChild")]
static void PerformActionOnChildren(this Transform parent, System.Action<Transform> action)
{
parent.ForEveryChild(action);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3bc029dcadc3ccd40ad4c6a3f51eac64
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,35 @@
using UnityEngine;
namespace UnityUtils
{
public static class Vector2Extensions
{
/// <summary>
/// Adds to any x y values of a Vector2
/// </summary>
public static Vector2 Add(this Vector2 vector2, float x = 0, float y = 0)
{
return new Vector2(vector2.x + x, vector2.y + y);
}
/// <summary>
/// Sets any x y values of a Vector2
/// </summary>
public static Vector2 With(this Vector2 vector2, float? x = null, float? y = null)
{
return new Vector2(x ?? vector2.x, y ?? vector2.y);
}
/// <summary>
/// Returns a Boolean indicating whether the current Vector2 is in a given range from another Vector2
/// </summary>
/// <param name="current">The current Vector2 position</param>
/// <param name="target">The Vector2 position to compare against</param>
/// <param name="range">The range value to compare against</param>
/// <returns>True if the current Vector2 is in the given range from the target Vector2, false otherwise</returns>
public static bool InRangeOf(this Vector2 current, Vector2 target, float range)
{
return (current - target).sqrMagnitude <= range * range;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 69ebf1c43ff162f488b3b1d0ae557d78
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,60 @@
using UnityEngine;
namespace UnityUtils
{
public static class Vector3Extensions
{
/// <summary>
/// Sets any x y z values of a Vector3
/// </summary>
public static Vector3 With(this Vector3 vector, float? x = null, float? y = null, float? z = null)
{
return new Vector3(x ?? vector.x, y ?? vector.y, z ?? vector.z);
}
/// <summary>
/// Adds to any x y z values of a Vector3
/// </summary>
public static Vector3 Add(this Vector3 vector, float x = 0, float y = 0, float z = 0)
{
return new Vector3(vector.x + x, vector.y + y, vector.z + z);
}
/// <summary>
/// Returns a Boolean indicating whether the current Vector3 is in a given range from another Vector3
/// </summary>
/// <param name="current">The current Vector3 position</param>
/// <param name="target">The Vector3 position to compare against</param>
/// <param name="range">The range value to compare against</param>
/// <returns>True if the current Vector3 is in the given range from the target Vector3, false otherwise</returns>
public static bool InRangeOf(this Vector3 current, Vector3 target, float range)
{
return (current - target).sqrMagnitude <= range * range;
}
/// <summary>
/// Divides two Vector3 objects component-wise.
/// </summary>
/// <remarks>
/// For each component in v0 (x, y, z), it is divided by the corresponding component in v1 if the component in v1 is not zero.
/// Otherwise, the component in v0 remains unchanged.
/// </remarks>
/// <example>
/// Use 'ComponentDivide' to scale a game object proportionally:
/// <code>
/// myObject.transform.localScale = originalScale.ComponentDivide(targetDimensions);
/// </code>
/// This scales the object size to fit within the target dimensions while maintaining its original proportions.
///</example>
/// <param name="v0">The Vector3 object that this method extends.</param>
/// <param name="v1">The Vector3 object by which v0 is divided.</param>
/// <returns>A new Vector3 object resulting from the component-wise division.</returns>
public static Vector3 ComponentDivide(this Vector3 v0, Vector3 v1)
{
return new Vector3(
v1.x != 0 ? v0.x / v1.x : v0.x,
v1.y != 0 ? v0.y / v1.y : v0.y,
v1.z != 0 ? v0.z / v1.z : v0.z);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fde3957705d25994292dd59ab09ce951
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,17 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &1
MonoBehaviour:
m_ObjectHideFlags: 53
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: de02f9e1d18f588468e474319d09a723, type: 3}
m_Name:
m_EditorClassIdentifier:
shaderVariantLimit: 128
customInterpolatorErrorThreshold: 32
customInterpolatorWarningThreshold: 16