This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEgo.cs
66 lines (55 loc) · 1.99 KB
/
Ego.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using UnityEngine;
public static class Ego
{
public static EgoComponent AddGameObject( GameObject gameObject )
{
var egoComponent = AddGameObjectToChildren( gameObject.transform );
EgoEvents<AddedGameObject>.AddEvent( new AddedGameObject( gameObject, egoComponent ) );
return egoComponent;
}
private static EgoComponent AddGameObjectToChildren( Transform transform )
{
for (int i = 0; i < transform.childCount; i++)
{
AddGameObjectToChildren( transform.GetChild( i ) );
}
var egoComponent = transform.GetComponent<EgoComponent>();
if( egoComponent == null )
{
egoComponent = transform.gameObject.AddComponent<EgoComponent>();
}
egoComponent.CreateMask();
return egoComponent;
}
public static C AddComponent<C>( EgoComponent egoComponent ) where C : Component
{
C component = null;
if( !egoComponent.TryGetComponents<C>( out component ) )
{
component = egoComponent.gameObject.AddComponent<C>();
egoComponent.mask[ ComponentIDs.Get( typeof( C ) ) ] = true;
EgoEvents<AddedComponent<C>>.AddEvent( new AddedComponent<C>( component, egoComponent ) );
}
return component;
}
public static void DestroyGameObject( EgoComponent egoComponent )
{
var gameObject = egoComponent.gameObject;
EgoEvents<DestroyedGameObject>.AddEvent( new DestroyedGameObject( gameObject, egoComponent ) );
EgoCleanUp.Destroy( egoComponent.gameObject );
}
public static bool DestroyComponent<C>( EgoComponent egoComponent ) where C : Component
{
C component = null;
if( !egoComponent.TryGetComponents<C>( out component ) ){ return false; }
var e = new DestroyedComponent<C>( component, egoComponent );
EgoEvents<DestroyedComponent<C>>.AddEvent( e );
EgoCleanUp<C>.Destroy( egoComponent, component );
return true;
}
public static void SetParent( EgoComponent parent, EgoComponent child )
{
if( child == null ){ Debug.LogWarning( "Cannot set the Parent of a null Child" ); }
EgoEvents<SetParent>.AddEvent( new SetParent( parent, child ) );
}
}