Skip to content

Commit 7d877ea

Browse files
committed
Initial commit.
0 parents  commit 7d877ea

9 files changed

Lines changed: 280 additions & 0 deletions

File tree

.gitattributes

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
###############################################################################
2+
# Set default behavior to automatically normalize line endings.
3+
###############################################################################
4+
* text=auto
5+
6+
###############################################################################
7+
# Set default behavior for command prompt diff.
8+
#
9+
# This is need for earlier builds of msysgit that does not have it on by
10+
# default for csharp files.
11+
# Note: This is only used by command line
12+
###############################################################################
13+
#*.cs diff=csharp
14+
15+
###############################################################################
16+
# Set the merge driver for project and solution files
17+
#
18+
# Merging from the command prompt will add diff markers to the files if there
19+
# are conflicts (Merging from VS is not affected by the settings below, in VS
20+
# the diff markers are never inserted). Diff markers may cause the following
21+
# file extensions to fail to load in VS. An alternative would be to treat
22+
# these files as binary and thus will always conflict and require user
23+
# intervention with every merge. To do so, just uncomment the entries below
24+
###############################################################################
25+
#*.sln merge=binary
26+
#*.csproj merge=binary
27+
#*.vbproj merge=binary
28+
#*.vcxproj merge=binary
29+
#*.vcproj merge=binary
30+
#*.dbproj merge=binary
31+
#*.fsproj merge=binary
32+
#*.lsproj merge=binary
33+
#*.wixproj merge=binary
34+
#*.modelproj merge=binary
35+
#*.sqlproj merge=binary
36+
#*.wwaproj merge=binary
37+
38+
###############################################################################
39+
# behavior for image files
40+
#
41+
# image files are treated as binary by default.
42+
###############################################################################
43+
#*.jpg binary
44+
#*.png binary
45+
#*.gif binary
46+
47+
###############################################################################
48+
# diff behavior for common document formats
49+
#
50+
# Convert binary document formats to text before diffing them. This feature
51+
# is only available from the command line. Turn it on by uncommenting the
52+
# entries below.
53+
###############################################################################
54+
#*.doc diff=astextplain
55+
#*.DOC diff=astextplain
56+
#*.docx diff=astextplain
57+
#*.DOCX diff=astextplain
58+
#*.dot diff=astextplain
59+
#*.DOT diff=astextplain
60+
#*.pdf diff=astextplain
61+
#*.PDF diff=astextplain
62+
#*.rtf diff=astextplain
63+
#*.RTF diff=astextplain

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
**/obj/
2+
**/bin/
3+
*.suo
4+
*.csproj.user
5+
*.vshost.exe.*
6+

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Unity Event Aggregator
2+
======================
3+
4+
Event aggregation in Unity made easy! Decouple your GameObjects for simpler and cleaner code.

src/UnityEventAggregator.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.21005.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnityEventAggregator", "UnityEventAggregator\UnityEventAggregator.csproj", "{B4CEC7B5-0CA8-4FAE-BFBD-1226710608CE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B4CEC7B5-0CA8-4FAE-BFBD-1226710608CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B4CEC7B5-0CA8-4FAE-BFBD-1226710608CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B4CEC7B5-0CA8-4FAE-BFBD-1226710608CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B4CEC7B5-0CA8-4FAE-BFBD-1226710608CE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using UnityEngine;
6+
7+
namespace UnityEventAggregator
8+
{
9+
public static class EventAggregator
10+
{
11+
private static readonly Dictionary<Type, List<object>> _cache = new Dictionary<Type, List<object>>();
12+
13+
/// <summary>
14+
/// Register the game object to listen for events of type T.
15+
/// </summary>
16+
/// <typeparam name="T">The type of event being listened for.</typeparam>
17+
/// <param name="obj"></param>
18+
public static void Register<T>(this object obj)
19+
{
20+
if (!_cache.ContainsKey(typeof(T))) _cache[typeof(T)] = new List<object>();
21+
_cache[typeof(T)].Add(obj);
22+
}
23+
24+
/// <summary>
25+
/// Removes the registration for listening to events of type T.
26+
/// </summary>
27+
/// <typeparam name="T">The type of event to no longer listen for.</typeparam>
28+
/// <param name="obj"></param>
29+
public static void UnRegister<T>(this object obj)
30+
{
31+
if (!_cache.ContainsKey(typeof(T))) return;
32+
_cache[typeof(T)].Remove(obj);
33+
}
34+
35+
/// <summary>
36+
/// Notifies all listeners of event type T.
37+
/// </summary>
38+
/// <typeparam name="T">The type of event being notified.</typeparam>
39+
/// <param name="message"></param>
40+
public static void SendMessage<T>(T message)
41+
{
42+
_cache[message.GetType()].Each(x => ((IListener<T>)x).Handle(message));
43+
}
44+
45+
/// <summary>
46+
/// Creates the cache for objects that listen to each event
47+
/// </summary>
48+
/// <typeparam name="T">Searches through all active GameObjects for those listening for event T and auto registers them.</typeparam>
49+
public static void UpdateCache<T>()
50+
{
51+
var type = typeof(IListener<T>);
52+
var list = new List<object>();
53+
54+
// Get all types of IListener<T>
55+
Assembly.GetExecutingAssembly()
56+
.GetTypes()
57+
.Where(x => x.GetInterfaces().Contains(type))
58+
.Each(x =>
59+
{
60+
// Add existing unity objects that listen for event
61+
GameObject.FindObjectsOfType<MonoBehaviour>()
62+
.Where(t => t.GetType() == x)
63+
.Each(list.Add);
64+
});
65+
66+
_cache[typeof(T)] = list;
67+
}
68+
}
69+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace UnityEventAggregator
5+
{
6+
public static class Extensions
7+
{
8+
public static void Each<T>(this IEnumerable<T> enumerable, Action<T> action)
9+
{
10+
if (enumerable == null) return;
11+
foreach (var e in enumerable)
12+
action(e);
13+
}
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace UnityEventAggregator
2+
{
3+
public interface IListener<T>
4+
{
5+
void Handle(T message);
6+
}
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("UnityEventAggregator")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Extend Health, Inc.")]
12+
[assembly: AssemblyProduct("UnityEventAggregator")]
13+
[assembly: AssemblyCopyright("Copyright © Extend Health, Inc. 2014")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("5661a2ca-83bf-4961-8bdc-2e524f43a03a")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{B4CEC7B5-0CA8-4FAE-BFBD-1226710608CE}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>UnityEventAggregator</RootNamespace>
11+
<AssemblyName>UnityEventAggregator</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Xml.Linq" />
36+
<Reference Include="System.Data.DataSetExtensions" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Xml" />
40+
<Reference Include="UnityEngine">
41+
<HintPath>..\..\..\..\..\..\Program Files (x86)\Unity\Editor\Data\Managed\UnityEngine.dll</HintPath>
42+
</Reference>
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="EventAggregator.cs" />
46+
<Compile Include="Extensions.cs" />
47+
<Compile Include="IListener.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
51+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
52+
Other similar extension points exist, see Microsoft.Common.targets.
53+
<Target Name="BeforeBuild">
54+
</Target>
55+
<Target Name="AfterBuild">
56+
</Target>
57+
-->
58+
</Project>

0 commit comments

Comments
 (0)