Skip to content
This repository was archived by the owner on Mar 21, 2026. It is now read-only.

Commit f7b3b16

Browse files
committed
Done documenting
1 parent 26dfc2b commit f7b3b16

11 files changed

Lines changed: 334 additions & 285 deletions

EnumerableExtension.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public static List<T> _Distinct<T>(this List<T> list)
1414
}
1515

1616
public static Dictionary<TKey, TValue>
17-
Dictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> list) =>
17+
MakeDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> list) =>
1818
list.Select(x => x).ToDictionary(x => x.Key, y => y.Value);
1919

20-
public static T Random<T>(this List<T> list)
20+
public static T Random<T>(this IList<T> list)
2121
{
2222
if (list == null || list.Count == 0) return default;
2323
return list[UnityEngine.Random.Range(0, list.Count)];
@@ -40,16 +40,15 @@ public static List<Vector3> RoundCords(this List<Vector3> list)
4040
public static List<Vector2> RoundCords(this List<Vector2> list)
4141
{
4242
var result = new List<Vector2>();
43-
for (var i = 0; i < list.Count; i++) result.Add(new Vector2((int)list[i].x, (int)list[i].y));
43+
for (var i = 0; i < list.Count; i++) result.Add(list[i].RoundCords());
4444

4545
return result;
4646
}
4747

48-
public static string GetString<T>(this IEnumerable<T> list, string separator = ", ")
49-
{
50-
return string.Join(separator, list);
51-
}
48+
public static string GetString<T>(this IEnumerable<T> list, string separator = ", ") =>
49+
string.Join(separator, list);
5250

51+
[Obsolete]
5352
public static void TryAdd<T>(this List<T> sequence, T item)
5453
{
5554
if (!sequence.Contains(item)) sequence.Add(item);
@@ -90,4 +89,24 @@ public static T Nearest<T>(this IEnumerable<T> list, Vector3 nearestTo) where T
9089

9190
return current;
9291
}
92+
93+
public static Vector3 Nearest(this IEnumerable<Vector3> list, Vector3 nearestTo)
94+
{
95+
var current = default(Vector3);
96+
97+
float oldDistance = int.MaxValue;
98+
if (list == null || list.Count() == 0) return current;
99+
foreach (var pos_ in list)
100+
{
101+
var pos = pos_;
102+
var dist = Utils.DistanceXZ(nearestTo, pos);
103+
if (dist < oldDistance)
104+
{
105+
current = pos_;
106+
oldDistance = dist;
107+
}
108+
}
109+
110+
return current;
111+
}
93112
}

GameObjectExtension.cs

Lines changed: 6 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Reflection;
1+
using System.ComponentModel;
2+
using System.Reflection;
23
using UnityEngine.UI;
4+
using Component = UnityEngine.Component;
35

46
namespace JFUtils;
57

@@ -13,51 +15,17 @@ public static string GetPrefabName(this GameObject gameObject)
1315
return prefabName;
1416
}
1517

16-
public static string GetPrefabName<T>(this T gameObject) where T : MonoBehaviour
18+
public static string GetPrefabName<T>(this T gameObject) where T : Component
1719
{
1820
var prefabName = Utils.GetPrefabName(gameObject.gameObject);
1921
for (var i = 0; i < 80; i++) prefabName = prefabName.Replace($" ({i})", "");
2022

2123
return prefabName;
2224
}
2325

24-
/// <summary>
25-
/// Extends GameObject with a shortcut for the Unity bool operator override.
26-
/// </summary>
27-
/// <summary>
28-
/// Facilitates use of null propagation operator for unity GameObjects by respecting op_equality.
29-
/// </summary>
30-
/// <param name="this"> this </param>
31-
/// <returns>Returns null when GameObject.op_equality returns false.</returns>
32-
public static GameObject OrNull(this GameObject @this) { return @this ? @this : null; }
26+
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component =>
27+
gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>();
3328

34-
/// <summary>
35-
/// Facilitates use of null propagation operator for unity MonBehaviours by respecting op_equality.
36-
/// </summary>
37-
/// <typeparam name="T">Any type that inherits MonoBehaviour</typeparam>
38-
/// <param name="this">this</param>
39-
/// <returns>Returns null when MonoBehaviours.op_equality returns false.</returns>
40-
public static T OrNull<T>(this T @this) where T : Object { return @this ? @this : null; }
41-
42-
/// <summary>
43-
/// Returns the component of Type type. If one doesn't already exist on the GameObject it will be added.
44-
/// </summary>
45-
/// <remarks>Source: https://wiki.unity3d.com/index.php/GetOrAddComponent</remarks>
46-
/// <typeparam name="T">The type of Component to return.</typeparam>
47-
/// <param name="gameObject">The GameObject this Component is attached to.</param>
48-
/// <returns>Component</returns>
49-
public static T GetOrAddComponent<T>(this GameObject gameObject) where T : Component
50-
{
51-
return gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>();
52-
}
53-
54-
/// <summary>
55-
/// Adds a new copy of the provided component to a gameObject
56-
/// </summary>
57-
/// <param name="gameObject"></param>
58-
/// <param name="duplicate"></param>
59-
/// <typeparam name="T"></typeparam>
60-
/// <returns></returns>
6129
public static Component AddComponentCopy<T>(this GameObject gameObject, T duplicate) where T : Component
6230
{
6331
var target = gameObject.AddComponent(duplicate.GetType());
@@ -80,143 +48,4 @@ public static Component AddComponentCopy<T>(this GameObject gameObject, T duplic
8048

8149
return target;
8250
}
83-
84-
internal static GameObject SetToTextHeight(this GameObject go)
85-
{
86-
go.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical,
87-
go.GetComponentInChildren<Text>().preferredHeight + 3f);
88-
return go;
89-
}
90-
91-
internal static GameObject SetUpperLeft(this GameObject go)
92-
{
93-
var rect = go.GetComponent<RectTransform>();
94-
rect.anchorMax = new Vector2(0, 1);
95-
rect.anchorMin = new Vector2(0, 1);
96-
rect.pivot = new Vector2(0, 1);
97-
rect.anchoredPosition = new Vector2(0, 0);
98-
return go;
99-
}
100-
101-
internal static GameObject SetMiddleLeft(this GameObject go)
102-
{
103-
var rect = go.GetComponent<RectTransform>();
104-
rect.anchorMax = new Vector2(0, 0.5f);
105-
rect.anchorMin = new Vector2(0, 0.5f);
106-
rect.pivot = new Vector2(0, 0.5f);
107-
rect.anchoredPosition = new Vector2(0, 0f);
108-
return go;
109-
}
110-
111-
internal static GameObject SetBottomLeft(this GameObject go)
112-
{
113-
var rect = go.GetComponent<RectTransform>();
114-
rect.anchorMax = new Vector2(0, 0);
115-
rect.anchorMin = new Vector2(0, 0);
116-
rect.pivot = new Vector2(0, 0);
117-
rect.anchoredPosition = new Vector2(0, 0f);
118-
return go;
119-
}
120-
121-
internal static GameObject SetUpperRight(this GameObject go)
122-
{
123-
var rect = go.GetComponent<RectTransform>();
124-
rect.anchorMax = new Vector2(1, 1);
125-
rect.anchorMin = new Vector2(1, 1);
126-
rect.pivot = new Vector2(1, 1);
127-
rect.anchoredPosition = new Vector2(0, 0);
128-
return go;
129-
}
130-
131-
internal static GameObject SetMiddleRight(this GameObject go)
132-
{
133-
var rect = go.GetComponent<RectTransform>();
134-
rect.anchorMax = new Vector2(1, 0.5f);
135-
rect.anchorMin = new Vector2(1, 0.5f);
136-
rect.pivot = new Vector2(1, 0.5f);
137-
rect.anchoredPosition = new Vector2(0, 0f);
138-
return go;
139-
}
140-
141-
internal static GameObject SetBottomRight(this GameObject go)
142-
{
143-
var rect = go.GetComponent<RectTransform>();
144-
rect.anchorMax = new Vector2(1, 0);
145-
rect.anchorMin = new Vector2(1, 0);
146-
rect.pivot = new Vector2(1f, 0f);
147-
rect.anchoredPosition = new Vector2(0, 0);
148-
return go;
149-
}
150-
151-
internal static GameObject SetUpperCenter(this GameObject go)
152-
{
153-
var rect = go.GetComponent<RectTransform>();
154-
rect.anchorMax = new Vector2(0.5f, 1f);
155-
rect.anchorMin = new Vector2(0.5f, 1f);
156-
rect.pivot = new Vector2(0.5f, 1f);
157-
rect.anchoredPosition = new Vector2(0, 0);
158-
return go;
159-
}
160-
161-
internal static GameObject SetMiddleCenter(this GameObject go)
162-
{
163-
var rect = go.GetComponent<RectTransform>();
164-
rect.anchorMax = new Vector2(0.5f, 0.5f);
165-
rect.anchorMin = new Vector2(0.5f, 0.5f);
166-
rect.pivot = new Vector2(0.5f, 0.5f);
167-
rect.anchoredPosition = new Vector2(0, 0);
168-
return go;
169-
}
170-
171-
internal static GameObject SetBottomCenter(this GameObject go)
172-
{
173-
var rect = go.GetComponent<RectTransform>();
174-
rect.anchorMax = new Vector2(0.5f, 0);
175-
rect.anchorMin = new Vector2(0.5f, 0);
176-
rect.pivot = new Vector2(0.5f, 0f);
177-
rect.anchoredPosition = new Vector2(0, 0);
178-
return go;
179-
}
180-
181-
internal static GameObject SetSize(this GameObject go, float width, float height)
182-
{
183-
var rect = go.GetComponent<RectTransform>();
184-
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
185-
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
186-
return go;
187-
}
188-
189-
internal static GameObject SetWidth(this GameObject go, float width)
190-
{
191-
var rect = go.GetComponent<RectTransform>();
192-
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
193-
return go;
194-
}
195-
196-
internal static GameObject SetHeight(this GameObject go, float height)
197-
{
198-
var rect = go.GetComponent<RectTransform>();
199-
rect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
200-
return go;
201-
}
202-
203-
internal static float GetWidth(this GameObject go)
204-
{
205-
var rect = go.GetComponent<RectTransform>();
206-
return rect.rect.width;
207-
}
208-
209-
internal static float GetHeight(this GameObject go)
210-
{
211-
var rect = go.GetComponent<RectTransform>();
212-
return rect.rect.height;
213-
}
214-
215-
internal static float GetTextHeight(this GameObject go) { return go.GetComponent<Text>().preferredHeight; }
216-
217-
internal static GameObject SetText(this GameObject go, string text)
218-
{
219-
go.GetComponent<Text>().text = text;
220-
return go;
221-
}
22251
}

JFUtils.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
<Compile Include="MonoBehaviourExtension.cs" />
104104
<Compile Include="ObjectExtension.cs" />
105105
<Compile Include="Properties\AssemblyInfo.cs" />
106-
<Compile Include="RectTransformExtension.cs" />
106+
<Compile Include="RectExtension.cs" />
107107
<Compile Include="RendererExtension.cs" />
108108
<Compile Include="SimpleVector2.cs" />
109109
<Compile Include="SimpleVector3.cs" />

ModBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public static AssetBundle LoadAssetBundle(string filename)
2727
var assembly = Assembly.GetExecutingAssembly();
2828
string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(filename));
2929

30-
using Stream stream = assembly.GetManifestResourceStream(resourceName)!;
31-
bundle = AssetBundle.LoadFromStream(stream);
30+
using (Stream stream = assembly.GetManifestResourceStream(resourceName)!)
31+
bundle = AssetBundle.LoadFromStream(stream);
3232
return bundle;
3333
}
3434

MonoBehaviourExtension.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ public static T SetActiveGO<T>(this T behaviour, bool flag) where T : Component
77
behaviour.gameObject.SetActive(flag);
88
return behaviour;
99
}
10+
11+
public static T ToggleActiveGO<T>(this T behaviour) where T : Component
12+
{
13+
behaviour.gameObject.SetActive(!behaviour.gameObject.activeSelf);
14+
return behaviour;
15+
}
1016
}

0 commit comments

Comments
 (0)