From 9a4f478eccd7e944c0aeab4ff54b30c283b00d02 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Mon, 14 Jul 2014 11:22:00 -0700 Subject: [PATCH 01/21] pull in new extension methods --- .../Extensions/TransformExtensions.cs | 27 +++++++---- .../Extensions/Vector2Extensions.cs | 18 ------- .../Extensions/Vector3Extensions.cs | 48 +++++++++++-------- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/Project/Assets/Plugins/ChicoPlugins/Extensions/TransformExtensions.cs b/Project/Assets/Plugins/ChicoPlugins/Extensions/TransformExtensions.cs index 9bc8fbc..e85dab7 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Extensions/TransformExtensions.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Extensions/TransformExtensions.cs @@ -9,6 +9,12 @@ public static class TransformExtensions { /// /// Gets all direct children. /// + /// + /// The children. + /// + /// + /// Root. + /// public static Transform[] GetChildren(this Transform root) { Transform[] children = new Transform[root.childCount]; int i=0; @@ -21,6 +27,9 @@ public static Transform[] GetChildren(this Transform root) { /// /// Attaches a list of transforms to this one. /// + /// + /// Parent transform. + /// /// /// Array, list, or other collection of transforms to add. /// @@ -30,9 +39,6 @@ public static void AttachChildren(this Transform root, IEnumerable ch } } - /// - /// Attach one transform to this one. - /// public static void AttachChild(this Transform root, Transform child) { child.parent = root; } @@ -40,6 +46,9 @@ public static void AttachChild(this Transform root, Transform child) { /// /// Clears the position of this transform without affecting its children. /// + /// + /// Root. + /// public static void ClearLocalPosition(this Transform root) { var children = root.GetChildren(); root.DetachChildren(); @@ -50,6 +59,9 @@ public static void ClearLocalPosition(this Transform root) { /// /// Clears rotation of this transform without affecting its children. /// + /// + /// Root. + /// public static void ClearLocalRotation(this Transform root) { var children = root.GetChildren(); root.DetachChildren(); @@ -60,6 +72,9 @@ public static void ClearLocalRotation(this Transform root) { /// /// Clears scale of this transform without affecting children. /// + /// + /// Root. + /// public static void ClearLocalScale(this Transform root) { var children = root.GetChildren(); root.DetachChildren(); @@ -67,16 +82,10 @@ public static void ClearLocalScale(this Transform root) { root.AttachChildren(children); } - /// - /// Multiplies this transform's localScale. - /// public static void MultiplyScale(this Transform root, float multiplier) { root.transform.localScale = root.transform.localScale * multiplier; } - /// - /// Multiplies this transform's localScale component-wise with another Vector3. - /// public static void MultiplyScale(this Transform root, Vector3 multiplier) { root.transform.localScale = root.transform.localScale.WithScale(multiplier); } diff --git a/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector2Extensions.cs b/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector2Extensions.cs index 175d07e..42d5ef2 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector2Extensions.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector2Extensions.cs @@ -5,40 +5,22 @@ /// Extension methods for Unity's Vector2 class. /// public static class Vector2Extensions { - /// - /// Creates a Vector3 with matching (x,y), requested (z). - /// public static Vector3 ToVector3(this Vector2 v, float z) { return new Vector3(v.x, v.y, z); } - /// - /// Copies this vector, overrides the copy's X. - /// public static Vector3 WithX(this Vector2 v, float x) { return new Vector2(x, v.y); } - /// - /// Copies this vector, overrides the copy's Y. - /// public static Vector3 WithY(this Vector2 v, float y) { return new Vector2(v.x, y); } - /// - /// Copies this vector, overrides the copy's length. - /// public static Vector2 WithLength(this Vector2 v, float length) { return v.normalized * length; } - /// - /// Copies this vector, multiplies the copy component-wise with another vector. - /// - /// - /// There is no float/int override because that's just scalar multiplication. ;) - /// public static Vector2 WithScale(this Vector2 v, Vector2 scale) { return Vector2.Scale(v, scale); } diff --git a/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector3Extensions.cs b/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector3Extensions.cs index a7c32d9..feb88cd 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector3Extensions.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Extensions/Vector3Extensions.cs @@ -5,47 +5,53 @@ /// Extension methods for Unity's Vector3 class. /// public static class Vector3Extensions { - /// - /// Copies this vector, overrides the copy's X. - /// public static Vector3 WithX(this Vector3 v, float x) { return new Vector3(x, v.y, v.z); } - /// - /// Copies this vector, overrides the copy's Y. - /// public static Vector3 WithY(this Vector3 v, float y) { return new Vector3(v.x, y, v.z); } - /// - /// Copies this vector, overrides the copy's Z. - /// public static Vector3 WithZ(this Vector3 v, float z) { return new Vector3(v.x, v.y, z); } - /// - /// Copies this vector, multiplies the copy component-wise with another vector. - /// - /// - /// There is no float/int override because that's just scalar multiplication. ;) - /// public static Vector3 WithScale(this Vector3 v, Vector3 other) { return Vector3.Scale(v, other); } + + public static Vector3 WithScaleX(this Vector3 v, float scale) { + return new Vector3(v.x * scale, v.y, v.z); + } + + public static Vector3 WithScaleY(this Vector3 v, float scale) { + return new Vector3(v.x, v.y * scale, v.z); + } + + public static Vector3 WithScaleZ(this Vector3 v, float scale) { + return new Vector3(v.x, v.y, v.z * scale); + } + + public static Vector3 WithClampX(this Vector3 v, float min, float max) { + float clamped = Mathf.Clamp(v.x, min, max); + return new Vector3(clamped, v.y, v.z); + } + + public static Vector3 WithClampY(this Vector3 v, float min, float max) { + float clamped = Mathf.Clamp(v.y, min, max); + return new Vector3(v.x, clamped, v.z); + } + + public static Vector3 WithClampZ(this Vector3 v, float min, float max) { + float clamped = Mathf.Clamp(v.z, min, max); + return new Vector3(v.x, v.y, clamped); + } - /// - /// Copies this vector, overrides the copy's length. - /// public static Vector3 WithLength(this Vector3 v, float length) { return v.normalized * length; } - /// - /// Returns Vector2 with matching (x,y). - /// public static Vector2 ToVector2(this Vector3 v) { return new Vector2(v.x, v.y); } From 7b4aa90aa7226f489d6dc256a47b810360e60bf4 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Mon, 14 Jul 2014 11:23:38 -0700 Subject: [PATCH 02/21] new AudioPool --- .../Plugins/ChicoPlugins/Pools/AudioPool.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs new file mode 100644 index 0000000..5a6c5b4 --- /dev/null +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs @@ -0,0 +1,44 @@ +using UnityEngine; +using System.Collections.Generic; + +[AddComponentMenu("ChicoPlugins/Pools/Audio Source")] +public class AudioPool : ComponentPool { + [SerializeField] float _reclaimRate = 0.5f; + public float reclaimRate { + get { return _reclaimRate; } + } + + float timeToReclaim; + + List watchlist = new List(); + + public void Reclaim() { + for (int i=watchlist.Count-1; i >= 0; i--) { + AudioSource item = watchlist[i]; + if (!item.isPlaying) { + watchlist.RemoveAt(i); + Add(item); + } + } + } + + protected override void OnGetNext(AudioSource item) { + watchlist.Add(item); + if (activateOnGet) { + item.Play(); + } + enabled = true; + } + + protected override void OnAwake() { + timeToReclaim = reclaimRate; + } + + protected override void OnUpdate() { + timeToReclaim -= Time.time; + if (timeToReclaim < 0f) { + timeToReclaim = _reclaimRate; + Reclaim(); + } + } +} From ff715634b7d18fa34d9289faf578fb8bb4cbbba0 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Mon, 14 Jul 2014 11:26:08 -0700 Subject: [PATCH 03/21] pull in TweenCam changes - LerpState replaced by InterpParam and InterpState - AddComponentMenu entries --- .../Plugins/ChicoPlugins/TweenCam/ChaseCam.cs | 1 + .../Plugins/ChicoPlugins/TweenCam/TweenCam.cs | 30 +++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Project/Assets/Plugins/ChicoPlugins/TweenCam/ChaseCam.cs b/Project/Assets/Plugins/ChicoPlugins/TweenCam/ChaseCam.cs index 9e324bf..dd2c254 100644 --- a/Project/Assets/Plugins/ChicoPlugins/TweenCam/ChaseCam.cs +++ b/Project/Assets/Plugins/ChicoPlugins/TweenCam/ChaseCam.cs @@ -4,6 +4,7 @@ /// TweenCam that follows a target Transform. /// [RequireComponent(typeof(Camera))] +[AddComponentMenu("ChicoPlugins/TweenCam/ChaseCam")] public class ChaseCam : TweenCam { /// /// Which Transform do we chase? diff --git a/Project/Assets/Plugins/ChicoPlugins/TweenCam/TweenCam.cs b/Project/Assets/Plugins/ChicoPlugins/TweenCam/TweenCam.cs index 1140d06..f45872b 100644 --- a/Project/Assets/Plugins/ChicoPlugins/TweenCam/TweenCam.cs +++ b/Project/Assets/Plugins/ChicoPlugins/TweenCam/TweenCam.cs @@ -70,6 +70,7 @@ /// /// [RequireComponent(typeof(Camera))] +[AddComponentMenu("ChicoPlugins/TweenCam/TweenCam")] public class TweenCam : MonoBehaviour { /// /// Timer for mode transitions. @@ -130,7 +131,7 @@ public CamState(Vector3 pos, Vector3 target, float fov, Vector3 up) { /// Cached reference to Camera component. /// private Camera cam; - + protected void Awake() { cam = GetComponent(); OnAwake(); @@ -153,7 +154,8 @@ protected void Update() { CamState b = state; a = lastStateFunc(a); b = stateFunc(b); - state = LerpState(a, b, tween.perc); + float param = InterpParam(tween.perc); + state = InterpState(a, b, param); } else { state = stateFunc(state); } @@ -162,7 +164,29 @@ protected void Update() { lastState = state; } - private CamState LerpState(CamState a, CamState b, float t) { + /// + /// Optional override to modify parameter to InterpState; otherwise the tween control is linear. Called once per frame during tweens. + /// + protected virtual float InterpParam(float param) { + return param; + } + + /// + /// Optional override to control interpolation between two cam states; otherwise the tween is a lerp. Called once per frame during tweens. + /// + /// + /// State to assign for this frame. + /// + /// + /// State before tweening. + /// + /// + /// State after tweening. + /// + /// + /// Tween param, generated by InterpParam. + /// + protected virtual CamState InterpState(CamState a, CamState b, float t) { a.pos = Vector3.Lerp(a.pos, b.pos, t); a.target = Vector3.Lerp(a.target, b.target, t); a.fov = Mathf.Lerp(a.fov, b.fov, t); From 9d1d1b26b039283dcebd348a565320d1da7583b4 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Mon, 14 Jul 2014 11:26:41 -0700 Subject: [PATCH 04/21] pull in Music changes - Fix volume bug - AddComponentMenu entries --- Project/Assets/Plugins/ChicoPlugins/Audio/MusicLooper.cs | 1 + Project/Assets/Plugins/ChicoPlugins/Audio/MusicPlayer.cs | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Project/Assets/Plugins/ChicoPlugins/Audio/MusicLooper.cs b/Project/Assets/Plugins/ChicoPlugins/Audio/MusicLooper.cs index 7b51533..28090bf 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Audio/MusicLooper.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Audio/MusicLooper.cs @@ -7,6 +7,7 @@ /// /// Still calls onMusicFinished() after re-starting the music. /// +[AddComponentMenu("ChicoPlugins/Music/Looper")] public class MusicLooper : MusicPlayer { protected override void MusicFinished() { music.Play(); diff --git a/Project/Assets/Plugins/ChicoPlugins/Audio/MusicPlayer.cs b/Project/Assets/Plugins/ChicoPlugins/Audio/MusicPlayer.cs index 15d3eac..a635cb1 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Audio/MusicPlayer.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Audio/MusicPlayer.cs @@ -1,6 +1,7 @@ using UnityEngine; using System.Collections; +[AddComponentMenu("ChicoPlugins/Music/Player")] public class MusicPlayer : MonoBehaviour { public AudioSource music; @@ -28,7 +29,8 @@ protected void Awake() { return; } } - + + baseVolume = music.volume; ConfigureSource(music); float configMulti = GetConfigVolumeMulti(); @@ -53,8 +55,7 @@ protected void Update() { /// Desired music volume; will be modified according to baseVolume. /// public void SetVolumeMulti(float volume) { - volume /= baseVolume; - ApplyVolumeMulti(volume); + ApplyVolumeMulti(volume * baseVolume); } /// @@ -95,6 +96,8 @@ protected virtual void OnUpdate() {} /// Configure an AudioSource for use with music. Maybe also useful for UI? /// public static void ConfigureSource(AudioSource source) { + if (source == null) return; + source.ignoreListenerPause = true; source.ignoreListenerVolume = true; source.bypassEffects = true; From 85510b62277c77ef3bda3d815322676fdae16559 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Mon, 14 Jul 2014 11:29:08 -0700 Subject: [PATCH 05/21] pull in CUI changes - AddComponentMenu entries - Fix mouse position per platform - CUIText properties are now readable - Fix CUIText init race condition --- .../ChicoPlugins/UI/CUIClickDelegate.cs | 1 + .../Plugins/ChicoPlugins/UI/CUIController.cs | 33 ++++--- .../Plugins/ChicoPlugins/UI/CUIFloatRange.cs | 1 + .../Plugins/ChicoPlugins/UI/CUIGotoPanel.cs | 1 + .../Plugins/ChicoPlugins/UI/CUIIntRange.cs | 1 + .../Plugins/ChicoPlugins/UI/CUIPanel.cs | 1 + .../Plugins/ChicoPlugins/UI/CUIPanelCam.cs | 1 + .../Plugins/ChicoPlugins/UI/CUIPopPanel.cs | 1 + .../Plugins/ChicoPlugins/UI/CUIPushPanel.cs | 1 + .../Plugins/ChicoPlugins/UI/CUISendMessage.cs | 1 + .../Assets/Plugins/ChicoPlugins/UI/CUIText.cs | 22 +++++ .../Plugins/ChicoPlugins/UI/CUITextButton.cs | 57 ++---------- .../Plugins/ChicoPlugins/UI/CUITextMesh.cs | 13 ++- .../Plugins/ChicoPlugins/UI/CUITextWidget.cs | 88 +++++++++++++++++++ .../Plugins/ChicoPlugins/UI/CUIToggle.cs | 1 + 15 files changed, 159 insertions(+), 64 deletions(-) create mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs index 0cd6f1f..4b937ec 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs @@ -4,6 +4,7 @@ /// /// Pass clicks to other objects via delegates. /// +[AddComponentMenu("ChicoPlugins/UI/Clickable/Delegate")] public class CUIClickDelegate : CUIClickable { public System.Action OnMouseDown; public System.Action OnMouseUp; diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs index fa4c229..f0c7073 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs @@ -76,6 +76,7 @@ /// /// [RequireComponent(typeof(Camera))] +[AddComponentMenu("ChicoPlugins/UI/Controller")] public class CUIController : MonoBehaviour { /// /// Cached camera reference (read-only). @@ -167,19 +168,24 @@ protected void Start() { protected void Update() { UpdateFrameValues(); OnUpdate(); - CacheLastFrameValues(); } + + protected void LateUpdate() { + CacheLastFrameValues(); + } /// /// Override for custom click detection. /// /// True if clicking during this frame. protected virtual bool GetMouseDown() { - #if UNITY_STANDALONE || UNITY_EDITOR - return Input.GetMouseButton(0); - #else - return Input.touchCount > 0; - #endif + #if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR + return Input.touchCount > 0; + + #else + return Input.GetMouseButton(0); + + #endif } /// @@ -189,13 +195,11 @@ protected virtual bool GetMouseDown() { protected virtual Vector3 GetMousePosition() { Vector3 mp = Input.mousePosition.WithZ(0.1f); - #if !UNITY_STANDALONE - #if UNITY_EDITOR - if (!Input.GetMouseButton(0)) { mp = Vector3.zero; } - #else - if (Input.touchCount < 1) { mp = Vector3.zero; } - #endif - #endif + #if UNITY_ANDROID || UNITY_IPHONE + if (!mouseDown) { + mp = Vector3.zero; + } + #endif return mp; } @@ -215,7 +219,7 @@ protected virtual Ray GetMouseRay() { protected virtual GameObject GetMouseTarget() { GameObject target = null; RaycastHit hit; - if (Physics.Raycast(mouseRay, out hit, 100000f, uiLayerMask)) { + if (mousePos != Vector3.zero && Physics.Raycast(mouseRay, out hit, 100000f, uiLayerMask)) { target = hit.collider.gameObject; } return target; @@ -328,6 +332,7 @@ protected virtual void OnMouseHold() { /// Alias for SendMessage. /// protected void Send(GameObject target, string message) { + //Debug.Log(string.Format("{0} ({1})", message, target.name), target); target.SendMessage(message, SendMessageOptions.DontRequireReceiver); } diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs index a6755b5..e9c34ff 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs @@ -1,6 +1,7 @@ using UnityEngine; using System.Collections; +[AddComponentMenu("ChicoPlugins/UI/Range/Float")] public class CUIFloatRange : CUIRange { protected override float ClampValue(float newValue, float min, float max) { return Mathf.Clamp(newValue, min, max); diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs index 7c755ea..7557aca 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs @@ -4,6 +4,7 @@ /// /// Button. When clicked, tells CUIPanelCam to switch panels WITHOUT a push. /// +[AddComponentMenu("ChicoPlugins/UI/Clickable/Goto Panel")] public class CUIGotoPanel : CUIClickable { public string panelName; diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs index 1c9112c..6b32a7f 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs @@ -4,6 +4,7 @@ /// /// Two buttons allowing user to pick an int. /// +[AddComponentMenu("ChicoPlugins/UI/Range/Int")] public class CUIIntRange : CUIRange { protected override int ClampValue(int newValue, int min, int max) { return Mathf.Clamp(newValue, min, max); diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs index 93bf0aa..7e3b204 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs @@ -4,6 +4,7 @@ /// /// For use with CUIPanelCam. /// +[AddComponentMenu("ChicoPlugins/UI/Panel")] public class CUIPanel : MonoBehaviour { /// /// Unique name for this panel. Leave blank to use GameObject's name. diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs index f4c9cc0..80a9a47 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs @@ -39,6 +39,7 @@ /// /// [RequireComponent(typeof(Camera))] +[AddComponentMenu("ChicoPlugins/UI/PanelCam")] public class CUIPanelCam : ChaseCam { Dictionary panels = new Dictionary(); diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs index 867990d..f7338f0 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs @@ -4,6 +4,7 @@ /// /// Button. Pairs with CUIPopPanel. When clicked, tells CUIPanelCam to go back one panel. /// +[AddComponentMenu("ChicoPlugins/UI/Clickable/Pop Panel")] public class CUIPopPanel : CUIClickable { public override void MouseRelease() { diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs index 46524f5..7de67df 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs @@ -4,6 +4,7 @@ /// /// Button. Pairs with CUIPopPanel. When clicked, tells CUIPanelCam to go forward one panel. /// +[AddComponentMenu("ChicoPlugins/UI/Clickable/Push Panel")] public class CUIPushPanel : CUIGotoPanel { protected override void GotoPanel(CUIPanelCam cam, string target) { cam.PushPanel(target); diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs index 6426886..8c7432b 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs @@ -4,6 +4,7 @@ /// /// Pass clicks to other GameObjects via SendMessage. /// +[AddComponentMenu("ChicoPlugins/UI/Clickable/SendMessage")] public class CUISendMessage : CUIClickable { public GameObject target; diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs index fec9dfe..919944e 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs @@ -7,12 +7,16 @@ public abstract class CUIText : MonoBehaviour { string lastMessage; Color lastColor; + + protected bool initialized { get; private set; } public string message { get { + Init(); return lastMessage; } set { + Init(); if (value != lastMessage) { lastMessage = value; ApplyMessage(value); @@ -22,16 +26,34 @@ public string message { public Color color { get { + Init(); return lastColor; } set { + Init(); if (value != lastColor) { lastColor = value; ApplyColor(value); } } } + + protected void Init() { + if (initialized) return; + + OnInit(); + + lastMessage = GetMessage(); + lastColor = GetColor(); + + initialized = true; + } + + protected virtual void OnInit() {} protected abstract void ApplyMessage(string newMessage); protected abstract void ApplyColor(Color newColor); + + protected abstract string GetMessage(); + protected abstract Color GetColor(); } diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs index dd0a39b..7f2676f 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs @@ -1,65 +1,24 @@ using UnityEngine; using System.Collections; -public class CUITextButton : CUIClickable { - CUIText text; - - public struct ButtonState { - public Color color; - public Vector3 size; - - public ButtonState(Color color, Vector3 size) { - this.color = color; - this.size = size; - } - } - - TweenState tween; - ButtonState state; - ButtonState lastState; - - ButtonState mainState; - ButtonState pressState; - - public Color mainColor = Color.black; +[AddComponentMenu("ChicoPlugins/UI/Text/TextButton")] +public class CUITextButton : CUITextWidget { public Color pressColor = Color.red; - public float pressScale = 1.1f; - - protected void Start() { - text = GetComponentInChildren(); - if (text == null) { - Debug.LogWarning("CUITextButton without TextMesh!", this); - return; - } - - tween = new TweenState(0f); - mainState = new ButtonState(mainColor, transform.localScale); + + protected ButtonState pressState; + + protected override void OnStart() { pressState = new ButtonState(pressColor, transform.localScale * pressScale); - - lastState = state = mainState; } public override void MouseIn() { - Debug.Log("MouseIn"); +// Debug.Log("MouseIn"); SwitchState(pressState, 0.1f); } public override void MouseOut() { - Debug.Log("MouseOut"); +// Debug.Log("MouseOut"); SwitchState(mainState, 0.2f); } - - protected void Update() { - if (tween.Tick(Time.deltaTime)) { - text.color = Color.Lerp(lastState.color, state.color, tween.perc); - transform.localScale = Vector3.Lerp(lastState.size, state.size, tween.perc); - } - } - - void SwitchState(ButtonState newState, float duration) { - lastState = new ButtonState(text.color, transform.localScale); - state = newState; - tween.Reset(duration); - } } diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs index e6c0207..9b7132a 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs @@ -2,10 +2,13 @@ using System.Collections; [RequireComponent(typeof(TextMesh))] +[AddComponentMenu("ChicoPlugins/UI/Text/TextMesh")] public class CUITextMesh : CUIText { protected TextMesh tm; - protected void Start() { + protected override void OnInit() { + if (initialized) return; + tm = GetComponent(); } @@ -16,4 +19,12 @@ protected override void ApplyMessage(string newMessage) { protected override void ApplyColor(Color newColor) { tm.color = newColor; } + + protected override string GetMessage() { + return tm.text; + } + + protected override Color GetColor() { + return tm.color; + } } diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs new file mode 100644 index 0000000..64153bb --- /dev/null +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs @@ -0,0 +1,88 @@ +using UnityEngine; +using System.Collections; + +/// +/// Widget to manage a child CUIText. +/// +[AddComponentMenu("ChicoPlugins/UI/Clickable/TextWidget")] +public class CUITextWidget : CUIClickable { + protected CUIText text { get; private set; } + + public string message { + get { return text.message; } + set { text.message = value; } + } + + public Color color { + get { return text.color; } + set { text.color = value; } + } + + public struct ButtonState { + public Color color; + public Vector3 size; + + public ButtonState(Color color, Vector3 size) { + this.color = color; + this.size = size; + } + } + + TweenState tween; + ButtonState state; + ButtonState lastState; + + protected ButtonState mainState; + + protected void Awake() { + text = GetComponentInChildren(); + if (text == null) { + Debug.LogWarning("CUITextButton without TextMesh!", this); + return; + } + + tween = new TweenState(0f); + mainState = new ButtonState(text.color, transform.localScale); + lastState = state = mainState; + + OnStart(); + } + + + protected virtual void Update() { + if (tween.Tick(Time.deltaTime)) { + text.color = Color.Lerp(lastState.color, state.color, tween.perc); + transform.localScale = Vector3.Lerp(lastState.size, state.size, tween.perc); + } + } + + public void PulseScale(float scale, float duration) { + Pulse(mainState.color, mainState.size * scale, duration); + } + + public void PulseColor(Color color, float duration) { + Pulse(color, mainState.size, duration); + } + + public void Pulse(Color color, Vector3 scale, float duration) { + ButtonState newState = new ButtonState(color, scale); + SnapState(newState); + SwitchState(mainState, duration); + } + + protected void SwitchState(ButtonState newState, float duration) { + lastState = new ButtonState(text.color, transform.localScale); + state = newState; + tween.Reset(duration); + } + + protected void SnapState(ButtonState newState) { + lastState = state; + state = newState; + tween.Stop(); + text.color = newState.color; + transform.localScale = newState.size; + } + + protected virtual void OnStart() {} +} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs index 2b12be8..814e66f 100644 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs +++ b/Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs @@ -4,6 +4,7 @@ /// /// Button allows user to toggle a bool. /// +[AddComponentMenu("ChicoPlugins/UI/Toggle")] public class CUIToggle : CUIValue { public GameObject enabledWidget; public GameObject disabledWidget; From bc3a051dfbcd3cd9b91218ac1bc4230a344b2831 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Mon, 14 Jul 2014 11:38:44 -0700 Subject: [PATCH 06/21] pull in ComponentPool changes - Static TryGetNext access (my favorite feature!) - AddComponentMenu entries - ComponentPoolBase simpler, public fields --- .../ChicoPlugins/Pools/ComponentPool.cs | 46 +++++++++++ .../ChicoPlugins/Pools/ComponentPoolBase.cs | 80 ++++--------------- .../ChicoPlugins/Pools/ParticlePool.cs | 1 + .../ChicoPlugins/Pools/TransformPool.cs | 1 + 4 files changed, 64 insertions(+), 64 deletions(-) diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs index 89809c0..6f394b6 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs @@ -247,4 +247,50 @@ protected virtual void OnAdd(T item) {} /// Note this will NOT be called if the GetNext call is returning null. /// protected virtual void OnGetNext(T item) {} + + /// + /// Static equivalent of GetNext. + /// + /// + /// True if you got an item; false otherwise (no such pool, pool is empty, etc.) + /// + public static bool TryGetNext(string poolName, out T item) { + ComponentPool pool = PoolManager.Get(poolName) as ComponentPool; + if (pool != null) { + item = pool.GetNext(); + } else { + item = null; + } + return item != null; + } + + /// + /// Static equivalent of GetNextAt. + /// + /// + /// True if you got an item; false otherwise (no such pool, pool is empty, etc.) + /// + public static bool TryGetNextAt(string poolName, Vector3 pos, out T item) { + ComponentPool pool = PoolManager.Get(poolName) as ComponentPool; + if (pool != null) { + item = pool.GetNextAt(pos); + } else { + item = null; + } + return item != null; + } + + /// + /// Static equivalent of GetNext. + /// + /// + /// True if the add succeeded; false otherwise (no such pool, pool is wrong type, etc.) + /// + public static bool TryAdd(string poolName, T item) { + ComponentPool pool = PoolManager.Get(poolName) as ComponentPool; + if (pool != null) { + pool.Add(item); + } + return pool != null; + } } diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs index 6890d50..52cf6b2 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs @@ -8,70 +8,22 @@ /// Mainly holds inspector values. /// public abstract class ComponentPoolBase : MonoBehaviour { - [SerializeField] string _id; - [SerializeField] bool _copyOnEmpty = true; - [SerializeField] int _copyOnStart = 0; - [SerializeField] int _copyRate = 5; - [SerializeField] bool _activateOnGet = true; - - /// - /// Unique identifier for the pool. (inspector) - /// - /// - /// Defaults to the name of the attached GameObject. - /// - public string id { - get { return _id; } - } - - /// - /// When the pool is exhausted, should it Instantiate() new members? (inspector) - /// - /// - /// If copyOnEmpty is true, the pool will store its first member "on ice" as - /// a master copy for extra members. - /// - public bool copyOnEmpty { - get { return _copyOnEmpty; } - } - - /// - /// Should the pool Instantiate() new members when gameplay starts? (inspector) - /// - /// - /// Useful to keep your scenes simple and uncluttered. - /// - public int copyOnStart { - get { return _copyOnStart; } - } + [SerializeField] string _id; + + public bool copyOnEmpty = true; + public int copyOnStart = 0; + public int copyRate = 5; + public bool activateOnGet = true; - /// - /// Max Instantiate() calls per frame? (inspector) - /// - /// - /// Referenced only if copyOnStart or copyOnEmpty are true. - /// - /// Note this value will appear to be ignored in one circumstance: if you - /// ask for more than copyRate items in a single frame and copyOnEmpty is - /// true, the pool will continue to clone one new member for each additional - /// request. - /// - public int copyRate { - get { return _copyRate; } - } + public string id { + get { return _id; } + } - /// - /// Should we SetActive(true) any pool members right before giving them to you? - /// - /// - /// Pool members will always be disabled as they are added to the pool; this - /// flag determines whether they are enabled as they leave the pool. - /// - public bool activateOnGet { - get { return _activateOnGet; } - } - - protected void Reset() { - _id = name; - } + protected void Reset() { + _id = name; + copyOnEmpty = true; + copyOnStart = 0; + copyRate = 5; + activateOnGet = true; + } } diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/ParticlePool.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/ParticlePool.cs index c406b33..b79148f 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/ParticlePool.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/ParticlePool.cs @@ -14,6 +14,7 @@ /// /// In the current build, this pool does not handle nested emitters. /// +[AddComponentMenu("ChicoPlugins/Pools/Particle System")] public class ParticlePool : ComponentPool { [SerializeField] float _reclaimRate = 0.5f; diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/TransformPool.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/TransformPool.cs index 7fb8518..eaaf778 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/TransformPool.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/TransformPool.cs @@ -8,5 +8,6 @@ /// Useful for storing any type of GameObject, including prefab-like objects or /// components for which you'd rather not create a custom ComponentPool. /// +[AddComponentMenu("ChicoPlugins/Pools/Transform")] public class TransformPool : ComponentPool { } From ecfadba78fec7c95c6b3611965854c2a2c625481 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Wed, 15 Oct 2014 16:45:33 -0700 Subject: [PATCH 07/21] ComponentPool names read from scene hierarchy. --- CHANGELOG.md | 5 +++++ .../Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs | 4 +--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 462d9a9..866f3c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Changelog for ChicoPlugins ==== +v0.4 (Not Released) +---- + +* Changed: ComponentPool names are now read directly from scene hierarchy. + v0.3 (October 15, 2014) ---- diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs index 52cf6b2..5818590 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs @@ -8,15 +8,13 @@ /// Mainly holds inspector values. /// public abstract class ComponentPoolBase : MonoBehaviour { - [SerializeField] string _id; - public bool copyOnEmpty = true; public int copyOnStart = 0; public int copyRate = 5; public bool activateOnGet = true; public string id { - get { return _id; } + get { return gameObject.name; } } protected void Reset() { From 0777b3c530e2e63b5ccf12009dcd090028853afb Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Wed, 15 Oct 2014 17:11:21 -0700 Subject: [PATCH 08/21] movers --- CHANGELOG.md | 1 + .../Plugins/ChicoPlugins/Movers/Bouncer.cs | 20 +++++++++++++++++++ .../Plugins/ChicoPlugins/Movers/Facer.cs | 16 +++++++++++++++ .../Plugins/ChicoPlugins/Movers/Spinner.cs | 14 +++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs create mode 100644 Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs create mode 100644 Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 866f3c4..38f0f9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Changelog for ChicoPlugins v0.4 (Not Released) ---- +* New: "Movers" module. Quick components to make objects spin, bounce, or face targets. * Changed: ComponentPool names are now read directly from scene hierarchy. v0.3 (October 15, 2014) diff --git a/Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs b/Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs new file mode 100644 index 0000000..9478634 --- /dev/null +++ b/Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs @@ -0,0 +1,20 @@ +using UnityEngine; +using System.Collections; + +public class Bouncer : MonoBehaviour { + + Vector3 origin; + + public Vector3 dir = Vector3.up; + public float period = 4f; + + void Start () { + origin = transform.position; + } + + // Update is called once per frame + void Update () { + float sin = Mathf.Sin(Time.time / (period * 2f * Mathf.PI)); + transform.position = origin + dir * sin; + } +} diff --git a/Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs b/Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs new file mode 100644 index 0000000..872f417 --- /dev/null +++ b/Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs @@ -0,0 +1,16 @@ +using UnityEngine; +using System.Collections; + +public class Facer : MonoBehaviour { + public Transform target; + public bool freezeVertical = true; + + // Update is called once per frame + void Update () { + Vector3 targetPos = target.position; + if (freezeVertical) { + targetPos.y = transform.position.y; + } + transform.LookAt(targetPos); + } +} diff --git a/Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs b/Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs new file mode 100644 index 0000000..54e3ce1 --- /dev/null +++ b/Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs @@ -0,0 +1,14 @@ +using UnityEngine; +using System.Collections; + +public class Spinner : MonoBehaviour { + + public Vector3 axis = Vector3.up; + public float degPerSecond = 90f; + + public bool local = true; + + void Update () { + transform.Rotate(axis.normalized * Time.deltaTime * degPerSecond, local ? Space.Self : Space.World); + } +} From 504c85973a82ff80592f7728c4fe4d0b25631919 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Wed, 15 Oct 2014 17:50:46 -0700 Subject: [PATCH 09/21] rm bad _id ref --- Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs index 5818590..a1b7e67 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPoolBase.cs @@ -18,7 +18,6 @@ public string id { } protected void Reset() { - _id = name; copyOnEmpty = true; copyOnStart = 0; copyRate = 5; From d69e7dd02e1147f4e985535e0cf98b46ee707a93 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Wed, 15 Oct 2014 18:00:35 -0700 Subject: [PATCH 10/21] update globalCopiesPending on destroy --- Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs index 6f394b6..5b90c21 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/ComponentPool.cs @@ -202,6 +202,7 @@ protected void Update() { protected void OnDestroy() { PoolManager.Remove(id); + globalCopiesPending -= copiesPending; } /// From e20f4be95a41db145f237668cd625d6e3ca1aad1 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 20:55:37 -0800 Subject: [PATCH 11/21] ignore meta files (for now) --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e5dc307..42ca41c 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ *.userprefs # Ignore project settings folder (for now) -Project/ProjectSettings \ No newline at end of file +Project/ProjectSettings +*.meta \ No newline at end of file From 2f6b5653d873607ebbb3f0bcc4056ea632b107f4 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 20:55:46 -0800 Subject: [PATCH 12/21] Introducing the MouseManager script --- .../Plugins/ChicoPlugins/UI/MouseManager.cs | 216 ++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/MouseManager.cs diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/MouseManager.cs b/Project/Assets/Plugins/ChicoPlugins/UI/MouseManager.cs new file mode 100644 index 0000000..35adff0 --- /dev/null +++ b/Project/Assets/Plugins/ChicoPlugins/UI/MouseManager.cs @@ -0,0 +1,216 @@ +using UnityEngine; + +/// +/// Handles and caches mouse, world, UI interaction for each frame. +/// +/// +/// - Builds centralized raycasts to avoid each script doing so ad hoc. +/// - Checks if mouse is over UI, world, etc. +/// - Listens for double clicks. +/// - Remembers last click frame, last release frame, etc. (useful for comparisons) +/// +public class MouseManager : MonoBehaviour { + [SerializeField] Camera _worldCam; + [SerializeField] Camera _uiCam; + [SerializeField] LayerMask _uiLayer; + [SerializeField] LayerMask _floorLayer; + + /// + /// This mouse manager's world camera. Set via inspector. + /// + public Camera worldCam { + get { return _worldCam; } + private set { _worldCam = value; } + } + + /// + /// This mouse manager's UI camera. Set via inspector. + /// + public Camera uiCam { + get { return _uiCam; } + private set { _uiCam = value; } + } + + /// + /// Collision layer mask to check UI collisions. Set via inspector. + /// + public LayerMask uiLayer { + get { return _uiLayer; } + private set { _uiLayer = value; } + } + + /// + /// Data for the current frame. + /// + public Frame currFrame { get; protected set; } + + /// + /// Data for the previous frame. + /// + public Frame prevFrame { get; protected set; } + + /// + /// Data for last frame where currFrame.mouseDown was true. + /// + public Frame lastClickFrame { get; protected set; } + + /// + /// Data for last frame where currFrame.mouseUp was true. + /// + /// The last release frame. + public Frame lastReleaseFrame { get; protected set; } + + /// + /// Returns true for one frame if the user has just clicked and released. + /// + public bool click { get; protected set; } + + /// + /// Returns true for one frame if the user has just tapped twice in quick succession. + /// + public bool doubleClick { get; protected set; } + + public struct Frame { + /// + /// Mouse position for this frame. Measured in screen coords. + /// + public readonly Vector3 mousePos; + + /// + /// Value of Input.GetMouseButtonUp for this frame. + /// + public readonly bool mouseUp; + + /// + /// Value of Input.GetMouseButtonDown for this frame. + /// + public readonly bool mouseDown; + + /// + /// Value of Input.GetMouseButton for this frame. + /// + public readonly bool mouseHeld; + + /// + /// Time.time as of this frame. Measured in seconds. + /// + public readonly float time; + + /// + /// Mouse ray out of UI camera. + /// + public readonly Ray uiRay; + + /// + /// Did uiRay hit anything? + /// + public readonly bool hasUiHit; + + /// + /// If hasUiHit is true, contains the hit data for uiRay. + /// + public readonly RaycastHit uiHit; + + /// + /// Mouse ray out of world camera. + /// + public readonly Ray worldRay; + + /// + /// Did worldRay hit anything? + /// + public readonly bool hasWorldHit; + + /// + /// If hasWorldHit is true, contains the hit data for worldRay. + /// + public readonly RaycastHit worldHit; + + /// + /// True if either hasUiHit or the collider hit by uiHit has changed since last frame. + /// + public readonly bool uiColliderChanged; + + /// + /// True if either hasWorldHit or the collider hit by worldHit has changed since last frame. + /// + public readonly bool worldColliderChanged; + + /// + /// Number of seconds since this frame occurred. + /// + /// The time since frame. + public float timeSinceFrame { + get { return Time.time - time; } + } + + public Frame(Frame prevFrame, Camera uiCam, Camera worldCam, LayerMask uiLayer) { + mousePos = Input.mousePosition; + mouseDown = Input.GetMouseButtonDown(0); + mouseUp = Input.GetMouseButtonUp(0); + mouseHeld = Input.GetMouseButton(0); + + time = Time.time; + + int uiLayerMask = uiLayer.value; + + uiRay = uiCam.ScreenPointToRay(mousePos); + hasUiHit = Physics.Raycast(uiRay, out uiHit, 10000f, uiLayerMask); + uiColliderChanged = (hasUiHit != prevFrame.hasUiHit) || (hasUiHit && uiHit.collider != prevFrame.uiHit.collider); + + worldRay = worldCam.ScreenPointToRay(mousePos); + hasWorldHit = Physics.Raycast(worldRay, out worldHit, 10000f, ~uiLayerMask); + worldColliderChanged = (hasWorldHit != prevFrame.hasWorldHit) || (hasWorldHit && worldHit.collider != prevFrame.worldHit.collider); + } + } + + protected void Awake() { + if (worldCam == null) { + Debug.LogError("MouseManager needs world camera", this); + } + + if (uiCam == null) { + Debug.LogError("MouseManager needs UI camera", this); + } + + if (uiLayer.value == 0) { + Debug.LogWarning("MouseManager has empty UI layer mask (won't hit any UI)", this); + } + } + + protected void Update() { + currFrame = new Frame(prevFrame, uiCam, worldCam, uiLayer); + + click = currFrame.mouseUp && (Time.time - lastClickFrame.time) < 0.3f; + doubleClick = currFrame.mouseDown && (Time.time - lastReleaseFrame.time) < 0.1f; + + if (currFrame.mouseDown) { + lastClickFrame = currFrame; + } + + if (currFrame.mouseUp) { + lastReleaseFrame = currFrame; + } + } + + protected void LateUpdate() { + prevFrame = currFrame; + } + + public Vector3 WorldToUIPoint(Vector3 worldPos) { + Vector3 screenPos = worldCam.WorldToScreenPoint(worldPos).WithZ(0.1f); + Vector3 uiPos = uiCam.ScreenToWorldPoint(screenPos); + return uiPos; + } + + public Vector3 UIToWorldPoint(Vector3 uiPos) { + Vector3 screenPos = uiCam.WorldToScreenPoint(uiPos); + Vector3 worldPos = worldCam.ScreenToWorldPoint(screenPos); + return worldPos; + } + + public Vector3 ScreenToWorldPoint(Vector3 screenPos) { + return worldCam.ScreenToWorldPoint(screenPos); + } + +} \ No newline at end of file From a6fe8037a91371d6972c1237af39b884fb2a4300 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 20:57:51 -0800 Subject: [PATCH 13/21] As mentioned a while back, removing CUI classes. --- .../ChicoPlugins/UI/CUIClickDelegate.cs | 51 --- .../Plugins/ChicoPlugins/UI/CUIClickable.cs | 17 - .../Plugins/ChicoPlugins/UI/CUIController.cs | 340 ------------------ .../Plugins/ChicoPlugins/UI/CUIFloatRange.cs | 36 -- .../Plugins/ChicoPlugins/UI/CUIGotoPanel.cs | 22 -- .../Plugins/ChicoPlugins/UI/CUIIntRange.cs | 31 -- .../Plugins/ChicoPlugins/UI/CUIPanel.cs | 13 - .../Plugins/ChicoPlugins/UI/CUIPanelCam.cs | 213 ----------- .../Plugins/ChicoPlugins/UI/CUIPopPanel.cs | 15 - .../Plugins/ChicoPlugins/UI/CUIPushPanel.cs | 12 - .../Plugins/ChicoPlugins/UI/CUIRange.cs | 65 ---- .../Plugins/ChicoPlugins/UI/CUISendMessage.cs | 45 --- .../Assets/Plugins/ChicoPlugins/UI/CUIText.cs | 59 --- .../Plugins/ChicoPlugins/UI/CUITextButton.cs | 24 -- .../Plugins/ChicoPlugins/UI/CUITextMesh.cs | 30 -- .../Plugins/ChicoPlugins/UI/CUITextWidget.cs | 88 ----- .../Plugins/ChicoPlugins/UI/CUIToggle.cs | 25 -- .../Plugins/ChicoPlugins/UI/CUIValue.cs | 77 ---- 18 files changed, 1163 deletions(-) delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIClickable.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIRange.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs delete mode 100644 Project/Assets/Plugins/ChicoPlugins/UI/CUIValue.cs diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs deleted file mode 100644 index 4b937ec..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickDelegate.cs +++ /dev/null @@ -1,51 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// Pass clicks to other objects via delegates. -/// -[AddComponentMenu("ChicoPlugins/UI/Clickable/Delegate")] -public class CUIClickDelegate : CUIClickable { - public System.Action OnMouseDown; - public System.Action OnMouseUp; - public System.Action OnMouseRelease; - public System.Action OnMouseOut; - public System.Action OnMouseIn; - public System.Action OnMouseHold; - - public override void MouseDown() { - if (OnMouseDown != null) { - OnMouseDown(); - } - } - - public override void MouseUp() { - if (OnMouseUp != null) { - OnMouseUp(); - } - } - - public override void MouseRelease() { - if (OnMouseRelease != null) { - OnMouseRelease(); - } - } - - public override void MouseOut() { - if (OnMouseOut != null) { - OnMouseOut(); - } - } - - public override void MouseIn() { - if (OnMouseIn != null) { - OnMouseIn(); - } - } - - public override void MouseHold() { - if (OnMouseHold != null) { - OnMouseHold(); - } - } -} \ No newline at end of file diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickable.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickable.cs deleted file mode 100644 index 4767ec5..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIClickable.cs +++ /dev/null @@ -1,17 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// UI element that can be clicked. -/// -/// -/// See CUIController for hook descriptions. -/// -public abstract class CUIClickable : MonoBehaviour { - public virtual void MouseDown() {} - public virtual void MouseUp() {} - public virtual void MouseRelease() {} - public virtual void MouseOut() {} - public virtual void MouseIn() {} - public virtual void MouseHold() {} -} \ No newline at end of file diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs deleted file mode 100644 index f0c7073..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIController.cs +++ /dev/null @@ -1,340 +0,0 @@ -using UnityEngine; -using System.Collections.Generic; - -/// -/// Attach to camera, drives UI click behavior. -/// -/// -/// -/// -/// Attempts to handle both touch and mouse input. Takes screen position from -/// mouse (or touches[0]), uses attached camera to raycast for any colliders on -/// same layer as the controller. -/// -/// -/// -/// "Click" is perhaps a bit of a misnomer: this script interprets the cursor as -/// clicking if left-click is held (standalone) or there is at least one touch -/// (mobile). -/// -/// -/// -/// One word of caution: on PC, the notion of "click" and "position" are -/// distinct; the user can move the mouse without clicking. On mobile, touching -/// *is* clicking, which may be confusing at first. -/// -/// -/// -/// Typical setup: place a second "UI" camera in the level, set it up to render -/// over the game scene. Attach CUIController to that camera. Recommend putting -/// all UI on a special layer, collecting all UI widgets under the camera. -/// -/// -/// -/// Recommended camera settings: -/// -/// -/// -/// Orthographic view makes it easy to layer the UI. -/// -/// -/// Clear flags: background only. -/// -/// -/// Culling mask: render UI layer only. -/// -/// -/// Depth: render over the main scene camera. -/// -/// -/// -/// -/// -/// Set of hooks, called at most once per frame in the order listed: -/// -/// -/// -/// MouseDown: called once when user begins click -/// -/// -/// MouseUp: called once when user ends click -/// -/// -/// MouseRelease: called once when MouseDown and MouseUp targeted the same GameObject -/// -/// -/// MouseOut: called each time the mouse exits a target. -/// -/// -/// MouseIn: called each time the mouse changes targets. -/// -/// -/// MouseHold: called once per frame while click is held over and object. -/// -/// -/// -/// -/// -[RequireComponent(typeof(Camera))] -[AddComponentMenu("ChicoPlugins/UI/Controller")] -public class CUIController : MonoBehaviour { - /// - /// Cached camera reference (read-only). - /// - public Camera uiCamera { get; private set; } - - /// - /// LayerMask used for raycast (read-only). - /// - public int uiLayerMask { get; private set; } - - /// - /// This frame, mouse cursor is over this GameObject (read-only). - /// - public GameObject mouseTarget { get; private set; } - - /// - /// Last frame, mouse cursor was over this GameObject (read-only). - /// - public GameObject lastMouseTarget { get; private set; } - - /// - /// This frame, mouse's screen position (read-only). - /// - public Vector3 mousePos { get; private set; } - - /// - /// Last frame, mouse's screen position (read-only). - /// - public Vector3 lastMousePos { get; private set; } - - /// - /// This frame, ray used for casting (read-only). - /// - public Ray mouseRay { get; private set; } - - /// - /// Last frame, ray used for casting (read-only). - /// - public Ray lastMouseRay { get; private set; } - - /// - /// This frame, is mouse pressed? (read-only) - /// - public bool mouseDown { get; private set; } - - /// - /// Last frame, was mouse pressed? (read-only) - /// - public bool lastMouseDown { get; private set; } - - /// - /// mouseTarget during the frame we first clicked (read-only). - /// - public GameObject mouseDownTarget { get; private set; } - - /// - /// Is this the "main" CUIController in the scene? (inspector) - /// - public bool isMain = false; - - /// - /// Returns first "main" CUIController in the scene. - /// - public static CUIController main { get; private set; } - - protected void Awake() { - uiCamera = GetComponent(); - uiLayerMask = 1 << gameObject.layer; - - if (isMain) { - if (main != null) { - Debug.LogWarning("Ignoring extra 'main' CUIController.", this); - } else { - main = this; - } - } - - OnAwake(); - } - - protected void Start() { - if (main == null) { - main = this; - } - OnStart(); - } - - protected void Update() { - UpdateFrameValues(); - OnUpdate(); - } - - protected void LateUpdate() { - CacheLastFrameValues(); - } - - /// - /// Override for custom click detection. - /// - /// True if clicking during this frame. - protected virtual bool GetMouseDown() { - #if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR - return Input.touchCount > 0; - - #else - return Input.GetMouseButton(0); - - #endif - } - - /// - /// Override for custom mouse position detection. - /// - /// Screen position for GetMouseRay (be careful to assign a z-component). - protected virtual Vector3 GetMousePosition() { - Vector3 mp = Input.mousePosition.WithZ(0.1f); - - #if UNITY_ANDROID || UNITY_IPHONE - if (!mouseDown) { - mp = Vector3.zero; - } - #endif - - return mp; - } - - /// - /// Override for custom raycast calculation. - /// - /// Ray to use for GetMouseTarget. - protected virtual Ray GetMouseRay() { - return uiCamera.ScreenPointToRay(mousePos); - } - - /// - /// Override for custom target picking. - /// - /// This frame's mouseTarget. - protected virtual GameObject GetMouseTarget() { - GameObject target = null; - RaycastHit hit; - if (mousePos != Vector3.zero && Physics.Raycast(mouseRay, out hit, 100000f, uiLayerMask)) { - target = hit.collider.gameObject; - } - return target; - } - - /// - /// Called at the beginning of each frame, updates internal values. - /// - protected virtual void UpdateFrameValues() { - mouseDown = GetMouseDown(); - mousePos = GetMousePosition(); - mouseRay = GetMouseRay(); - mouseTarget = GetMouseTarget(); - } - - /// - /// Called at the end of each frame, caches internal values. - /// - protected virtual void CacheLastFrameValues() { - lastMouseDown = mouseDown; - lastMousePos = mousePos; - lastMouseRay = mouseRay; - lastMouseTarget = mouseTarget; - } - - /// - /// Override to add to Awake(). - /// - protected virtual void OnAwake() {} - - /// - /// Override to add to Start(). - /// - protected virtual void OnStart() {} - - /// - /// Removes "main" if needed. Overrides must call base! - /// - protected virtual void OnDestroy() { - if (main == this) { - main = null; - } - } - - /// - /// Handles per-frame hook calls. Override to customize. - /// - protected virtual void OnUpdate() { - if (mouseDown && !lastMouseDown) { - OnMouseDown(); - } else if (!mouseDown && lastMouseDown) { - OnMouseUp(); - } - - //always assume mouse has moved -- because the camera itself might have - OnMouseMove(); - - if (mouseDown) { - OnMouseHold(); - } - } - - /// - /// Called once when click begins. Calls "MouseDown". - /// - protected virtual void OnMouseDown() { - mouseDownTarget = mouseTarget; - if (mouseDownTarget != null) { - Send(mouseDownTarget, "MouseDown"); - } - } - - /// - /// Called once when click ends. Calls "MouseUp", "MouseRelease". - /// - protected virtual void OnMouseUp() { - if (lastMouseTarget != null) { - Send(lastMouseTarget, "MouseUp"); - } - if (mouseDownTarget != null && mouseDownTarget == lastMouseTarget) { - Send(lastMouseTarget, "MouseRelease"); - } - mouseDownTarget = null; - } - - /// - /// Called once per frame when position moves. Calls "MouseOut", "MouseIn". - /// - protected virtual void OnMouseMove() { - if (mouseTarget != lastMouseTarget) { - if (lastMouseTarget != null) { - Send(lastMouseTarget, "MouseOut"); - } - if (mouseTarget != null) { - Send(mouseTarget, "MouseIn"); - } - } - } - - /// - /// Called once per frame while clicking. Calls "MouseHold". - /// - protected virtual void OnMouseHold() { - if (mouseTarget != null) { - Send(mouseTarget, "MouseHold"); - } - } - - /// - /// Alias for SendMessage. - /// - protected void Send(GameObject target, string message) { - //Debug.Log(string.Format("{0} ({1})", message, target.name), target); - target.SendMessage(message, SendMessageOptions.DontRequireReceiver); - } - - -} \ No newline at end of file diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs deleted file mode 100644 index e9c34ff..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIFloatRange.cs +++ /dev/null @@ -1,36 +0,0 @@ -using UnityEngine; -using System.Collections; - -[AddComponentMenu("ChicoPlugins/UI/Range/Float")] -public class CUIFloatRange : CUIRange { - protected override float ClampValue(float newValue, float min, float max) { - return Mathf.Clamp(newValue, min, max); - } - - protected override string GetString(float newValue) { - string format = "n0"; - if (step <= 0.001f) { - format = "n3"; - } else if (step <= 0.01f) { - format = "n2"; - } else if (step <= 0.1f) { - format = "n1"; - } - return newValue.ToString(format); - } - - protected override void Reset() { - defaultValue = 5f; - min = 0f; - max = 10f; - step = 0.5f; - } - - public override void UpClick() { - SetValue(current + step); - } - - public override void DownClick() { - SetValue(current - step); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs deleted file mode 100644 index 7557aca..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIGotoPanel.cs +++ /dev/null @@ -1,22 +0,0 @@ -using UnityEngine; -using System.Collections.Generic; - -/// -/// Button. When clicked, tells CUIPanelCam to switch panels WITHOUT a push. -/// -[AddComponentMenu("ChicoPlugins/UI/Clickable/Goto Panel")] -public class CUIGotoPanel : CUIClickable { - public string panelName; - - public override void MouseRelease() { - - //TODO - foreach (CUIPanelCam cam in CUIPanelCam.panelCams) { - GotoPanel(cam, panelName); - } - } - - protected virtual void GotoPanel(CUIPanelCam cam, string target) { - cam.SwitchPanel(target); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs deleted file mode 100644 index 6b32a7f..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIIntRange.cs +++ /dev/null @@ -1,31 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// Two buttons allowing user to pick an int. -/// -[AddComponentMenu("ChicoPlugins/UI/Range/Int")] -public class CUIIntRange : CUIRange { - protected override int ClampValue(int newValue, int min, int max) { - return Mathf.Clamp(newValue, min, max); - } - - protected override string GetString(int newValue) { - return newValue.ToString("n"); - } - - protected override void Reset() { - defaultValue = 5; - min = 1; - max = 10; - step = 1; - } - - public override void UpClick() { - SetValue(current + step); - } - - public override void DownClick() { - SetValue(current - step); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs deleted file mode 100644 index 7e3b204..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// For use with CUIPanelCam. -/// -[AddComponentMenu("ChicoPlugins/UI/Panel")] -public class CUIPanel : MonoBehaviour { - /// - /// Unique name for this panel. Leave blank to use GameObject's name. - /// - public string panelName = ""; -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs deleted file mode 100644 index 80a9a47..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPanelCam.cs +++ /dev/null @@ -1,213 +0,0 @@ -using UnityEngine; -using System.Collections.Generic; - -/// -/// Attach to a UI camera so that it can scroll between various "panels". -/// -/// -/// -/// -/// At startup, this script recursively searches its children for CUIPanel -/// components, registering them as it goes. You can later tell it to "switch" -/// the camera between those panels. -/// -/// -/// -/// A CUIPanel should not contain another CUIPanel. Such panels will -/// be ignored by the search. -/// -/// -/// -/// CUIPanel does not apply any restriction as far as what the user can click. -/// It is a location in space, and nothing more. -/// -/// -/// -/// Generally, you'll control this by calling PushPanel (advances one panel) and -/// PopPanel (goes back one panel). These functions can easily be controlled by -/// the CUIPushPanel and CUIPopPanel widgets. -/// -/// -/// -/// Highly suggest you read the comments on handleBackButton and quitOnLastBack. -/// -/// -/// -/// Note this is technically a ChaseCam (which is a TweenCam), so you can access -/// all those functions if you need to. -/// -/// -/// -[RequireComponent(typeof(Camera))] -[AddComponentMenu("ChicoPlugins/UI/PanelCam")] -public class CUIPanelCam : ChaseCam { - Dictionary panels = new Dictionary(); - - List backPanels = new List(); //mostly like a stack, but we need to iterate sometimes - - static HashSet _panelCams = new HashSet(); - - /// - /// Enumerator for all CUIPanelCams currently active. - /// - public static CUIPanelCam[] panelCams { - get { - CUIPanelCam[] cams = new CUIPanelCam[_panelCams.Count]; - _panelCams.CopyTo(cams); - return cams; - } - } - - /// - /// At start, snap to this panel (inspector). - /// - public string defaultPanel = "main"; - - /// - /// Handle "back" or "escape" keys by calling PopPanel? (inspector) - /// - public bool handleBackButton = true; - - /// - /// When calling PopPanel with no more panels, should the game quit? (inspector) - /// - public bool quitOnLastBack = true; - - /// - /// Delegate to call when we PopPanel with no more panels. (inspector) - /// - public System.Action onLastBack; - - public float switchTime = 0.25f; - - public CUIPanel currentPanel { get; protected set; } - - protected override void OnAwake() { - base.OnAwake(); - _panelCams.Add(this); - } - - protected override void OnStart() { - base.OnStart(); - RegisterPanels(target); - - if (panels.ContainsKey(defaultPanel)) { - currentPanel = panels[defaultPanel]; - SnapTarget(panels[defaultPanel].transform); - } - } - - /// - /// Add a CUIPanel to the set. Note that children are automatically searched at startup. - /// - /// - /// Panel names must be unique within a set; if a panel has no name set, its - /// GameObject name is used instead. - /// - public void AddPanel(CUIPanel panel) { - //use panel's name (or its GameObject name) - string panelName = panel.panelName; - if (string.IsNullOrEmpty(panelName)) { - panelName = panel.name; - } - - //names must be unique - if (panels.ContainsKey(panelName)) { - Debug.LogWarning("Duplicate CUIPanel name '"+panel.panelName+"'", this); - } else { - panels.Add(panelName, panel); - } - } - - /// - /// Remove a CUIPanel from the set. - /// - public void RemovePanel(CUIPanel panel) { - panels.Remove(panel.panelName); - - //edge case: if panel being removed was on the stack, we must remove it - while (backPanels.Remove(panel)); - } - - /// - /// Move the camera to a CUIPanel (by name). - /// - public void SwitchPanel(string panelName) { - CUIPanel p; - if (panels.TryGetValue(panelName, out p)) { - currentPanel = p; - SwitchTarget(p.transform, switchTime); - } else { - Debug.LogWarning("CUIPanelCam has no panel named '"+panelName+"'", this); - } - } - - /// - /// Move the camera to a CUIPanel, add it to the list of "back" panels. - /// - public void PushPanel(string panelName) { - CUIPanel p; - if (panels.TryGetValue(panelName, out p)) { - backPanels.Add(currentPanel); - currentPanel = p; - SwitchTarget(p.transform, switchTime); - } else { - Debug.LogWarning("CUIPanelCam has no panel named '"+panelName+"'", this); - } - } - - /// - /// Move the camera back one panel. If no more panels, call delegate and/or quit. - /// - /// - /// If the backPanels list is empty, we will call the onLastBackDelegate; if - /// quitOnLastBack is true, we will then ALSO call Application.Quit. - /// - public void PopPanel() { - int index = backPanels.Count - 1; - if (index >= 0) { - CUIPanel p = backPanels[index]; - backPanels.RemoveAt(index); - currentPanel = p; - SwitchTarget(p.transform, switchTime); - Debug.Log("Back to " + p); - } else { - Debug.Log("LastBack"); //TODO - if (onLastBack != null) { - onLastBack(); - } - if (quitOnLastBack) { - Application.Quit(); - } - } - } - - void RegisterPanels(Transform root) { - CUIPanel panel = root.GetComponent(); - if (panel != null) { - AddPanel(panel); - } else { - foreach (Transform child in root) { - RegisterPanels(child); - } - } - } - - protected override CamState GetStateDefault(CamState state) { - state.pos = target.position - target.forward * 10f; - state.target = target.position; - return state; - } - - protected override void OnUpdate() { - base.OnUpdate(); - if (handleBackButton && Input.GetKeyDown(KeyCode.Escape)) { - Debug.Log("Popping panel"); //TODO - PopPanel(); - } - } - - protected void OnDestroy() { - _panelCams.Remove(this); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs deleted file mode 100644 index f7338f0..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPopPanel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using UnityEngine; -using System.Collections.Generic; - -/// -/// Button. Pairs with CUIPopPanel. When clicked, tells CUIPanelCam to go back one panel. -/// -[AddComponentMenu("ChicoPlugins/UI/Clickable/Pop Panel")] -public class CUIPopPanel : CUIClickable { - - public override void MouseRelease() { - foreach (CUIPanelCam cam in CUIPanelCam.panelCams) { - cam.PopPanel(); - } - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs deleted file mode 100644 index 7de67df..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIPushPanel.cs +++ /dev/null @@ -1,12 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// Button. Pairs with CUIPopPanel. When clicked, tells CUIPanelCam to go forward one panel. -/// -[AddComponentMenu("ChicoPlugins/UI/Clickable/Push Panel")] -public class CUIPushPanel : CUIGotoPanel { - protected override void GotoPanel(CUIPanelCam cam, string target) { - cam.PushPanel(target); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIRange.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIRange.cs deleted file mode 100644 index e3bb6d1..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIRange.cs +++ /dev/null @@ -1,65 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// UI widget representing a range of values (int picker, float picker, etc.) -/// -public abstract class CUIRange : CUIValue { - /// - /// Button to call DownClick. - /// - public CUIClickDelegate downButton; - - /// - /// Button to call UpClick. - /// - public CUIClickDelegate upButton; - - /// - /// Range should disallow values below this (inspector). - /// - public T min; - - /// - /// Range should disallow values above this (inspector). - /// - public T max; - - /// - /// On button press, range should change by this much (inspector). - /// - public T step; - - protected override void OnStart() { - if (downButton != null) { - downButton.OnMouseRelease = () => this.DownClick(); - } else { - Debug.LogWarning("CUIRange missing downButton", this); - } - if (upButton != null) { - upButton.OnMouseRelease = () => this.UpClick(); - } else { - Debug.LogWarning("CUIRange missing upButton", this); - } - } - - protected override T OnSetValue(T newValue) { - newValue = ClampValue(newValue, min, max); - return newValue; - } - - /// - /// Override to provide type-specific clamp function. - /// - protected abstract T ClampValue(T newValue, T min, T max); - - /// - /// Called when "up" button is pressed. - /// - public abstract void UpClick(); - - /// - /// Called when "down" button is pressed. - /// - public abstract void DownClick(); -} \ No newline at end of file diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs deleted file mode 100644 index 8c7432b..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUISendMessage.cs +++ /dev/null @@ -1,45 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// Pass clicks to other GameObjects via SendMessage. -/// -[AddComponentMenu("ChicoPlugins/UI/Clickable/SendMessage")] -public class CUISendMessage : CUIClickable { - public GameObject target; - - public string mouseDownMessage; - public string mouseUpMessage; - public string mouseReleaseMessage; - public string mouseOutMessage; - public string mouseInMessage; - public string mouseHoldMessage; - - public override void MouseDown() { - if (!string.IsNullOrEmpty(mouseDownMessage)) Send(mouseDownMessage); - } - - public override void MouseUp() { - if (!string.IsNullOrEmpty(mouseUpMessage)) Send(mouseUpMessage); - } - - public override void MouseRelease() { - if (!string.IsNullOrEmpty(mouseReleaseMessage)) Send(mouseReleaseMessage); - } - - public override void MouseOut() { - if (!string.IsNullOrEmpty(mouseOutMessage)) Send(mouseOutMessage); - } - - public override void MouseIn() { - if (!string.IsNullOrEmpty(mouseInMessage)) Send(mouseInMessage); - } - - public override void MouseHold() { - if (!string.IsNullOrEmpty(mouseHoldMessage)) Send(mouseHoldMessage); - } - - protected void Send(string message) { - target.SendMessage(message, SendMessageOptions.DontRequireReceiver); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs deleted file mode 100644 index 919944e..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIText.cs +++ /dev/null @@ -1,59 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// UI widget for text. Extend for specific text classes (ie: TextMesh). -/// -public abstract class CUIText : MonoBehaviour { - string lastMessage; - Color lastColor; - - protected bool initialized { get; private set; } - - public string message { - get { - Init(); - return lastMessage; - } - set { - Init(); - if (value != lastMessage) { - lastMessage = value; - ApplyMessage(value); - } - } - } - - public Color color { - get { - Init(); - return lastColor; - } - set { - Init(); - if (value != lastColor) { - lastColor = value; - ApplyColor(value); - } - } - } - - protected void Init() { - if (initialized) return; - - OnInit(); - - lastMessage = GetMessage(); - lastColor = GetColor(); - - initialized = true; - } - - protected virtual void OnInit() {} - - protected abstract void ApplyMessage(string newMessage); - protected abstract void ApplyColor(Color newColor); - - protected abstract string GetMessage(); - protected abstract Color GetColor(); -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs deleted file mode 100644 index 7f2676f..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextButton.cs +++ /dev/null @@ -1,24 +0,0 @@ -using UnityEngine; -using System.Collections; - -[AddComponentMenu("ChicoPlugins/UI/Text/TextButton")] -public class CUITextButton : CUITextWidget { - public Color pressColor = Color.red; - public float pressScale = 1.1f; - - protected ButtonState pressState; - - protected override void OnStart() { - pressState = new ButtonState(pressColor, transform.localScale * pressScale); - } - - public override void MouseIn() { -// Debug.Log("MouseIn"); - SwitchState(pressState, 0.1f); - } - - public override void MouseOut() { -// Debug.Log("MouseOut"); - SwitchState(mainState, 0.2f); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs deleted file mode 100644 index 9b7132a..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextMesh.cs +++ /dev/null @@ -1,30 +0,0 @@ -using UnityEngine; -using System.Collections; - -[RequireComponent(typeof(TextMesh))] -[AddComponentMenu("ChicoPlugins/UI/Text/TextMesh")] -public class CUITextMesh : CUIText { - protected TextMesh tm; - - protected override void OnInit() { - if (initialized) return; - - tm = GetComponent(); - } - - protected override void ApplyMessage(string newMessage) { - tm.text = newMessage; - } - - protected override void ApplyColor(Color newColor) { - tm.color = newColor; - } - - protected override string GetMessage() { - return tm.text; - } - - protected override Color GetColor() { - return tm.color; - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs deleted file mode 100644 index 64153bb..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUITextWidget.cs +++ /dev/null @@ -1,88 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// Widget to manage a child CUIText. -/// -[AddComponentMenu("ChicoPlugins/UI/Clickable/TextWidget")] -public class CUITextWidget : CUIClickable { - protected CUIText text { get; private set; } - - public string message { - get { return text.message; } - set { text.message = value; } - } - - public Color color { - get { return text.color; } - set { text.color = value; } - } - - public struct ButtonState { - public Color color; - public Vector3 size; - - public ButtonState(Color color, Vector3 size) { - this.color = color; - this.size = size; - } - } - - TweenState tween; - ButtonState state; - ButtonState lastState; - - protected ButtonState mainState; - - protected void Awake() { - text = GetComponentInChildren(); - if (text == null) { - Debug.LogWarning("CUITextButton without TextMesh!", this); - return; - } - - tween = new TweenState(0f); - mainState = new ButtonState(text.color, transform.localScale); - lastState = state = mainState; - - OnStart(); - } - - - protected virtual void Update() { - if (tween.Tick(Time.deltaTime)) { - text.color = Color.Lerp(lastState.color, state.color, tween.perc); - transform.localScale = Vector3.Lerp(lastState.size, state.size, tween.perc); - } - } - - public void PulseScale(float scale, float duration) { - Pulse(mainState.color, mainState.size * scale, duration); - } - - public void PulseColor(Color color, float duration) { - Pulse(color, mainState.size, duration); - } - - public void Pulse(Color color, Vector3 scale, float duration) { - ButtonState newState = new ButtonState(color, scale); - SnapState(newState); - SwitchState(mainState, duration); - } - - protected void SwitchState(ButtonState newState, float duration) { - lastState = new ButtonState(text.color, transform.localScale); - state = newState; - tween.Reset(duration); - } - - protected void SnapState(ButtonState newState) { - lastState = state; - state = newState; - tween.Stop(); - text.color = newState.color; - transform.localScale = newState.size; - } - - protected virtual void OnStart() {} -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs deleted file mode 100644 index 814e66f..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIToggle.cs +++ /dev/null @@ -1,25 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// Button allows user to toggle a bool. -/// -[AddComponentMenu("ChicoPlugins/UI/Toggle")] -public class CUIToggle : CUIValue { - public GameObject enabledWidget; - public GameObject disabledWidget; - - protected override bool OnSetValue(bool newValue) { - if (enabledWidget != null) { - enabledWidget.SetActive(newValue); - } - if (disabledWidget != null) { - disabledWidget.SetActive(!newValue); - } - return newValue; - } - - public override void MouseRelease() { - SetValue(!current); - } -} diff --git a/Project/Assets/Plugins/ChicoPlugins/UI/CUIValue.cs b/Project/Assets/Plugins/ChicoPlugins/UI/CUIValue.cs deleted file mode 100644 index 1ab2ae3..0000000 --- a/Project/Assets/Plugins/ChicoPlugins/UI/CUIValue.cs +++ /dev/null @@ -1,77 +0,0 @@ -using UnityEngine; -using System.Collections; - -/// -/// UI widget representing a value that can be changed (int, float, string, etc.) -/// -public abstract class CUIValue : CUIClickable { - public T defaultValue = default(T); - - /// - /// Delegate to be called by SetValue. - /// - public System.Action onChange; - - /// - /// Widget's current value (read-only). - /// - public T current { get; private set; } - - /// - /// Widget to show the current value. - /// - public CUIText textWidget; - - protected void Start() { - SetValue(GetDefaultValue(), false); - OnStart(); - } - - /// - /// Call to change the widget's value. - /// - /// Input value. - /// Call the onChange delegate? - public void SetValue(T newValue, bool fireOnChange=true) { - current = OnSetValue(newValue); - if (textWidget != null) { - textWidget.message = GetString(newValue); - } - if (fireOnChange && onChange != null) { - onChange(current); - } - } - - /// - /// Called by SetValue. Gives us a chance to handle or modify the input. - /// - /// Input value. - /// newValue (possibly modified) - protected virtual T OnSetValue(T newValue) { - return newValue; - } - - /// - /// Override to provide type-specific ToString function. - /// - protected virtual string GetString(T newValue) { - return newValue.ToString(); - } - - /// - /// Called at the end of Start(). - /// - protected virtual void OnStart() {} - - /// - /// Called when the component is attached; override to set default values. - /// - protected virtual void Reset() {} - - /// - /// Optional override. Called during Start() to populate our default value. - /// - protected virtual T GetDefaultValue() { - return defaultValue; - } -} \ No newline at end of file From c52e6e40dd7e129a907bbe7006eb4e95f79c878c Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 21:04:02 -0800 Subject: [PATCH 14/21] document AudioPool --- .../Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs b/Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs index 5a6c5b4..491c7bb 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Pools/AudioPool.cs @@ -1,6 +1,17 @@ using UnityEngine; using System.Collections.Generic; +/// +/// AudioSource pool. +/// +/// +/// Any AudioSource released will be added to an internal watchlist; the list +/// is periodically checked, and any finished sound will be returned to the +/// pool. +/// +/// If activateOnGet is true, the pool will call Play() on any AudioSource it +/// releases; otherwise, you'll need to do that yourself. +/// [AddComponentMenu("ChicoPlugins/Pools/Audio Source")] public class AudioPool : ComponentPool { [SerializeField] float _reclaimRate = 0.5f; From 88dbea947a2cc922d44102138acd7d74cf2f11f8 Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 21:07:58 -0800 Subject: [PATCH 15/21] Better analogies for the bag classes. --- Project/Assets/Plugins/ChicoPlugins/Bags/RandomBag.cs | 2 +- Project/Assets/Plugins/ChicoPlugins/Bags/ShuffleBag.cs | 6 ++++-- Project/Assets/Plugins/ChicoPlugins/Bags/WeightedBag.cs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Project/Assets/Plugins/ChicoPlugins/Bags/RandomBag.cs b/Project/Assets/Plugins/ChicoPlugins/Bags/RandomBag.cs index 4875c39..183bee9 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Bags/RandomBag.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Bags/RandomBag.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; /// -/// Bag returns items in random order. +/// Bag returns items at random, like a fair dice roll. /// public class RandomBag : SimpleBag { protected List members; diff --git a/Project/Assets/Plugins/ChicoPlugins/Bags/ShuffleBag.cs b/Project/Assets/Plugins/ChicoPlugins/Bags/ShuffleBag.cs index d04ba51..aa5c062 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Bags/ShuffleBag.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Bags/ShuffleBag.cs @@ -2,9 +2,11 @@ using System.Collections.Generic; /// -/// Bag returns item in random order. Will not repeat until bag is "emptied" and "refilled". -/// +/// Bag returns items at random, like a deck of cards. /// +/// +/// Note that the big will automatically refill itself once emptied. +/// public class ShuffleBag : RandomBag { /// /// An internal list, used by Refill(). diff --git a/Project/Assets/Plugins/ChicoPlugins/Bags/WeightedBag.cs b/Project/Assets/Plugins/ChicoPlugins/Bags/WeightedBag.cs index 54db0dc..449b40b 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Bags/WeightedBag.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Bags/WeightedBag.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; /// -/// Bag returns item with weighted randomness. Items are NOT removed. +/// Bag returns items at random with weights, like a biased dice roll. /// public class WeightedBag : AbstractBag { const float DEFAULT_WEIGHT = 1f; From 074bff026740b8b40789600dc88e94b45de2f1cd Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 21:14:14 -0800 Subject: [PATCH 16/21] component menu entries, docs for movers --- Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs | 4 ++++ Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs | 4 ++++ Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs b/Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs index 9478634..e95e913 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Movers/Bouncer.cs @@ -1,6 +1,10 @@ using UnityEngine; using System.Collections; +/// +/// Simple movement: bounces an object up and down. +/// +[AddComponentMenu("ChicoPlugins/Movers/Bouncer")] public class Bouncer : MonoBehaviour { Vector3 origin; diff --git a/Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs b/Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs index 872f417..37a7bba 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Movers/Facer.cs @@ -1,6 +1,10 @@ using UnityEngine; using System.Collections; +/// +/// Simple movement: makes one object face another, once per frame. +/// +[AddComponentMenu("ChicoPlugins/Movers/Facer")] public class Facer : MonoBehaviour { public Transform target; public bool freezeVertical = true; diff --git a/Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs b/Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs index 54e3ce1..a1f5cb3 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Movers/Spinner.cs @@ -1,6 +1,10 @@ using UnityEngine; using System.Collections; +/// +/// Simple movement: rotates an object once per frame. +/// +[AddComponentMenu("ChicoPlugins/Movers/Spinner")] public class Spinner : MonoBehaviour { public Vector3 axis = Vector3.up; From 8a52d4cc1085f3cfc1af2ca09517c27f6c47e23c Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 21:15:14 -0800 Subject: [PATCH 17/21] component menu entries, docs for loaders --- Project/Assets/Plugins/ChicoPlugins/Loading/LevelLoader.cs | 1 + Project/Assets/Plugins/ChicoPlugins/Loading/SimpleLoadUI.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/Project/Assets/Plugins/ChicoPlugins/Loading/LevelLoader.cs b/Project/Assets/Plugins/ChicoPlugins/Loading/LevelLoader.cs index 9f2ff97..64d5c6c 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Loading/LevelLoader.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Loading/LevelLoader.cs @@ -4,6 +4,7 @@ /// /// Switch levels with a loading screen. /// +[AddComponentMenu("ChicoPlugins/Loading/Level Loader")] public class LevelLoader : MonoBehaviour { /// /// Global hook for level loading with transition scene. diff --git a/Project/Assets/Plugins/ChicoPlugins/Loading/SimpleLoadUI.cs b/Project/Assets/Plugins/ChicoPlugins/Loading/SimpleLoadUI.cs index 3d5eac8..00d0b8f 100644 --- a/Project/Assets/Plugins/ChicoPlugins/Loading/SimpleLoadUI.cs +++ b/Project/Assets/Plugins/ChicoPlugins/Loading/SimpleLoadUI.cs @@ -6,6 +6,7 @@ /// /// Recommended for use as a placeholder with LevelLoader. /// +[AddComponentMenu("ChicoPlugins/Loading/Simple Load UI")] public class SimpleLoadUI : MonoBehaviour { public Color backgroundColor = Color.black; public Color textColor = Color.blue; From c67f7ba9314866e1424ee14157a76da2a2057fea Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 21:16:16 -0800 Subject: [PATCH 18/21] Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38f0f9f..77569d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ v0.4 (Not Released) ---- * New: "Movers" module. Quick components to make objects spin, bounce, or face targets. +* New: "MouseManager" class. Helpful for tracking simple mouse and touch input over multiple frames. * Changed: ComponentPool names are now read directly from scene hierarchy. +* Removed: CUI module. +* Improved documentation for several classes. + v0.3 (October 15, 2014) ---- From 6a6b4a0a26e45dab5e25d63fdf3efb848c29345f Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 21:29:03 -0800 Subject: [PATCH 19/21] license file in project (for export) --- .../Assets/Plugins/ChicoPlugins/LICENSE.txt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Project/Assets/Plugins/ChicoPlugins/LICENSE.txt diff --git a/Project/Assets/Plugins/ChicoPlugins/LICENSE.txt b/Project/Assets/Plugins/ChicoPlugins/LICENSE.txt new file mode 100644 index 0000000..826f07b --- /dev/null +++ b/Project/Assets/Plugins/ChicoPlugins/LICENSE.txt @@ -0,0 +1,22 @@ +ChicoPlugins + +The MIT License (MIT) + +Copyright (c) 2013 Robert Utter + +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. From 64baa8d3533d575fa0b01e9c8bb0eaebe9890dea Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 22:53:13 -0800 Subject: [PATCH 20/21] Much better README --- README.md | 158 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 103 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 048a4c6..236dfd2 100644 --- a/README.md +++ b/README.md @@ -9,39 +9,29 @@ This project is a collection of files and modules that I've found useful for mul Installation ---- -Many files, functions, or modules may be useful individually. Those, you can download or copy directly. +This is fundamentally a loose collection of scripts. Some of them are useful individually, so feel free to just download one or two as long as you keep to the MIT license. -To deploy the plugins in your project: +To import the whole set in one go, you can download a `.unitypackage` file from the releases link. -* Clone the repo to your machine -* Copy the contents of `Project/Assets/Plugins` into your project's `Assets/Plugins` folder -* Future releases may include .unitypackage archives to make this simpler +If you'd rather get a development copy, you can clone the repo and open the `Project` folder in Unity: -To get a full development build: - -* Clone the repo to your machine -* The `Project` folder contains everything you need to create a Unity project -* In Unity: `File` > `New Project` > `Browse...` > Select the cloned "Project" folder +* `git clone https://github.com/areyoutoo/ChicoPlugins.git` +* In Unity: `File` > `New Project` > `Browse...` > Find the cloned repo and select the "Project" folder * Unity will automatically create metadata files (asset database, project settings, solutions, etc.) Bags module ---- -Helper classes to manage the storage and randomized retrieval of values. [Managed randomness](http://gamedevelopment.tutsplus.com/tutorials/shuffle-bags-making-random-feel-more-random--gamedev-1249) sometimes [feels more authentic](http://seanmonstar.com/post/708989796/a-less-random-generator) for gameplay. - -All bags provide a common `GetNext()` call. Most of them provide a simple `Add(T)` call, or can be populated during construction by passing any IEnumerable reference. +The Bags module has some helper classes to collect values and retrieve them at random. -
-
RandomBag
-
Pick items with uniform randomness.
+* **RandomBag** returns items at random, like a fair dice roll. +* **ShuffleBag** returns items at random, like a deck of cards. It also refills automatically once empty. +* **WeightedBag** returns items with weighted randomness, like a biased dice roll. -
ShuffleBag
-
Pick items with uniform randomness; avoids repeating items until the bag has been completely used (and automatically refilled).
+[Managed randomness](http://gamedevelopment.tutsplus.com/tutorials/shuffle-bags-making-random-feel-more-random--gamedev-1249) sometimes [feels more authentic](http://seanmonstar.com/post/708989796/a-less-random-generator) for gameplay. -
WeightedBag
-
Pick items with weighted randomness.
-
+You can retrieve a value from a bag by calling its `GetNext()` method. Most of the bags can be filled by calling `Add(T)`, or by passing an IEnumerable to their constructor. ComponentPools module @@ -49,67 +39,125 @@ ComponentPools module Instantiate() and Destroy() can be expensive calls; it is often more performant to recycle GameObjects using an [object pool](http://answers.unity3d.com/questions/196413/gameobject-pool-to-avoid-performance-issues-in-ins.html) pattern. -Our pools focus on components and the scene hierarchy. With a scene hierarchy like this: +Our pools are managed directly in the scene hierarchy. - Pool (has ParticlePool named "Explosion") - Pooled object (has ParticleSystem) - Pooled object (has ParticleSystem) +Here's an example using a ParticlePool, which stores ParticleSystem components: -It's easy to find and recycle those particles: - - PoolManager.Get("Explosion").GetNextAt(Vector3.zero); + Scene (your scene file) -Or: + ParticlePools (empty container) + + ExplosionParticlePool (has a ParticlePool attached) + Explosion (has a ParticleSystem attached) + Explosion (has a ParticleSystem attached) + + SmokeParticlePool (has a ParticlePool attached) + Smoke (has a ParticleSystem attached) + Smoke (has a ParticleSystem attached) + +When your code needs to play an explosion: ParticleSystem ps; - ParticlePool.TryGetNextAt("Explosion", Vector3.zero, out ps); + ParticlePool.TryGetNextAt("ExplosionParticlePool", transform.position out ps); -* The pool will move a particle system to the requested location. -* By default, the particles will play immediately. -* By default, new particle systems will be spawned when the pool is empty. -* Finished particles will be reclaimed automatically. +That will find a copy of the "Explosion" effect and move it to our position. With default settings, it will even activate it for us, and return it to the pool once it's finished playing. -Pool structure is managed at runtime, using the scene hierarchy. +Here's a slightly more robust example: -
-
PoolManager
-
Static class to easily find and use pools.
+ ParticleSystem mySmoke, myExplosion; + ParticleSystem ps; + if (ParticlePool.TryGetNextAt("ExplosionParticlePool", transform.position, out ps)) + { + myExplosion = ps; + Debug.Log("Playing Explosion", this); + } + else + { + Debug.Log("Failed to get Explosion copy", this); + } + + if (ParticlePool.TryGetNextAt("SmokeParticlePool", transform.position, out ps)) + { + mySmoke = ps; + Debug.Log("Playing Smoke", this); + } + else + { + Debug.Log("Failed to get Smoke copy", this); + } + +If the pool is empty, default settings will clone another copy of the effect. + +Repeated cloning can be attempted in batches of increasing size, with each batch occurring over multiple frames, to reduce performance overhead. + +While the pool manages only a single component type, each component is attached to a GameObject, so moving a new pool member will also move anything else that's attached to it. This is something to be careful about, but can also be very useful in some circumstances. + +Several built-in pool types are available: + +* **ParticlePool** for ParticleSystem components. +* **AudioPool** for AudioSource components. +* **TransformPool** for Transform components (useful for just about anything). + +It's also easy to build your own pool types with just a few lines of code. Check out the existing pools to see just how simple those files are. -
ParticlePool
-
Pool to manage ParticleSystem components.
+It's easy to manage other components, including your own custom components, by creating a new class inheriting from `ComponentPool`. -
AudioPool
-
Pool to manage AudioSource components.
-
TransformPool
-
Every GameObject has a transform, making this pool extremely versatile.
-
+Miscellaneous modules +---- -It's easy to manage other components, including your own custom components, by creating a new class inheriting from `ComponentPool`. +### MouseManager +MouseManager is a standalone class which attempts to manage mouse clicking *and* touch input together. It won't catch every elaborate case, but it usually does the trick for simple arrangements involving pointing, clicking, dragging, and so on. + +This object assumes that your scene has at least two cameras: one main world camera, one UI camera. You can have others. + +The class keeps track of information over multiple frames, including information about whether the mouse position hits anything in the world or UI space, what was being pointed at the last time the mouse was clicked, and so on. + +As far as this class is concerned, clicking the left mouse button begins touching the screen, and releasing the button ends the touch. Multiple touches are not processed or handled. -Loading module ----- + +### Loading module Simple loading screen support, similar to [this blog post](http://chicounity3d.wordpress.com/2014/01/25/loading-screen-tutorial/). +First, create a level that *is* a splash screen. Second, show that level between scenes. This module makes it easy and quick to get rolling with a basic setup. -TweenCam module ----- + //loads the next scene, using default scene "Loading" as splash screen + LevelLoader.Load("Menu"); + + //loads the next scene, using specified scene as splash screen + LevelLoader.Load("Menu", "LoadingScene"); + +You can build any loading screen that you want. Or, you can use an empty scene which contains only the built-in `SimpleLoadUI` component; that script will show a simple text message using OnGUI calls. + + +### TweenCam module TweenCam tracks its state with two points in space: where is the camera, and what is it looking at? You can provide your own functions to provide that state, once per frame, and cleanly tween between output from two such functions. For advanced use, TweenCam also allows you to control the camera's FOV/size and "up" direction. -UI module ----- +### Audio module -This is a work-in-progress module, an attempt to create a bare-bones UI system. +The **MusicPlayer** script can be used to quickly convert an AudioSource to a music source (ignores audio effects, falloff distance, listener volume, etc.) +The **MusicLooper** script does the same, but also loops the audio. If you call `DontDestroyOnLoad` for this object, it could be used to play music throughout your entire game. -Extensions module ----- + +### Movers module + +The movers module contains some very simple movement scripts. Attach one or more scripts to a GameObject to cause movement once per frame: + +* **Spinner** rotates the object once per frame. +* **Bouncer** makes the object bounce up and down over time. +* **Facer** makes the object look at another object each frame. + +The movers can be customized, for example by setting a new axis of rotation, using local or world space, changing the speed or magnitude of movement, and so on. + + +### Extensions module This module provides a few [extension methods](http://www.third-helix.com/2013/09/adding-to-unitys-built-in-classes-using-extension-methods/) to make your life easier. From e96b4a87e3390b6e1147828e62f8f888228ab9eb Mon Sep 17 00:00:00 2001 From: Robert Utter Date: Tue, 3 Mar 2015 22:57:43 -0800 Subject: [PATCH 21/21] Release date for v0.4 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77569d4..e593024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ Changelog for ChicoPlugins ==== -v0.4 (Not Released) +v0.4 (March 3, 2015) ---- * New: "Movers" module. Quick components to make objects spin, bounce, or face targets.