Skip to content

Commit a129f49

Browse files
authored
Merge pull request #5 from coryleach/dev
GameObjectSetActiveBinding
2 parents b471284 + 3c81f46 commit a129f49

File tree

5 files changed

+249
-3
lines changed

5 files changed

+249
-3
lines changed

Demo/Demo.unitypackage

196 Bytes
Binary file not shown.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ A general purpose ComponentBinding monobehaviour is included to wire any two Uni
2222
#### Using UnityPackageManager (for Unity 2019.3 or later)
2323
Open the package manager window (menu: Window > Package Manager)<br/>
2424
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
25-
https://github.com/coryleach/UnityBindings.git#1.0.4<br/>
25+
https://github.com/coryleach/UnityBindings.git#1.0.5<br/>
2626

2727
#### Using UnityPackageManager (for Unity 2019.1 or later)
2828

2929
Find the manifest.json file in the Packages folder of your project and edit it to look like this:
3030
```js
3131
{
3232
"dependencies": {
33-
"com.gameframe.bindings": "https://github.com/coryleach/UnityBindings.git#1.0.4",
33+
"com.gameframe.bindings": "https://github.com/coryleach/UnityBindings.git#1.0.5",
3434
...
3535
},
3636
}

Runtime/GameObjectSetActiveBinding.cs

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace Gameframe.Bindings
5+
{
6+
/// <summary>
7+
/// Binding component for controlling the active state of a GameObject
8+
/// </summary>
9+
public class GameObjectSetActiveBinding : BindingBehaviour
10+
{
11+
public enum ConversionType
12+
{
13+
None,
14+
EnableWhenNumberNotEqual,
15+
EnableWhenNumberGreaterThan,
16+
EnableWhenNumberLessThan,
17+
EnableWhenObjectNotNull,
18+
EnableWhenStringEquals
19+
}
20+
21+
[SerializeField] private GameObject target;
22+
[SerializeField] private ConversionType conversionType = ConversionType.EnableWhenNumberNotEqual;
23+
[SerializeField] private int numberCompareValue;
24+
[SerializeField] private string stringCompareValue = string.Empty;
25+
[SerializeField] private bool invert;
26+
27+
private readonly GameObjectEnabler _enabler = new GameObjectEnabler();
28+
29+
public GameObject Target
30+
{
31+
get => target;
32+
set => target = value;
33+
}
34+
35+
public ConversionType Conversion
36+
{
37+
get => conversionType;
38+
set => conversionType = value;
39+
}
40+
41+
public int NumberCompareValue
42+
{
43+
get => numberCompareValue;
44+
set => numberCompareValue = value;
45+
}
46+
47+
public string StringCompareValue
48+
{
49+
get => stringCompareValue;
50+
set => stringCompareValue = value;
51+
}
52+
53+
public bool Invert
54+
{
55+
get => invert;
56+
set => invert = value;
57+
}
58+
59+
protected override void SetupBindingTarget(Binding binding)
60+
{
61+
binding.Converter = Converter;
62+
_enabler.Target = target;
63+
binding.SetTarget(_enabler,nameof(GameObjectEnabler.Active), false);
64+
}
65+
66+
private object Converter(object sourceValue)
67+
{
68+
try
69+
{
70+
if (invert)
71+
{
72+
return !ConvertValue(sourceValue);
73+
}
74+
75+
return ConvertValue(sourceValue);
76+
}
77+
catch (Exception exception)
78+
{
79+
Debug.LogError($"GameObjectSetActiveBinding conversion failed with exception: {exception}", this);
80+
enabled = false;
81+
return false;
82+
}
83+
}
84+
85+
private bool ConvertValue(object sourceValue)
86+
{
87+
if (sourceValue == null)
88+
{
89+
return false;
90+
}
91+
92+
switch (conversionType)
93+
{
94+
case ConversionType.EnableWhenObjectNotNull:
95+
return true;
96+
case ConversionType.EnableWhenNumberNotEqual:
97+
return ConvertNotEqual(sourceValue);
98+
case ConversionType.EnableWhenNumberGreaterThan:
99+
return ConvertGreaterThan(sourceValue);
100+
case ConversionType.EnableWhenNumberLessThan:
101+
return ConvertLessThan(sourceValue);
102+
case ConversionType.EnableWhenStringEquals:
103+
return ((string)sourceValue) == stringCompareValue;
104+
case ConversionType.None:
105+
return (bool)sourceValue;
106+
default:
107+
throw new ArgumentOutOfRangeException();
108+
}
109+
}
110+
111+
private bool ConvertNotEqual(object sourceValue)
112+
{
113+
switch (Type.GetTypeCode(sourceValue.GetType()))
114+
{
115+
case TypeCode.Boolean:
116+
return (bool)sourceValue;
117+
case TypeCode.Int16:
118+
return (short)sourceValue != numberCompareValue;
119+
case TypeCode.Int32:
120+
return (int)sourceValue != numberCompareValue;
121+
case TypeCode.Int64:
122+
return (long)sourceValue != numberCompareValue;
123+
case TypeCode.Byte:
124+
return (byte)sourceValue != numberCompareValue;
125+
case TypeCode.Char:
126+
return (char)sourceValue != numberCompareValue;
127+
case TypeCode.Decimal:
128+
return (decimal)sourceValue != numberCompareValue;
129+
case TypeCode.SByte:
130+
return (sbyte)sourceValue != numberCompareValue;
131+
case TypeCode.Single:
132+
return (float)sourceValue != numberCompareValue;
133+
case TypeCode.Double:
134+
return (double)sourceValue != numberCompareValue;
135+
case TypeCode.UInt16:
136+
return (ushort)sourceValue != numberCompareValue;
137+
case TypeCode.UInt32:
138+
return (uint)sourceValue != numberCompareValue;
139+
case TypeCode.UInt64:
140+
return (ulong)sourceValue != (ulong)numberCompareValue;
141+
default:
142+
return false;
143+
}
144+
}
145+
146+
private bool ConvertGreaterThan(object sourceValue)
147+
{
148+
switch (Type.GetTypeCode(sourceValue.GetType()))
149+
{
150+
case TypeCode.Boolean:
151+
return (bool)sourceValue;
152+
case TypeCode.Int16:
153+
return (short)sourceValue > numberCompareValue;
154+
case TypeCode.Int32:
155+
return (int)sourceValue > numberCompareValue;
156+
case TypeCode.Int64:
157+
return (long)sourceValue > numberCompareValue;
158+
case TypeCode.Byte:
159+
return (byte)sourceValue > numberCompareValue;
160+
case TypeCode.Char:
161+
return (char)sourceValue > numberCompareValue;
162+
case TypeCode.Decimal:
163+
return (decimal)sourceValue > numberCompareValue;
164+
case TypeCode.SByte:
165+
return (sbyte)sourceValue > numberCompareValue;
166+
case TypeCode.Single:
167+
return (float)sourceValue > numberCompareValue;
168+
case TypeCode.Double:
169+
return (double)sourceValue > numberCompareValue;
170+
case TypeCode.UInt16:
171+
return (ushort)sourceValue > numberCompareValue;
172+
case TypeCode.UInt32:
173+
return (uint)sourceValue > numberCompareValue;
174+
case TypeCode.UInt64:
175+
return (ulong)sourceValue > (ulong)numberCompareValue;
176+
default:
177+
return false;
178+
}
179+
}
180+
181+
private bool ConvertLessThan(object sourceValue)
182+
{
183+
switch (Type.GetTypeCode(sourceValue.GetType()))
184+
{
185+
case TypeCode.Boolean:
186+
return (bool)sourceValue;
187+
case TypeCode.Int16:
188+
return (short)sourceValue < numberCompareValue;
189+
case TypeCode.Int32:
190+
return (int)sourceValue < numberCompareValue;
191+
case TypeCode.Int64:
192+
return (long)sourceValue < numberCompareValue;
193+
case TypeCode.Byte:
194+
return (byte)sourceValue < numberCompareValue;
195+
case TypeCode.Char:
196+
return (char)sourceValue < numberCompareValue;
197+
case TypeCode.Decimal:
198+
return (decimal)sourceValue < numberCompareValue;
199+
case TypeCode.SByte:
200+
return (sbyte)sourceValue < numberCompareValue;
201+
case TypeCode.Single:
202+
return (float)sourceValue < numberCompareValue;
203+
case TypeCode.Double:
204+
return (double)sourceValue < numberCompareValue;
205+
case TypeCode.UInt16:
206+
return (ushort)sourceValue < numberCompareValue;
207+
case TypeCode.UInt32:
208+
return (uint)sourceValue < numberCompareValue;
209+
case TypeCode.UInt64:
210+
return (ulong)sourceValue < (ulong)numberCompareValue;
211+
default:
212+
return false;
213+
}
214+
}
215+
216+
private class GameObjectEnabler
217+
{
218+
private GameObject target;
219+
220+
public GameObject Target
221+
{
222+
get => target;
223+
set => target = value;
224+
}
225+
226+
public bool Active
227+
{
228+
get => target.activeSelf;
229+
set => target.SetActive(value);
230+
}
231+
}
232+
233+
}
234+
}
235+

Runtime/GameObjectSetActiveBinding.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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.gameframe.bindings",
33
"displayName": "Gameframe.Bindings",
4-
"version": "1.0.4",
4+
"version": "1.0.5",
55
"description": "This is a library of binding components that allow you to quickly wire data sources to target properties via the inspector. \n \nBinding changes propagate via the System.ComponentModel.INotifyPropertyChanged interface but the included binding components will also refresh their target properties in OnEnable. \nA general purpose ComponentBinding monobehaviour is included to wire any two UnityEngine.Objects together as well as a TextBinding for quick and simple binding to text fields.",
66
"keywords": [],
77
"author": {

0 commit comments

Comments
 (0)