com.soosvet_games.common_tools 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
using System.Collections.Generic;
|
|
2
|
+
using UnityEngine;
|
|
3
|
+
|
|
4
|
+
namespace SoosvetGames.CommonTools.Extensions
|
|
5
|
+
{
|
|
6
|
+
public static class TransformExtensions
|
|
7
|
+
{
|
|
8
|
+
public static void DestroyChildren( this Transform transform, bool isImmediate = false )
|
|
9
|
+
{
|
|
10
|
+
if ( transform == null ) return;
|
|
11
|
+
|
|
12
|
+
for ( int i = transform.childCount - 1; i >= 0; i-- )
|
|
13
|
+
{
|
|
14
|
+
Transform child;
|
|
15
|
+
try
|
|
16
|
+
{
|
|
17
|
+
child = transform.GetChild( i );
|
|
18
|
+
}
|
|
19
|
+
catch
|
|
20
|
+
{
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if ( !child ) continue;
|
|
25
|
+
|
|
26
|
+
if ( isImmediate )
|
|
27
|
+
{
|
|
28
|
+
Object.DestroyImmediate( child.gameObject );
|
|
29
|
+
}
|
|
30
|
+
else
|
|
31
|
+
{
|
|
32
|
+
Object.Destroy( child.gameObject );
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public static IEnumerable< Transform > GetChildren( this Transform transform )
|
|
38
|
+
{
|
|
39
|
+
var i = 0;
|
|
40
|
+
|
|
41
|
+
while ( i < transform.childCount )
|
|
42
|
+
{
|
|
43
|
+
yield return transform.GetChild( i );
|
|
44
|
+
++i;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public static T GetOrAddComponent< T >( this GameObject gameObject ) where T : Component
|
|
49
|
+
{
|
|
50
|
+
var result = gameObject.GetComponent< T >();
|
|
51
|
+
return result == null ? gameObject.AddComponent< T >() : result;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Collections.Generic;
|
|
3
|
+
|
|
4
|
+
namespace SoosvetGames.CommonTools
|
|
5
|
+
{
|
|
6
|
+
public class ServiceLocator
|
|
7
|
+
{
|
|
8
|
+
private readonly Dictionary< Type, object > _services = new();
|
|
9
|
+
|
|
10
|
+
public void Clear()
|
|
11
|
+
{
|
|
12
|
+
_services.Clear();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public void Register< T >( T service )
|
|
16
|
+
{
|
|
17
|
+
_services[ typeof(T) ] = service;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public T GetAs< T >()
|
|
21
|
+
{
|
|
22
|
+
return (T)_services[ typeof(T) ];
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|