Last active
August 29, 2015 14:25
-
-
Save kruncher/57a06d17f24cc29e1182 to your computer and use it in GitHub Desktop.
Designer friendly modular components that can be mixed and matched in an almost "Visual Logic" way thanks to UnityEvent and Zenject. LICENSE: MIT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public static class AudioManagerExtensions { | |
public static void PlayClip(this IAudioManager audioManager, AudioClip clip) { | |
audioManager.PlayClip(clip, 1f); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.Events; | |
public sealed class Damager : MonoBehaviour { | |
#region Inspectable Properties | |
[SerializeField] | |
private int _damageHitpoints = 1; | |
public int DamageHitpoints { | |
get { return _damageHitpoints; } | |
set { _damageHitpoints = value; } | |
} | |
#endregion | |
#region Inspectable Events | |
[Serializable] | |
public sealed class DamagerEventHandler : UnityEvent<Damager, GameObject> { } | |
[SerializeField] | |
private DamagerEventHandler _damagedEvent = new DamagerEventHandler(); | |
public DamagerEventHandler DamagedEvent { | |
get { return _damagedEvent; } | |
} | |
#endregion | |
#region Messages and Event Handlers | |
private void OnCollisionEnter(Collision collision) { | |
var otherHealth = collision.gameObject.GetComponent<Health>(); | |
if (otherHealth != null) | |
otherHealth.Hitpoints -= DamageHitpoints; | |
DamagedEvent.Invoke(this, collision.gameObject); | |
} | |
#endregion | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.Events; | |
public sealed class Health : MonoBehaviour { | |
private int _hitpoints; | |
#region Inspectable Properties | |
[SerializeField] | |
private int _initialHitpoints = 10; | |
public int InitialHitpoints { | |
get { return _initialHitpoints; } | |
set { _initialHitpoints = value; } | |
} | |
#endregion | |
#region Inspectable Events | |
[Serializable] | |
public sealed class HealthEventHandler : UnityEvent<Health> { } | |
[SerializeField] | |
private HealthEventHandler _damagedEvent = new HealthEventHandler(); | |
[SerializeField] | |
private HealthEventHandler _destroyedEvent = new HealthEventHandler(); | |
public HealthEventHandler DamagedEvent { | |
get { return _damagedEvent; } | |
} | |
public HealthEventHandler DestroyedEvent { | |
get { return _destroyedEvent; } | |
} | |
#endregion | |
#region Messages and Event Handlers | |
private void Awake() { | |
_hitpoints = InitialHitpoints; | |
} | |
#endregion | |
public int Hitpoints { | |
get { return _hitpoints; } | |
set { | |
if (value == _hitpoints) | |
return; | |
int oldValue = _hitpoints; | |
_hitpoints = Mathf.Max(0, value); | |
if (_hitpoints < oldValue) { | |
DamagedEvent.Invoke(this); | |
if (_hitpoints == 0) | |
DestroyedEvent.Invoke(this); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public interface IAudioManager { | |
void PlayClip(AudioClip clip, float volumeScale); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) 2015 Lea Hayes | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using Zenject; | |
public class PlayAudioClip : MonoBehaviour { | |
[Inject] | |
private IAudioManager _audioManager; | |
#region Inspectable Properties | |
[SerializeField] | |
private AudioClip _clip; | |
[SerializeField] | |
private float _volumeScale = 1f; | |
public AudioClip Clip { | |
get { return _clip; } | |
set { _clip = value; } | |
} | |
public float VolumeScale { | |
get { return _volumeScale; } | |
set { _volumeScale = value; } | |
} | |
#endregion | |
public void Execute() { | |
if (Clip != null && VolumeScale > 0f) | |
_audioManager.PlayClip(Clip, VolumeScale); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SpawnPrefab : MonoBehaviour { | |
#region Inspectable Properties | |
[SerializeField] | |
private GameObject _prefab; | |
[SerializeField] | |
private bool _inheritPosition; | |
[SerializeField] | |
private bool _inheritRotation; | |
[SerializeField] | |
private bool _inheritScale; | |
public GameObject Prefab { | |
get { return _prefab; } | |
set { _prefab = value; } | |
} | |
public bool InheritPosition { | |
get { return _inheritPosition; } | |
set { _inheritPosition = value; } | |
} | |
public bool InheritRotation { | |
get { return _inheritRotation; } | |
set { _inheritRotation = value; } | |
} | |
public bool InheritScale { | |
get { return _inheritScale; } | |
set { _inheritScale = value; } | |
} | |
#endregion | |
public void Execute() { | |
// TODO: Use pooling instead :) | |
var instance = (GameObject)Instantiate(_prefab); | |
if (InheritPosition) | |
instance.transform.position = transform.position; | |
if (InheritRotation) | |
instance.transform.rotation = transform.rotation; | |
if (InheritScale) | |
instance.transform.localScale = transform.localScale; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment