Skip to content

Commit 028d8be

Browse files
Added support for the [Asset] attribute
1 parent ed3dfa1 commit 028d8be

File tree

7 files changed

+80
-6
lines changed

7 files changed

+80
-6
lines changed

.github/WIKI/lifecycle.png

2.43 KB
Loading

.github/WIKI/lifecycle.sketch

2.79 KB
Binary file not shown.

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# Entity Component System
88

9-
[![npm](https://img.shields.io/badge/upm-3.6.0-232c37.svg?style=for-the-badge)]()
9+
[![npm](https://img.shields.io/badge/upm-3.7.0-232c37.svg?style=for-the-badge)]()
1010
[![npm](https://img.shields.io/github/stars/elraccoone/unity-entity-component-system.svg?style=for-the-badge)]()
1111
[![npm](https://img.shields.io/badge/build-passing-brightgreen.svg?style=for-the-badge)]()
1212

@@ -61,6 +61,7 @@ Use build in file generator to create new instances for any of these types.
6161
```cs
6262
/// Base class for every controller.
6363
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
64+
/// NOTE: This class allows the usage of [Asset] properties.
6465
abstract class Controller {
6566

6667
/// A reference to the controller.
@@ -95,14 +96,16 @@ abstract class Controller {
9596
bool HasService<S> () where S : IService, new();
9697
/// Gets an asset from this controller.
9798
AssetType GetAsset<AssetType> (string name);
99+
/// Gets an asset from this controller.
100+
System.Object GetAsset (string name);
98101
/// Check whether this controller has an asset.
99102
bool HasAsset (string name);
100103
}
101104
```
102105

103106
```cs
104107
/// Base class for every entity component.
105-
/// NOTE: This class allows the usage of [Referenced] and [Protected] objects and primitives.
108+
/// NOTE: This class allows the usage of [Referenced] and [Protected] properties.
106109
abstract class EntityComponent<EntityComponentType, EntitySystemType> : UnityEngine.MonoBehaviour, IEntityComponent
107110
where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new()
108111
where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new() {
@@ -144,6 +147,7 @@ abstract class EntityComponent<EntityComponentType, EntitySystemType> : UnityEng
144147
```cs
145148
/// Base class for every entity system.
146149
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
150+
/// NOTE: This class allows the usage of [Asset] properties.
147151
abstract class EntitySystem<EntitySystemType, EntityComponentType> : IEntitySystem
148152
where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new()
149153
where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new() {
@@ -226,6 +230,7 @@ abstract class EntitySystem<EntitySystemType, EntityComponentType> : IEntitySyst
226230
```cs
227231
/// Base class for every service
228232
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
233+
/// NOTE: This class allows the usage of [Asset] properties.
229234
abstract class Service<ServiceType> : IService
230235
where ServiceType : Service<ServiceType>, new() {
231236

Runtime/Attributes/Asset.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
namespace ElRaccoone.EntityComponentSystem {
2+
3+
/// Describes an asset which should be assigned automatically.
4+
[System.AttributeUsage (System.AttributeTargets.Field)]
5+
public class Asset : System.Attribute {
6+
7+
/// Defines whether the field name should be used for getting the asset from
8+
/// the controller. When set to false, the asset name string will be used
9+
/// instead.
10+
private bool useFieldNameAsAssetName = false;
11+
12+
/// The asset name will be used when defined that the field name should not
13+
/// be used for finding the asset name. Thus using this asset name property
14+
/// instead.
15+
private string assetName = "";
16+
17+
/// Defines the property as an asset, when the controller is done
18+
/// registering, the asset will be fetched from the controller using the
19+
/// name of the property field.
20+
public Asset () {
21+
this.useFieldNameAsAssetName = true;
22+
this.assetName = "";
23+
}
24+
25+
/// Defines the property as an asset, when the controller is done
26+
/// registering, the asset will be fetched from the controller using the
27+
/// given asset name.
28+
public Asset(string assetName) {
29+
this.useFieldNameAsAssetName = false;
30+
this.assetName = assetName;
31+
}
32+
33+
/// Sets the attributes values on an object.
34+
public static void SetAttributeValues (System.Object target) {
35+
var _fields = target.GetType ().GetFields (
36+
System.Reflection.BindingFlags.Instance |
37+
System.Reflection.BindingFlags.NonPublic);
38+
foreach (var _field in _fields) {
39+
var _assetFieldAttribute = System.Attribute.GetCustomAttribute (_field, typeof (Asset)) as Asset;
40+
if (_assetFieldAttribute != null)
41+
_field.SetValue (target, Controller.Instance.GetAsset(
42+
_assetFieldAttribute.useFieldNameAsAssetName == true ? _field.Name : _assetFieldAttribute.assetName));
43+
}
44+
}
45+
}
46+
}

Runtime/Attributes/Asset.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Controller.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ public void Register (params System.Type[] typesOf) {
137137
Injected.SetAttributeValues (this.systems[_systemIndex]);
138138
for (var _serviceIndex = 0; _serviceIndex < this.services.Count; _serviceIndex++)
139139
Injected.SetAttributeValues (this.services[_serviceIndex]);
140+
141+
// Set Values of the 'Asset' attributes
142+
Asset.SetAttributeValues (this);
143+
for (var _systemIndex = 0; _systemIndex < this.systems.Count; _systemIndex++)
144+
Asset.SetAttributeValues (this.systems[_systemIndex]);
145+
for (var _serviceIndex = 0; _serviceIndex < this.services.Count; _serviceIndex++)
146+
Asset.SetAttributeValues (this.services[_serviceIndex]);
140147
}
141148

142149
/// Enables or disabled a system, enabling the systems allows them to invoke
@@ -223,11 +230,16 @@ public System.Object GetService (System.Type typeOf) {
223230
}
224231

225232
/// Gets an asset from this controller.
226-
public AssetType GetAsset<AssetType> (string name) where AssetType : UnityEngine.Object {
233+
public UnityEngine.Object GetAsset (string name) {
227234
for (var _i = 0; _i < this.assets.Length; _i++)
228235
if (this.assets[_i].name == name)
229-
return this.assets[_i] as AssetType;
230-
throw new System.Exception ("Unable to get asset, it was not added to the controller");
236+
return this.assets[_i];
237+
throw new System.Exception ($"Unable to get asset '{name}', it was not found on the controller");
238+
}
239+
240+
/// Gets an asset from this controller.
241+
public AssetType GetAsset<AssetType> (string name) where AssetType : UnityEngine.Object {
242+
return this.GetAsset(name) as AssetType;
231243
}
232244

233245
/// Check whether this controller has an asset.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nl.elraccoone.entity-component-system",
33
"displayName": "Entity Component System",
4-
"version": "3.6.0",
4+
"version": "3.7.0",
55
"unity": "2019.1",
66
"description": "A better approach to game design that allows you to concentrate on the actual problems you are solving: the data and behavior that make up your game. By moving from object-oriented to data-oriented design it will be easier for you to reuse the code and easier for others to understand and work on it."
77
}

0 commit comments

Comments
 (0)