|
1 | 1 | namespace ElRaccoone.EntityComponentSystem { |
2 | | - |
| 2 | + /// <summary> |
3 | 3 | /// Describes an asset which should be assigned automatically. |
| 4 | + /// </summary> |
4 | 5 | [System.AttributeUsage (System.AttributeTargets.Field)] |
5 | 6 | public class Asset : System.Attribute { |
6 | | - |
| 7 | + /// <summary> |
7 | 8 | /// Defines whether the field name should be used for getting the asset from |
8 | 9 | /// the controller. When set to false, the asset name string will be used |
9 | 10 | /// instead. |
10 | | - private bool useFieldNameAsAssetName = false; |
| 11 | + /// </summary> |
| 12 | + bool useFieldNameAsAssetName = false; |
11 | 13 |
|
| 14 | + /// <summary> |
12 | 15 | /// The asset name will be used when defined that the field name should not |
13 | 16 | /// be used for finding the asset name. Thus using this asset name property |
14 | 17 | /// instead. |
15 | | - private string assetName = ""; |
| 18 | + /// </summary> |
| 19 | + string assetName = ""; |
16 | 20 |
|
| 21 | + /// <summary> |
17 | 22 | /// Defines the property as an asset, when the controller is done |
18 | 23 | /// registering, the asset will be fetched from the controller using the |
19 | 24 | /// name of the property field. |
| 25 | + /// </summary> |
20 | 26 | public Asset () { |
21 | | - this.useFieldNameAsAssetName = true; |
22 | | - this.assetName = ""; |
| 27 | + useFieldNameAsAssetName = true; |
| 28 | + assetName = ""; |
23 | 29 | } |
24 | 30 |
|
| 31 | + /// <summary> |
25 | 32 | /// Defines the property as an asset, when the controller is done |
26 | 33 | /// registering, the asset will be fetched from the controller using the |
27 | 34 | /// given asset name. |
28 | | - public Asset(string assetName) { |
29 | | - this.useFieldNameAsAssetName = false; |
| 35 | + /// </summary> |
| 36 | + /// <param name="assetName">The asset name.</param> |
| 37 | + public Asset (string assetName) { |
| 38 | + useFieldNameAsAssetName = false; |
30 | 39 | this.assetName = assetName; |
31 | 40 | } |
32 | 41 |
|
| 42 | + /// <summary> |
33 | 43 | /// Sets the attributes values on an object. |
| 44 | + /// </summary> |
| 45 | + /// <param name="target">The target object.</param> |
34 | 46 | 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)); |
| 47 | + var targetType = target.GetType (); |
| 48 | + var fields = targetType.GetFields (System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); |
| 49 | + foreach (var field in fields) { |
| 50 | + var assetFieldAttribute = System.Attribute.GetCustomAttribute (field, typeof (Asset)) as Asset; |
| 51 | + if (assetFieldAttribute == null) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + var assetName = assetFieldAttribute.useFieldNameAsAssetName ? field.Name : assetFieldAttribute.assetName; |
| 55 | + var asset = Controller.Instance.GetAsset (assetName); |
| 56 | + field.SetValue (target, asset); |
43 | 57 | } |
44 | 58 | } |
45 | 59 | } |
|
0 commit comments