import successful, grid has visuals

cell values and grid lines are displayed. no interaction available yet.
This commit is contained in:
Simon O'Shea
2023-07-31 18:45:01 -04:00
parent 4699ce7287
commit 5f9e8f979d
56 changed files with 4851 additions and 7 deletions
@@ -0,0 +1,212 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
//#define SOUND_MANAGER // Has Sound_Manager in project
//#define CURSOR_MANAGER // Has Cursor_Manager in project
using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.EventSystems;
namespace CodeMonkey.Utils {
/*
* Button Actions on a World BoxCollider
* */
public class Button_Sprite : MonoBehaviour {
private static Func<Camera> GetWorldCamera;
public static void SetGetWorldCamera(Func<Camera> GetWorldCamera) {
Button_Sprite.GetWorldCamera = GetWorldCamera;
}
public Action ClickFunc = null;
public Action MouseRightDownOnceFunc = null;
public Action MouseRightDownFunc = null;
public Action MouseRightUpFunc = null;
public Action MouseDownOnceFunc = null;
public Action MouseUpOnceFunc = null;
public Action MouseOverOnceFunc = null;
public Action MouseOutOnceFunc = null;
public Action MouseOverOnceTooltipFunc = null;
public Action MouseOutOnceTooltipFunc = null;
private bool draggingMouseRight;
private Vector3 mouseRightDragStart;
public Action<Vector3, Vector3> MouseRightDragFunc = null;
public Action<Vector3, Vector3> MouseRightDragUpdateFunc = null;
public bool triggerMouseRightDragOnEnter = false;
public enum HoverBehaviour {
Custom,
Change_Color,
Change_Image,
Change_SetActive,
}
public HoverBehaviour hoverBehaviourType = HoverBehaviour.Custom;
private Action hoverBehaviourFunc_Enter, hoverBehaviourFunc_Exit;
public Color hoverBehaviour_Color_Enter = new Color(1, 1, 1, 1), hoverBehaviour_Color_Exit = new Color(1, 1, 1, 1);
public SpriteRenderer hoverBehaviour_Image;
public Sprite hoverBehaviour_Sprite_Exit, hoverBehaviour_Sprite_Enter;
public bool hoverBehaviour_Move = false;
public Vector2 hoverBehaviour_Move_Amount = Vector2.zero;
private Vector3 posExit, posEnter;
public bool triggerMouseOutFuncOnClick = false;
public bool clickThroughUI = false;
private Action internalOnMouseDownFunc = null, internalOnMouseEnterFunc = null, internalOnMouseExitFunc = null;
#if SOUND_MANAGER
public Sound_Manager.Sound mouseOverSound, mouseClickSound;
#endif
#if CURSOR_MANAGER
public CursorManager.CursorType cursorMouseOver, cursorMouseOut;
#endif
public void SetHoverBehaviourChangeColor(Color colorOver, Color colorOut) {
hoverBehaviourType = HoverBehaviour.Change_Color;
hoverBehaviour_Color_Enter = colorOver;
hoverBehaviour_Color_Exit = colorOut;
if (hoverBehaviour_Image == null) hoverBehaviour_Image = transform.GetComponent<SpriteRenderer>();
hoverBehaviour_Image.color = hoverBehaviour_Color_Exit;
SetupHoverBehaviour();
}
void OnMouseDown() {
if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
if (internalOnMouseDownFunc != null) internalOnMouseDownFunc();
if (ClickFunc != null) ClickFunc();
if (MouseDownOnceFunc != null) MouseDownOnceFunc();
if (triggerMouseOutFuncOnClick) OnMouseExit();
}
public void Manual_OnMouseExit() {
OnMouseExit();
}
void OnMouseUp() {
if (MouseUpOnceFunc != null) MouseUpOnceFunc();
}
void OnMouseEnter() {
if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
if (internalOnMouseEnterFunc != null) internalOnMouseEnterFunc();
if (hoverBehaviour_Move) transform.localPosition = posEnter;
if (hoverBehaviourFunc_Enter != null) hoverBehaviourFunc_Enter();
if (MouseOverOnceFunc != null) MouseOverOnceFunc();
if (MouseOverOnceTooltipFunc != null) MouseOverOnceTooltipFunc();
}
void OnMouseExit() {
if (internalOnMouseExitFunc != null) internalOnMouseExitFunc();
if (hoverBehaviour_Move) transform.localPosition = posExit;
if (hoverBehaviourFunc_Exit != null) hoverBehaviourFunc_Exit();
if (MouseOutOnceFunc != null) MouseOutOnceFunc();
if (MouseOutOnceTooltipFunc != null) MouseOutOnceTooltipFunc();
}
void OnMouseOver() {
if (!clickThroughUI && IsPointerOverUI()) return; // Over UI!
if (Input.GetMouseButton(1)) {
if (MouseRightDownFunc != null) MouseRightDownFunc();
if (!draggingMouseRight && triggerMouseRightDragOnEnter) {
draggingMouseRight = true;
mouseRightDragStart = GetWorldPositionFromUI();
}
}
if (Input.GetMouseButtonDown(1)) {
draggingMouseRight = true;
mouseRightDragStart = GetWorldPositionFromUI();
if (MouseRightDownOnceFunc != null) MouseRightDownOnceFunc();
}
}
void Update() {
if (draggingMouseRight) {
if (MouseRightDragUpdateFunc != null) MouseRightDragUpdateFunc(mouseRightDragStart, GetWorldPositionFromUI());
}
if (Input.GetMouseButtonUp(1)) {
if (draggingMouseRight) {
draggingMouseRight = false;
if (MouseRightDragFunc != null) MouseRightDragFunc(mouseRightDragStart, GetWorldPositionFromUI());
}
if (MouseRightUpFunc != null) MouseRightUpFunc();
}
}
void Awake() {
if (GetWorldCamera == null) SetGetWorldCamera(() => Camera.main); // Set default World Camera
posExit = transform.localPosition;
posEnter = transform.localPosition + (Vector3)hoverBehaviour_Move_Amount;
SetupHoverBehaviour();
#if SOUND_MANAGER
// Sound Manager
internalOnMouseDownFunc += () => { if (mouseClickSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseClickSound); };
internalOnMouseEnterFunc += () => { if (mouseOverSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseOverSound); };
#endif
#if CURSOR_MANAGER
// Cursor Manager
internalOnMouseExitFunc += () => { if (cursorMouseOut != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOut); };
internalOnMouseEnterFunc += () => { if (cursorMouseOver != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOver); };
#endif
}
private void SetupHoverBehaviour() {
switch (hoverBehaviourType) {
case HoverBehaviour.Change_Color:
hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Enter; };
hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Exit; };
break;
case HoverBehaviour.Change_Image:
hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Enter; };
hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Exit; };
break;
case HoverBehaviour.Change_SetActive:
hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.gameObject.SetActive(true); };
hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.gameObject.SetActive(false); };
break;
}
}
private static Vector3 GetWorldPositionFromUI() {
Vector3 worldPosition = GetWorldCamera().ScreenToWorldPoint(Input.mousePosition);
return worldPosition;
}
private static bool IsPointerOverUI() {
if (EventSystem.current.IsPointerOverGameObject()) {
return true;
} else {
PointerEventData pe = new PointerEventData(EventSystem.current);
pe.position = Input.mousePosition;
List<RaycastResult> hits = new List<RaycastResult>();
EventSystem.current.RaycastAll(pe, hits);
return hits.Count > 0;
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 4771330f098d1db4898ad07204154d04
timeCreated: 1513255758
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,228 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
//#define SOUND_MANAGER // Has Sound_Manager in project
//#define CURSOR_MANAGER // Has Cursor_Manager in project
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace CodeMonkey.Utils {
/*
* Button in the UI
* */
public class Button_UI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler {
public Action ClickFunc = null;
public Action MouseRightClickFunc = null;
public Action MouseMiddleClickFunc = null;
public Action MouseDownOnceFunc = null;
public Action MouseUpFunc = null;
public Action MouseOverOnceTooltipFunc = null;
public Action MouseOutOnceTooltipFunc = null;
public Action MouseOverOnceFunc = null;
public Action MouseOutOnceFunc = null;
public Action MouseOverFunc = null;
public Action MouseOverPerSecFunc = null; //Triggers every sec if mouseOver
public Action MouseUpdate = null;
public Action<PointerEventData> OnPointerClickFunc;
public enum HoverBehaviour {
Custom,
Change_Color,
Change_Color_Auto,
Change_Image,
Change_SetActive,
}
public HoverBehaviour hoverBehaviourType = HoverBehaviour.Custom;
private Action hoverBehaviourFunc_Enter, hoverBehaviourFunc_Exit;
public Color hoverBehaviour_Color_Enter, hoverBehaviour_Color_Exit;
public Image hoverBehaviour_Image;
public Sprite hoverBehaviour_Sprite_Exit, hoverBehaviour_Sprite_Enter;
public bool hoverBehaviour_Move = false;
public Vector2 hoverBehaviour_Move_Amount = Vector2.zero;
private Vector2 posExit, posEnter;
public bool triggerMouseOutFuncOnClick = false;
private bool mouseOver;
private float mouseOverPerSecFuncTimer;
private Action internalOnPointerEnterFunc = null, internalOnPointerExitFunc = null, internalOnPointerClickFunc = null;
#if SOUND_MANAGER
public Sound_Manager.Sound mouseOverSound, mouseClickSound;
#endif
#if CURSOR_MANAGER
public CursorManager.CursorType cursorMouseOver, cursorMouseOut;
#endif
public virtual void OnPointerEnter(PointerEventData eventData) {
if (internalOnPointerEnterFunc != null) internalOnPointerEnterFunc();
if (hoverBehaviour_Move) transform.GetComponent<RectTransform>().anchoredPosition = posEnter;
if (hoverBehaviourFunc_Enter != null) hoverBehaviourFunc_Enter();
if (MouseOverOnceFunc != null) MouseOverOnceFunc();
if (MouseOverOnceTooltipFunc != null) MouseOverOnceTooltipFunc();
mouseOver = true;
mouseOverPerSecFuncTimer = 0f;
}
public virtual void OnPointerExit(PointerEventData eventData) {
if (internalOnPointerExitFunc != null) internalOnPointerExitFunc();
if (hoverBehaviour_Move) transform.GetComponent<RectTransform>().anchoredPosition = posExit;
if (hoverBehaviourFunc_Exit != null) hoverBehaviourFunc_Exit();
if (MouseOutOnceFunc != null) MouseOutOnceFunc();
if (MouseOutOnceTooltipFunc != null) MouseOutOnceTooltipFunc();
mouseOver = false;
}
public virtual void OnPointerClick(PointerEventData eventData) {
if (internalOnPointerClickFunc != null) internalOnPointerClickFunc();
if (OnPointerClickFunc != null) OnPointerClickFunc(eventData);
if (eventData.button == PointerEventData.InputButton.Left) {
if (triggerMouseOutFuncOnClick) {
OnPointerExit(eventData);
}
if (ClickFunc != null) ClickFunc();
}
if (eventData.button == PointerEventData.InputButton.Right)
if (MouseRightClickFunc != null) MouseRightClickFunc();
if (eventData.button == PointerEventData.InputButton.Middle)
if (MouseMiddleClickFunc != null) MouseMiddleClickFunc();
}
public void Manual_OnPointerExit() {
OnPointerExit(null);
}
public bool IsMouseOver() {
return mouseOver;
}
public void OnPointerDown(PointerEventData eventData) {
if (MouseDownOnceFunc != null) MouseDownOnceFunc();
}
public void OnPointerUp(PointerEventData eventData) {
if (MouseUpFunc != null) MouseUpFunc();
}
private void Update() {
if (mouseOver) {
if (MouseOverFunc != null) MouseOverFunc();
mouseOverPerSecFuncTimer -= Time.unscaledDeltaTime;
if (mouseOverPerSecFuncTimer <= 0) {
mouseOverPerSecFuncTimer += 1f;
if (MouseOverPerSecFunc != null) MouseOverPerSecFunc();
}
}
if (MouseUpdate != null) MouseUpdate();
}
private void Awake() {
posExit = transform.GetComponent<RectTransform>().anchoredPosition;
posEnter = transform.GetComponent<RectTransform>().anchoredPosition + hoverBehaviour_Move_Amount;
SetHoverBehaviourType(hoverBehaviourType);
#if SOUND_MANAGER
// Sound Manager
internalOnPointerEnterFunc += () => { if (mouseOverSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseOverSound); };
internalOnPointerClickFunc += () => { if (mouseClickSound != Sound_Manager.Sound.None) Sound_Manager.PlaySound(mouseClickSound); };
#endif
#if CURSOR_MANAGER
// Cursor Manager
internalOnPointerEnterFunc += () => { if (cursorMouseOver != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOver); };
internalOnPointerExitFunc += () => { if (cursorMouseOut != CursorManager.CursorType.None) CursorManager.SetCursor(cursorMouseOut); };
#endif
}
public void SetHoverBehaviourType(HoverBehaviour hoverBehaviourType) {
this.hoverBehaviourType = hoverBehaviourType;
switch (hoverBehaviourType) {
case HoverBehaviour.Change_Color:
hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Enter; };
hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = hoverBehaviour_Color_Exit; };
break;
case HoverBehaviour.Change_Image:
hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Enter; };
hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.sprite = hoverBehaviour_Sprite_Exit; };
break;
case HoverBehaviour.Change_SetActive:
hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.gameObject.SetActive(true); };
hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.gameObject.SetActive(false); };
break;
case HoverBehaviour.Change_Color_Auto:
Color color = hoverBehaviour_Image.color;
if (color.r >= 1f) color.r = .9f;
if (color.g >= 1f) color.g = .9f;
if (color.b >= 1f) color.b = .9f;
Color colorOver = color * 1.3f; // Over color lighter
hoverBehaviourFunc_Enter = delegate () { hoverBehaviour_Image.color = colorOver; };
hoverBehaviourFunc_Exit = delegate () { hoverBehaviour_Image.color = color; };
break;
}
}
public void RefreshHoverBehaviourType() {
SetHoverBehaviourType(hoverBehaviourType);
}
/*
* Class for temporarily intercepting a button action
* Useful for Tutorial disabling specific buttons
* */
public class InterceptActionHandler {
private Action removeInterceptFunc;
public InterceptActionHandler(Action removeInterceptFunc) {
this.removeInterceptFunc = removeInterceptFunc;
}
public void RemoveIntercept() {
removeInterceptFunc();
}
}
public InterceptActionHandler InterceptActionClick(Func<bool> testPassthroughFunc) {
return InterceptAction("ClickFunc", testPassthroughFunc);
}
public InterceptActionHandler InterceptAction(string fieldName, Func<bool> testPassthroughFunc) {
return InterceptAction(GetType().GetField(fieldName), testPassthroughFunc);
}
public InterceptActionHandler InterceptAction(System.Reflection.FieldInfo fieldInfo, Func<bool> testPassthroughFunc) {
Action backFunc = fieldInfo.GetValue(this) as Action;
InterceptActionHandler interceptActionHandler = new InterceptActionHandler(() => fieldInfo.SetValue(this, backFunc));
fieldInfo.SetValue(this, (Action)delegate () {
if (testPassthroughFunc()) {
// Passthrough
interceptActionHandler.RemoveIntercept();
backFunc();
}
});
return interceptActionHandler;
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 602f74cbbbd80034b96f88091a8e7c1d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,187 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System;
using System.Collections.Generic;
using UnityEngine;
namespace CodeMonkey.Utils {
/*
* Executes a Function periodically
* */
public class FunctionPeriodic {
/*
* Class to hook Actions into MonoBehaviour
* */
private class MonoBehaviourHook : MonoBehaviour {
public Action OnUpdate;
private void Update() {
if (OnUpdate != null) OnUpdate();
}
}
private static List<FunctionPeriodic> funcList; // Holds a reference to all active timers
private static GameObject initGameObject; // Global game object used for initializing class, is destroyed on scene change
private static void InitIfNeeded() {
if (initGameObject == null) {
initGameObject = new GameObject("FunctionPeriodic_Global");
funcList = new List<FunctionPeriodic>();
}
}
// Persist through scene loads
public static FunctionPeriodic Create_Global(Action action, Func<bool> testDestroy, float timer) {
FunctionPeriodic functionPeriodic = Create(action, testDestroy, timer, "", false, false, false);
MonoBehaviour.DontDestroyOnLoad(functionPeriodic.gameObject);
return functionPeriodic;
}
// Trigger [action] every [timer], execute [testDestroy] after triggering action, destroy if returns true
public static FunctionPeriodic Create(Action action, Func<bool> testDestroy, float timer) {
return Create(action, testDestroy, timer, "", false);
}
public static FunctionPeriodic Create(Action action, float timer) {
return Create(action, null, timer, "", false, false, false);
}
public static FunctionPeriodic Create(Action action, float timer, string functionName) {
return Create(action, null, timer, functionName, false, false, false);
}
public static FunctionPeriodic Create(Action callback, Func<bool> testDestroy, float timer, string functionName, bool stopAllWithSameName) {
return Create(callback, testDestroy, timer, functionName, false, false, stopAllWithSameName);
}
public static FunctionPeriodic Create(Action action, Func<bool> testDestroy, float timer, string functionName, bool useUnscaledDeltaTime, bool triggerImmediately, bool stopAllWithSameName) {
InitIfNeeded();
if (stopAllWithSameName) {
StopAllFunc(functionName);
}
GameObject gameObject = new GameObject("FunctionPeriodic Object " + functionName, typeof(MonoBehaviourHook));
FunctionPeriodic functionPeriodic = new FunctionPeriodic(gameObject, action, timer, testDestroy, functionName, useUnscaledDeltaTime);
gameObject.GetComponent<MonoBehaviourHook>().OnUpdate = functionPeriodic.Update;
funcList.Add(functionPeriodic);
if (triggerImmediately) action();
return functionPeriodic;
}
public static void RemoveTimer(FunctionPeriodic funcTimer) {
InitIfNeeded();
funcList.Remove(funcTimer);
}
public static void StopTimer(string _name) {
InitIfNeeded();
for (int i = 0; i < funcList.Count; i++) {
if (funcList[i].functionName == _name) {
funcList[i].DestroySelf();
return;
}
}
}
public static void StopAllFunc(string _name) {
InitIfNeeded();
for (int i = 0; i < funcList.Count; i++) {
if (funcList[i].functionName == _name) {
funcList[i].DestroySelf();
i--;
}
}
}
public static bool IsFuncActive(string name) {
InitIfNeeded();
for (int i = 0; i < funcList.Count; i++) {
if (funcList[i].functionName == name) {
return true;
}
}
return false;
}
private GameObject gameObject;
private float timer;
private float baseTimer;
private bool useUnscaledDeltaTime;
private string functionName;
public Action action;
public Func<bool> testDestroy;
private FunctionPeriodic(GameObject gameObject, Action action, float timer, Func<bool> testDestroy, string functionName, bool useUnscaledDeltaTime) {
this.gameObject = gameObject;
this.action = action;
this.timer = timer;
this.testDestroy = testDestroy;
this.functionName = functionName;
this.useUnscaledDeltaTime = useUnscaledDeltaTime;
baseTimer = timer;
}
public void SkipTimerTo(float timer) {
this.timer = timer;
}
public void SetBaseTimer(float baseTimer) {
this.baseTimer = baseTimer;
}
public float GetBaseTimer() {
return baseTimer;
}
private void Update() {
if (useUnscaledDeltaTime) {
timer -= Time.unscaledDeltaTime;
} else {
timer -= Time.deltaTime;
}
if (timer <= 0) {
action();
if (testDestroy != null && testDestroy()) {
//Destroy
DestroySelf();
} else {
//Repeat
timer += baseTimer;
}
}
}
public void DestroySelf() {
RemoveTimer(this);
if (gameObject != null) {
UnityEngine.Object.Destroy(gameObject);
}
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f522bd6f08a5c984a9512bb60ebc1818
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,183 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System;
using UnityEngine;
using System.Collections.Generic;
namespace CodeMonkey.Utils {
/*
* Triggers a Action after a certain time
* */
public class FunctionTimer {
/*
* Class to hook Actions into MonoBehaviour
* */
private class MonoBehaviourHook : MonoBehaviour {
public Action OnUpdate;
private void Update() {
if (OnUpdate != null) OnUpdate();
}
}
private static List<FunctionTimer> timerList; // Holds a reference to all active timers
private static GameObject initGameObject; // Global game object used for initializing class, is destroyed on scene change
private static void InitIfNeeded() {
if (initGameObject == null) {
initGameObject = new GameObject("FunctionTimer_Global");
timerList = new List<FunctionTimer>();
}
}
public static FunctionTimer Create(Action action, float timer) {
return Create(action, timer, "", false, false);
}
public static FunctionTimer Create(Action action, float timer, string functionName) {
return Create(action, timer, functionName, false, false);
}
public static FunctionTimer Create(Action action, float timer, string functionName, bool useUnscaledDeltaTime) {
return Create(action, timer, functionName, useUnscaledDeltaTime, false);
}
public static FunctionTimer Create(Action action, float timer, string functionName, bool useUnscaledDeltaTime, bool stopAllWithSameName) {
InitIfNeeded();
if (stopAllWithSameName) {
StopAllTimersWithName(functionName);
}
GameObject obj = new GameObject("FunctionTimer Object "+functionName, typeof(MonoBehaviourHook));
FunctionTimer funcTimer = new FunctionTimer(obj, action, timer, functionName, useUnscaledDeltaTime);
obj.GetComponent<MonoBehaviourHook>().OnUpdate = funcTimer.Update;
timerList.Add(funcTimer);
return funcTimer;
}
public static void RemoveTimer(FunctionTimer funcTimer) {
InitIfNeeded();
timerList.Remove(funcTimer);
}
public static void StopAllTimersWithName(string functionName) {
InitIfNeeded();
for (int i = 0; i < timerList.Count; i++) {
if (timerList[i].functionName == functionName) {
timerList[i].DestroySelf();
i--;
}
}
}
public static void StopFirstTimerWithName(string functionName) {
InitIfNeeded();
for (int i = 0; i < timerList.Count; i++) {
if (timerList[i].functionName == functionName) {
timerList[i].DestroySelf();
return;
}
}
}
private GameObject gameObject;
private float timer;
private string functionName;
private bool active;
private bool useUnscaledDeltaTime;
private Action action;
public FunctionTimer(GameObject gameObject, Action action, float timer, string functionName, bool useUnscaledDeltaTime) {
this.gameObject = gameObject;
this.action = action;
this.timer = timer;
this.functionName = functionName;
this.useUnscaledDeltaTime = useUnscaledDeltaTime;
}
private void Update() {
if (useUnscaledDeltaTime) {
timer -= Time.unscaledDeltaTime;
} else {
timer -= Time.deltaTime;
}
if (timer <= 0) {
// Timer complete, trigger Action
action();
DestroySelf();
}
}
private void DestroySelf() {
RemoveTimer(this);
if (gameObject != null) {
UnityEngine.Object.Destroy(gameObject);
}
}
/*
* Class to trigger Actions manually without creating a GameObject
* */
public class FunctionTimerObject {
private float timer;
private Action callback;
public FunctionTimerObject(Action callback, float timer) {
this.callback = callback;
this.timer = timer;
}
public bool Update() {
return Update(Time.deltaTime);
}
public bool Update(float deltaTime) {
timer -= deltaTime;
if (timer <= 0) {
callback();
return true;
} else {
return false;
}
}
}
// Create a Object that must be manually updated through Update();
public static FunctionTimerObject CreateObject(Action callback, float timer) {
return new FunctionTimerObject(callback, timer);
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9c8b5f0217e73fa47827cf77f22c2f8d
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,157 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System;
using UnityEngine;
using System.Collections.Generic;
namespace CodeMonkey.Utils {
/*
* Calls function on every Update until it returns true
* */
public class FunctionUpdater {
/*
* Class to hook Actions into MonoBehaviour
* */
private class MonoBehaviourHook : MonoBehaviour {
public Action OnUpdate;
private void Update() {
if (OnUpdate != null) OnUpdate();
}
}
private static List<FunctionUpdater> updaterList; // Holds a reference to all active updaters
private static GameObject initGameObject; // Global game object used for initializing class, is destroyed on scene change
private static void InitIfNeeded() {
if (initGameObject == null) {
initGameObject = new GameObject("FunctionUpdater_Global");
updaterList = new List<FunctionUpdater>();
}
}
public static FunctionUpdater Create(Action updateFunc) {
return Create(() => { updateFunc(); return false; }, "", true, false);
}
public static FunctionUpdater Create(Action updateFunc, string functionName) {
return Create(() => { updateFunc(); return false; }, functionName, true, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc) {
return Create(updateFunc, "", true, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc, string functionName) {
return Create(updateFunc, functionName, true, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active) {
return Create(updateFunc, functionName, active, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active, bool stopAllWithSameName) {
InitIfNeeded();
if (stopAllWithSameName) {
StopAllUpdatersWithName(functionName);
}
GameObject gameObject = new GameObject("FunctionUpdater Object " + functionName, typeof(MonoBehaviourHook));
FunctionUpdater functionUpdater = new FunctionUpdater(gameObject, updateFunc, functionName, active);
gameObject.GetComponent<MonoBehaviourHook>().OnUpdate = functionUpdater.Update;
updaterList.Add(functionUpdater);
return functionUpdater;
}
private static void RemoveUpdater(FunctionUpdater funcUpdater) {
InitIfNeeded();
updaterList.Remove(funcUpdater);
}
public static void DestroyUpdater(FunctionUpdater funcUpdater) {
InitIfNeeded();
if (funcUpdater != null) {
funcUpdater.DestroySelf();
}
}
public static void StopUpdaterWithName(string functionName) {
InitIfNeeded();
for (int i = 0; i < updaterList.Count; i++) {
if (updaterList[i].functionName == functionName) {
updaterList[i].DestroySelf();
return;
}
}
}
public static void StopAllUpdatersWithName(string functionName) {
InitIfNeeded();
for (int i = 0; i < updaterList.Count; i++) {
if (updaterList[i].functionName == functionName) {
updaterList[i].DestroySelf();
i--;
}
}
}
private GameObject gameObject;
private string functionName;
private bool active;
private Func<bool> updateFunc; // Destroy Updater if return true;
public FunctionUpdater(GameObject gameObject, Func<bool> updateFunc, string functionName, bool active) {
this.gameObject = gameObject;
this.updateFunc = updateFunc;
this.functionName = functionName;
this.active = active;
}
public void Pause() {
active = false;
}
public void Resume() {
active = true;
}
private void Update() {
if (!active) return;
if (updateFunc()) {
DestroySelf();
}
}
public void DestroySelf() {
RemoveUpdater(this);
if (gameObject != null) {
UnityEngine.Object.Destroy(gameObject);
}
}
}
}
@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7eacfbf662ad34e46932f36084c15e11
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
@@ -0,0 +1,170 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System;
using UnityEngine;
using UnityEngine.UI;
namespace CodeMonkey.Utils {
/*
* Bar in the UI with scaleable Bar and Background
* */
public class UI_Bar {
public GameObject gameObject;
private RectTransform rectTransform;
private RectTransform background;
private RectTransform bar;
private Vector2 size;
/*
* Outline into for Bar
* */
public class Outline {
public float size = 1f;
public Color color = Color.black;
public Outline(float size, Color color) {
this.size = size;
this.color = color;
}
}
public UI_Bar(GameObject gameObject, RectTransform background, RectTransform bar) {
this.gameObject = gameObject;
rectTransform = gameObject.GetComponent<RectTransform>();
this.background = background;
this.bar = bar;
size = background.GetComponent<RectTransform>().sizeDelta;
}
public UI_Bar(GameObject gameObject, Transform background, Transform bar) {
this.gameObject = gameObject;
rectTransform = gameObject.GetComponent<RectTransform>();
this.background = background.GetComponent<RectTransform>();
this.bar = bar.GetComponent<RectTransform>();
size = background.GetComponent<RectTransform>().sizeDelta;
}
public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio) {
SetupParent(parent, anchoredPosition, size);
SetupBar(barColor);
SetSize(sizeRatio);
}
public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color barColor, float sizeRatio, Outline outline) {
SetupParent(parent, anchoredPosition, size);
if (outline != null) SetupOutline(outline, size);
SetupBar(barColor);
SetSize(sizeRatio);
}
public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio) {
SetupParent(parent, anchoredPosition, size);
SetupBackground(backgroundColor);
SetupBar(barColor);
SetSize(sizeRatio);
}
public UI_Bar(Transform parent, Vector2 anchoredPosition, Vector2 size, Color backgroundColor, Color barColor, float sizeRatio, Outline outline) {
SetupParent(parent, anchoredPosition, size);
if (outline != null) SetupOutline(outline, size);
SetupBackground(backgroundColor);
SetupBar(barColor);
SetSize(sizeRatio);
}
private void SetupParent(Transform parent, Vector2 anchoredPosition, Vector2 size) {
this.size = size;
gameObject = new GameObject("UI_Bar", typeof(RectTransform));
rectTransform = gameObject.GetComponent<RectTransform>();
rectTransform.SetParent(parent, false);
rectTransform.sizeDelta = size;
rectTransform.anchorMin = new Vector2(0, .5f);
rectTransform.anchorMax = new Vector2(0, .5f);
rectTransform.pivot = new Vector2(0, .5f);
rectTransform.anchoredPosition = anchoredPosition;
}
private RectTransform SetupOutline(Outline outline, Vector2 size) {
return UtilsClass.DrawSprite(outline.color, gameObject.transform, Vector2.zero, size + new Vector2(outline.size, outline.size), "Outline");
}
private void SetupBackground(Color backgroundColor) {
background = UtilsClass.DrawSprite(backgroundColor, gameObject.transform, Vector2.zero, Vector2.zero, "Background");
background.anchorMin = new Vector2(0,0);
background.anchorMax = new Vector2(1,1);
}
private void SetupBar(Color barColor) {
bar = UtilsClass.DrawSprite(barColor, gameObject.transform, Vector2.zero, Vector2.zero, "Bar");
bar.anchorMin = new Vector2(0,0);
bar.anchorMax = new Vector2(0,1f);
bar.pivot = new Vector2(0,.5f);
}
public void SetSize(float sizeRatio) {
bar.sizeDelta = new Vector2(sizeRatio * size.x, 0f);
}
public void SetColor(Color color) {
bar.GetComponent<Image>().color = color;
}
public void SetActive(bool active) {
gameObject.SetActive(active);
}
public Vector2 GetAnchoredPosition() {
return rectTransform.anchoredPosition;
}
public Vector2 GetSize() {
return rectTransform.sizeDelta;
}
public void AddOutline(Outline outline) {
RectTransform outlineRectTransform = SetupOutline(outline, size);
outlineRectTransform.transform.SetAsFirstSibling();
}
public void SetRaycastTarget(bool set) {
foreach (Transform trans in gameObject.transform) {
if (trans.GetComponent<Image>() != null) {
trans.GetComponent<Image>().raycastTarget = set;
}
}
}
public void DestroySelf() {
UnityEngine.Object.Destroy(gameObject);
}
public Button_UI AddButton() {
return AddButton(null, null, null);
}
public Button_UI AddButton(Action ClickFunc, Action MouseOverOnceFunc, Action MouseOutOnceFunc) {
Button_UI buttonUI = gameObject.AddComponent<Button_UI>();
if (ClickFunc != null)
buttonUI.ClickFunc = ClickFunc;
if (MouseOverOnceFunc != null)
buttonUI.MouseOverOnceFunc = MouseOverOnceFunc;
if (MouseOutOnceFunc != null)
buttonUI.MouseOutOnceFunc = MouseOutOnceFunc;
return buttonUI;
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 49ffd25ff4b0ca3429d1b1e2dc0ff037
timeCreated: 1524300423
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,104 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace CodeMonkey.Utils {
/*
* UI Container with multiple bars, useful for displaying one bar with multiple inner bars like success chance and failure chance
* */
public class UI_BarMultiple {
private GameObject gameObject;
private RectTransform rectTransform;
private RectTransform[] barArr;
private Image[] barImageArr;
private Vector2 size;
public class Outline {
public float size = 1f;
public Color color = Color.black;
public Outline(float size, Color color) {
this.size = size;
this.color = color;
}
}
public UI_BarMultiple(Transform parent, Vector2 anchoredPosition, Vector2 size, Color[] barColorArr, Outline outline) {
this.size = size;
SetupParent(parent, anchoredPosition, size);
if (outline != null) SetupOutline(outline, size);
List<RectTransform> barList = new List<RectTransform>();
List<Image> barImageList = new List<Image>();
List<float> defaultSizeList = new List<float>();
foreach (Color color in barColorArr) {
barList.Add(SetupBar(color));
defaultSizeList.Add(1f / barColorArr.Length);
}
barArr = barList.ToArray();
barImageArr = barImageList.ToArray();
SetSizes(defaultSizeList.ToArray());
}
private void SetupParent(Transform parent, Vector2 anchoredPosition, Vector2 size) {
gameObject = new GameObject("UI_BarMultiple", typeof(RectTransform));
rectTransform = gameObject.GetComponent<RectTransform>();
rectTransform.SetParent(parent, false);
rectTransform.sizeDelta = size;
rectTransform.anchorMin = new Vector2(0, .5f);
rectTransform.anchorMax = new Vector2(0, .5f);
rectTransform.pivot = new Vector2(0, .5f);
rectTransform.anchoredPosition = anchoredPosition;
}
private void SetupOutline(Outline outline, Vector2 size) {
UtilsClass.DrawSprite(outline.color, gameObject.transform, Vector2.zero, size + new Vector2(outline.size, outline.size), "Outline");
}
private RectTransform SetupBar(Color barColor) {
RectTransform bar = UtilsClass.DrawSprite(barColor, gameObject.transform, Vector2.zero, Vector2.zero, "Bar");
bar.anchorMin = new Vector2(0,0);
bar.anchorMax = new Vector2(0,1f);
bar.pivot = new Vector2(0,.5f);
return bar;
}
public void SetSizes(float[] sizeArr) {
if (sizeArr.Length != barArr.Length) {
throw new System.Exception("Length doesn't match!");
}
Vector2 pos = Vector2.zero;
for (int i=0; i<sizeArr.Length; i++) {
float scaledSize = sizeArr[i] * size.x;
barArr[i].anchoredPosition = pos;
barArr[i].sizeDelta = new Vector2(scaledSize, 0f);
pos.x += scaledSize;
}
}
public RectTransform GetBar(int index) {
return barArr[index];
}
public Vector2 GetSize() {
return size;
}
public void DestroySelf() {
UnityEngine.Object.Destroy(gameObject);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 9cf7b152002fbca49b6a1a4623f200a7
timeCreated: 1524300429
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,131 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System;
using UnityEngine;
using UnityEngine.UI;
namespace CodeMonkey.Utils {
/*
* Sprite in the UI
* */
public class UI_Sprite {
private static Transform GetCanvasTransform() {
return UtilsClass.GetCanvasTransform();
}
public static UI_Sprite CreateDebugButton(Vector2 anchoredPosition, Vector2 size, Action ClickFunc) {
return CreateDebugButton(anchoredPosition, size, ClickFunc, Color.green);
}
public static UI_Sprite CreateDebugButton(Vector2 anchoredPosition, Vector2 size, Action ClickFunc, Color color) {
UI_Sprite uiSprite = new UI_Sprite(GetCanvasTransform(), Assets.i.s_White, anchoredPosition, size, color);
uiSprite.AddButton(ClickFunc, null, null);
return uiSprite;
}
public static UI_Sprite CreateDebugButton(Transform parent, Vector2 anchoredPosition, string text, Action ClickFunc) {
return CreateDebugButton(parent, anchoredPosition, text, ClickFunc, Color.green);
}
public static UI_Sprite CreateDebugButton(Vector2 anchoredPosition, string text, Action ClickFunc) {
return CreateDebugButton(anchoredPosition, text, ClickFunc, Color.green);
}
public static UI_Sprite CreateDebugButton(Transform parent, Vector2 anchoredPosition, string text, Action ClickFunc, Color color) {
return CreateDebugButton(parent, anchoredPosition, text, ClickFunc, color, new Vector2(30, 20));
}
public static UI_Sprite CreateDebugButton(Vector2 anchoredPosition, string text, Action ClickFunc, Color color) {
return CreateDebugButton(GetCanvasTransform(), anchoredPosition, text, ClickFunc, color, new Vector2(30, 20));
}
public static UI_Sprite CreateDebugButton(Transform parent, Vector2 anchoredPosition, string text, Action ClickFunc, Color color, Vector2 padding) {
UI_TextComplex uiTextComplex;
UI_Sprite uiSprite = CreateDebugButton(parent, anchoredPosition, Vector2.zero, ClickFunc, color, text, out uiTextComplex);
uiSprite.SetSize(new Vector2(uiTextComplex.GetTotalWidth(), uiTextComplex.GetTotalHeight()) + padding);
return uiSprite;
}
public static UI_Sprite CreateDebugButton(Vector2 anchoredPosition, Vector2 size, Action ClickFunc, Color color, string text) {
UI_TextComplex uiTextComplex;
return CreateDebugButton(anchoredPosition, size, ClickFunc, color, text, out uiTextComplex);
}
public static UI_Sprite CreateDebugButton(Vector2 anchoredPosition, Vector2 size, Action ClickFunc, Color color, string text, out UI_TextComplex uiTextComplex) {
return CreateDebugButton(GetCanvasTransform(), anchoredPosition, size, ClickFunc, color, text, out uiTextComplex);
}
public static UI_Sprite CreateDebugButton(Transform parent, Vector2 anchoredPosition, Vector2 size, Action ClickFunc, Color color, string text, out UI_TextComplex uiTextComplex) {
if (color.r >= 1f) color.r = .9f;
if (color.g >= 1f) color.g = .9f;
if (color.b >= 1f) color.b = .9f;
Color colorOver = color * 1.1f; // button over color lighter
UI_Sprite uiSprite = new UI_Sprite(parent, Assets.i.s_White, anchoredPosition, size, color);
uiSprite.AddButton(ClickFunc, () => uiSprite.SetColor(colorOver), () => uiSprite.SetColor(color));
uiTextComplex = new UI_TextComplex(uiSprite.gameObject.transform, Vector2.zero, 12, '#', text, null, null);
uiTextComplex.SetTextColor(Color.black);
uiTextComplex.SetAnchorMiddle();
uiTextComplex.CenterOnPosition(Vector2.zero);
return uiSprite;
}
public GameObject gameObject;
public Image image;
public RectTransform rectTransform;
public UI_Sprite(Transform parent, Sprite sprite, Vector2 anchoredPosition, Vector2 size, Color color) {
rectTransform = UtilsClass.DrawSprite(sprite, parent, anchoredPosition, size, "UI_Sprite");
gameObject = rectTransform.gameObject;
image = gameObject.GetComponent<Image>();
image.color = color;
}
public void SetColor(Color color) {
image.color = color;
}
public void SetSprite(Sprite sprite) {
image.sprite = sprite;
}
public void SetSize(Vector2 size) {
rectTransform.sizeDelta = size;
}
public void SetAnchoredPosition(Vector2 anchoredPosition) {
rectTransform.anchoredPosition = anchoredPosition;
}
public Button_UI AddButton(Action ClickFunc, Action MouseOverOnceFunc, Action MouseOutOnceFunc) {
Button_UI buttonUI = gameObject.AddComponent<Button_UI>();
if (ClickFunc != null)
buttonUI.ClickFunc = ClickFunc;
if (MouseOverOnceFunc != null)
buttonUI.MouseOverOnceFunc = MouseOverOnceFunc;
if (MouseOutOnceFunc != null)
buttonUI.MouseOutOnceFunc = MouseOutOnceFunc;
return buttonUI;
}
public void DestroySelf() {
UnityEngine.Object.Destroy(gameObject);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: d0052e8fbe8cbcd4d8c48038d364333b
timeCreated: 1524300216
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,151 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using UnityEngine;
using UnityEngine.UI;
namespace CodeMonkey.Utils {
/*
* Displays text with icons in between the text
* */
public class UI_TextComplex {
private static Transform GetCanvasTransform() {
return UtilsClass.GetCanvasTransform();
}
public struct Icon {
public Sprite sprite;
public Vector2 size;
public Color color;
public Icon(Sprite sprite, Vector2 size, Color? color = null) {
this.sprite = sprite;
this.size = size;
if (color == null) {
this.color = Color.white;
} else {
this.color = (Color) color;
}
}
}
public GameObject gameObject;
private Transform transform;
private RectTransform rectTransform;
// iconChar prepends the iconArr index;
// Example using iconChar '#':
// test #0 asdf
// Displays "test [iconArr[0]] asdf"
public UI_TextComplex(Transform parent, Vector2 anchoredPosition, int fontSize, char iconChar, string text, Icon[] iconArr, Font font) {
SetupParent(parent, anchoredPosition);
string tmp = text;
float textPosition = 0f;
while (tmp.IndexOf(iconChar) != -1) {
string untilTmp = tmp.Substring(0, tmp.IndexOf(iconChar));
string iconNumber = tmp.Substring(tmp.IndexOf(iconChar)+1);
int indexOfSpaceAfterIconNumber = iconNumber.IndexOf(" ");
if (indexOfSpaceAfterIconNumber != -1) {
// Still has more space after iconNumber
iconNumber = iconNumber.Substring(0, indexOfSpaceAfterIconNumber);
} else {
// No more space after iconNumber
}
tmp = tmp.Substring(tmp.IndexOf(iconChar+iconNumber) + (iconChar+iconNumber).Length);
if (untilTmp.Trim() != "") {
Text uiText = UtilsClass.DrawTextUI(untilTmp, transform, new Vector2(textPosition,0), fontSize, font);
textPosition += uiText.preferredWidth;
}
// Draw Icon
int iconIndex = UtilsClass.Parse_Int(iconNumber, 0);
Icon icon = iconArr[iconIndex];
UtilsClass.DrawSprite(icon.sprite, transform, new Vector2(textPosition + icon.size.x / 2f, 0), icon.size);
textPosition += icon.size.x;
}
if (tmp.Trim() != "") {
UtilsClass.DrawTextUI(tmp, transform, new Vector2(textPosition,0), fontSize, font);
}
}
private void SetupParent(Transform parent, Vector2 anchoredPosition) {
gameObject = new GameObject("UI_TextComplex", typeof(RectTransform));
transform = gameObject.transform;
rectTransform = gameObject.GetComponent<RectTransform>();
rectTransform.SetParent(parent, false);
rectTransform.sizeDelta = new Vector2(0, 0);
rectTransform.anchorMin = new Vector2(0, .5f);
rectTransform.anchorMax = new Vector2(0, .5f);
rectTransform.pivot = new Vector2(0, .5f);
rectTransform.anchoredPosition = anchoredPosition;
}
public void SetTextColor(Color color) {
foreach (Transform trans in transform) {
Text text = trans.GetComponent<Text>();
if (text != null) {
text.color = color;
}
}
}
public float GetTotalWidth() {
float textPosition = 0f;
foreach (Transform trans in transform) {
Text text = trans.GetComponent<Text>();
if (text != null) {
textPosition += text.preferredWidth;
}
Image image = trans.GetComponent<Image>();
if (image != null) {
textPosition += image.GetComponent<RectTransform>().sizeDelta.x;
}
}
return textPosition;
}
public float GetTotalHeight() {
foreach (Transform trans in transform) {
Text text = trans.GetComponent<Text>();
if (text != null) {
return text.preferredHeight;
}
}
return 0f;
}
public void AddTextOutline(Color color, float size) {
foreach (Transform textComplexTrans in transform) {
if (textComplexTrans.GetComponent<Text>() != null) {
Outline outline = textComplexTrans.gameObject.AddComponent<Outline>();
outline.effectColor = color;
outline.effectDistance = new Vector2(size, size);
}
}
}
public void SetAnchorMiddle() {
rectTransform.anchorMin = new Vector2(.5f, .5f);
rectTransform.anchorMax = new Vector2(.5f, .5f);
}
public void CenterOnPosition(Vector2 position) {
rectTransform.anchoredPosition = position + new Vector2(-GetTotalWidth() / 2f, 0);
}
public void DestroySelf() {
Object.Destroy(gameObject);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 8ba4b604c0e9db241bb3d130165415cd
timeCreated: 1524300347
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ce2355f335dcae94abba806390b1bd76
timeCreated: 1524300728
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,150 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using UnityEngine;
namespace CodeMonkey.Utils {
/*
* Bar in the World, great for quickly making a health bar
* */
public class World_Bar {
private Outline outline;
private GameObject gameObject;
private Transform transform;
private Transform background;
private Transform bar;
public static int GetSortingOrder(Vector3 position, int offset, int baseSortingOrder = 5000) {
return (int)(baseSortingOrder - position.y) + offset;
}
public class Outline {
public float size = 1f;
public Color color = Color.black;
}
public static World_Bar Create(Vector3 localPosition, Vector3 localScale, Color? backgroundColor, Color barColor, float sizeRatio, Outline outline = null) {
return Create(null, localPosition, localScale, backgroundColor, barColor, sizeRatio, 0, outline);
}
public static World_Bar Create(Transform parent, Vector3 localPosition, Vector3 localScale, Color? backgroundColor, Color barColor, float sizeRatio, int sortingOrder, Outline outline = null) {
return new World_Bar(parent, localPosition, localScale, backgroundColor, barColor, sizeRatio, sortingOrder, outline);
}
public World_Bar(Transform parent, Vector3 localPosition, Vector3 localScale, Color? backgroundColor, Color barColor, float sizeRatio, int sortingOrder, Outline outline = null) {
this.outline = outline;
SetupParent(parent, localPosition);
if (outline != null) SetupOutline(outline, localScale, sortingOrder - 1);
if (backgroundColor != null) SetupBackground((Color)backgroundColor, localScale, sortingOrder);
SetupBar(barColor, localScale, sortingOrder + 1);
SetSize(sizeRatio);
}
private void SetupParent(Transform parent, Vector3 localPosition) {
gameObject = new GameObject("World_Bar");
transform = gameObject.transform;
transform.SetParent(parent);
transform.localPosition = localPosition;
}
private void SetupOutline(Outline outline, Vector3 localScale, int sortingOrder) {
UtilsClass.CreateWorldSprite(transform, "Outline", Assets.i.s_White, new Vector3(0,0), localScale + new Vector3(outline.size, outline.size), sortingOrder, outline.color);
}
private void SetupBackground(Color backgroundColor, Vector3 localScale, int sortingOrder) {
background = UtilsClass.CreateWorldSprite(transform, "Background", Assets.i.s_White, new Vector3(0,0), localScale, sortingOrder, backgroundColor).transform;
}
private void SetupBar(Color barColor, Vector3 localScale, int sortingOrder) {
GameObject barGO = new GameObject("Bar");
bar = barGO.transform;
bar.SetParent(transform);
bar.localPosition = new Vector3(-localScale.x / 2f, 0, 0);
bar.localScale = new Vector3(1,1,1);
Transform barIn = UtilsClass.CreateWorldSprite(bar, "BarIn", Assets.i.s_White, new Vector3(localScale.x / 2f, 0), localScale, sortingOrder, barColor).transform;
}
public void SetLocalScale(Vector3 localScale) {
// Outline
if (transform.Find("Outline") != null) {
// Has outline
transform.Find("Outline").localScale = localScale + new Vector3(outline.size, outline.size);
}
//Background
background.localScale = localScale;
// Set Bar Scale
bar.localPosition = new Vector3(-localScale.x / 2f, 0, 0);
Transform barIn = bar.Find("BarIn");
barIn.localScale = localScale;
barIn.localPosition = new Vector3(localScale.x / 2f, 0);
}
public void SetSortingOrder(int sortingOrder) {
bar.Find("BarIn").GetComponent<SpriteRenderer>().sortingOrder = sortingOrder;
if (background != null) background.GetComponent<SpriteRenderer>().sortingOrder = sortingOrder - 1;
if (transform.Find("Outline") != null) transform.Find("Outline").GetComponent<SpriteRenderer>().sortingOrder = sortingOrder - 2;
}
public void SetSortingLayerName(string sortingLayerName) {
bar.Find("BarIn").GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
if (background != null) background.GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
if (transform.Find("Outline") != null) transform.Find("Outline").GetComponent<SpriteRenderer>().sortingLayerName = sortingLayerName;
}
public void SetRotation(float rotation) {
transform.localEulerAngles = new Vector3(0, 0, rotation);
}
public void SetSize(float sizeRatio) {
bar.localScale = new Vector3(sizeRatio, 1, 1);
}
public void SetColor(Color color) {
bar.Find("BarIn").GetComponent<SpriteRenderer>().color = color;
}
public void SetActive(bool isActive) {
gameObject.SetActive(isActive);
}
public void Show() {
gameObject.SetActive(true);
}
public void Hide() {
gameObject.SetActive(false);
}
public Button_Sprite AddButton(System.Action ClickFunc, System.Action MouseOverOnceFunc, System.Action MouseOutOnceFunc) {
Button_Sprite buttonSprite = gameObject.AddComponent<Button_Sprite>();
if (ClickFunc != null)
buttonSprite.ClickFunc = ClickFunc;
if (MouseOverOnceFunc != null)
buttonSprite.MouseOverOnceFunc = MouseOverOnceFunc;
if (MouseOutOnceFunc != null)
buttonSprite.MouseOutOnceFunc = MouseOutOnceFunc;
return buttonSprite;
}
public void DestroySelf() {
if (gameObject != null) {
Object.Destroy(gameObject);
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: cc20fdc736e8c9442b35e4bcc04c596c
timeCreated: 1524300272
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,237 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using UnityEngine;
namespace CodeMonkey.Utils {
/*
* Mesh in the World
* */
public class World_Mesh {
private const int sortingOrderDefault = 5000;
public GameObject gameObject;
public Transform transform;
private Material material;
private Vector3[] vertices;
private Vector2[] uv;
private int[] triangles;
private Mesh mesh;
public static World_Mesh CreateEmpty(Vector3 position, float eulerZ, Material material, int sortingOrderOffset = 0) {
return new World_Mesh(null, position, Vector3.one, eulerZ, material, new Vector3[0], new Vector2[0], new int[0], sortingOrderOffset);
}
public static World_Mesh Create(Vector3 position, float eulerZ, Material material, Vector3[] vertices, Vector2[] uv, int[] triangles, int sortingOrderOffset = 0) {
return new World_Mesh(null, position, Vector3.one, eulerZ, material, vertices, uv, triangles, sortingOrderOffset);
}
public static World_Mesh Create(Vector3 position, float eulerZ, float meshWidth, float meshHeight, Material material, UVCoords uvCoords, int sortingOrderOffset = 0) {
return new World_Mesh(null, position, Vector3.one, eulerZ, meshWidth, meshHeight, material, uvCoords, sortingOrderOffset);
}
public static World_Mesh Create(Vector3 lowerLeftCorner, float width, float height, Material material, UVCoords uvCoords, int sortingOrderOffset = 0) {
return Create(lowerLeftCorner, lowerLeftCorner + new Vector3(width, height), material, uvCoords, sortingOrderOffset);
}
public static World_Mesh Create(Vector3 lowerLeftCorner, Vector3 upperRightCorner, Material material, UVCoords uvCoords, int sortingOrderOffset = 0) {
float width = upperRightCorner.x - lowerLeftCorner.x;
float height = upperRightCorner.y - lowerLeftCorner.y;
Vector3 localScale = upperRightCorner - lowerLeftCorner;
Vector3 position = lowerLeftCorner + localScale * .5f;
return new World_Mesh(null, position, Vector3.one, 0f, width, height, material, uvCoords, sortingOrderOffset);
}
private static int GetSortingOrder(Vector3 position, int offset, int baseSortingOrder = sortingOrderDefault) {
return (int)(baseSortingOrder - position.y) + offset;
}
public class UVCoords {
public int x, y, width, height;
public UVCoords(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
}
public World_Mesh(Transform parent, Vector3 localPosition, Vector3 localScale, float eulerZ, float meshWidth, float meshHeight, Material material, UVCoords uvCoords, int sortingOrderOffset) {
this.material = material;
vertices = new Vector3[4];
uv = new Vector2[4];
triangles = new int[6];
/* 0,1
* 1,1
* 0,0
* 1,0
*/
float meshWidthHalf = meshWidth / 2f;
float meshHeightHalf = meshHeight / 2f;
vertices[0] = new Vector3(-meshWidthHalf, meshHeightHalf);
vertices[1] = new Vector3( meshWidthHalf, meshHeightHalf);
vertices[2] = new Vector3(-meshWidthHalf, -meshHeightHalf);
vertices[3] = new Vector3( meshWidthHalf, -meshHeightHalf);
if (uvCoords == null) {
uvCoords = new UVCoords(0, 0, material.mainTexture.width, material.mainTexture.height);
}
Vector2[] uvArray = GetUVRectangleFromPixels(uvCoords.x, uvCoords.y, uvCoords.width, uvCoords.height, material.mainTexture.width, material.mainTexture.height);
ApplyUVToUVArray(uvArray, ref uv);
triangles[0] = 0;
triangles[1] = 1;
triangles[2] = 2;
triangles[3] = 2;
triangles[4] = 1;
triangles[5] = 3;
mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
gameObject = new GameObject("Mesh", typeof(MeshFilter), typeof(MeshRenderer));
gameObject.transform.parent = parent;
gameObject.transform.localPosition = localPosition;
gameObject.transform.localScale = localScale;
gameObject.transform.localEulerAngles = new Vector3(0, 0, eulerZ);
gameObject.GetComponent<MeshFilter>().mesh = mesh;
gameObject.GetComponent<MeshRenderer>().material = material;
transform = gameObject.transform;
SetSortingOrderOffset(sortingOrderOffset);
}
public World_Mesh(Transform parent, Vector3 localPosition, Vector3 localScale, float eulerZ, Material material, Vector3[] vertices, Vector2[] uv, int[] triangles, int sortingOrderOffset) {
this.material = material;
this.vertices = vertices;
this.uv = uv;
this.triangles = triangles;
mesh = new Mesh();
mesh.vertices = vertices;
mesh.uv = uv;
mesh.triangles = triangles;
gameObject = new GameObject("Mesh", typeof(MeshFilter), typeof(MeshRenderer));
gameObject.transform.parent = parent;
gameObject.transform.localPosition = localPosition;
gameObject.transform.localScale = localScale;
gameObject.transform.localEulerAngles = new Vector3(0, 0, eulerZ);
gameObject.GetComponent<MeshFilter>().mesh = mesh;
gameObject.GetComponent<MeshRenderer>().material = material;
transform = gameObject.transform;
SetSortingOrderOffset(sortingOrderOffset);
}
private Vector2 ConvertPixelsToUVCoordinates(int x, int y, int textureWidth, int textureHeight) {
return new Vector2((float)x / textureWidth, (float)y / textureHeight);
}
private Vector2[] GetUVRectangleFromPixels(int x, int y, int width, int height, int textureWidth, int textureHeight) {
/* 0, 1
* 1, 1
* 0, 0
* 1, 0
* */
return new Vector2[] {
ConvertPixelsToUVCoordinates(x, y + height, textureWidth, textureHeight),
ConvertPixelsToUVCoordinates(x + width, y + height, textureWidth, textureHeight),
ConvertPixelsToUVCoordinates(x, y, textureWidth, textureHeight),
ConvertPixelsToUVCoordinates(x + width, y, textureWidth, textureHeight)
};
}
private void ApplyUVToUVArray(Vector2[] uv, ref Vector2[] mainUV) {
if (uv == null || uv.Length < 4 || mainUV == null || mainUV.Length < 4) throw new System.Exception();
mainUV[0] = uv[0];
mainUV[1] = uv[1];
mainUV[2] = uv[2];
mainUV[3] = uv[3];
}
public void SetUVCoords(UVCoords uvCoords) {
Vector2[] uvArray = GetUVRectangleFromPixels(uvCoords.x, uvCoords.y, uvCoords.width, uvCoords.height, material.mainTexture.width, material.mainTexture.height);
ApplyUVToUVArray(uvArray, ref uv);
mesh.uv = uv;
}
public void SetSortingOrderOffset(int sortingOrderOffset) {
SetSortingOrder(GetSortingOrder(gameObject.transform.position, sortingOrderOffset));
}
public void SetSortingOrder(int sortingOrder) {
gameObject.GetComponent<Renderer>().sortingOrder = sortingOrder;
}
public void SetLocalScale(Vector3 localScale) {
transform.localScale = localScale;
}
public void SetPosition(Vector3 localPosition) {
transform.localPosition = localPosition;
}
public void AddPosition(Vector3 addPosition) {
transform.localPosition += addPosition;
}
public Vector3 GetPosition() {
return transform.localPosition;
}
public int GetSortingOrder() {
return gameObject.GetComponent<Renderer>().sortingOrder;
}
public Mesh GetMesh() {
return mesh;
}
public void Show() {
gameObject.SetActive(true);
}
public void Hide() {
gameObject.SetActive(false);
}
public void DestroySelf() {
Object.Destroy(gameObject);
}
public static void CreateMesh() {
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 91ce8dcaef210db41a183b4558f16bb2
timeCreated: 1524300280
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,225 @@
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System;
using UnityEngine;
namespace CodeMonkey.Utils {
/*
* Sprite in the World
* */
public class World_Sprite {
private const int sortingOrderDefault = 5000;
public GameObject gameObject;
public Transform transform;
private SpriteRenderer spriteRenderer;
public static World_Sprite CreateDebugButton(Vector3 position, System.Action ClickFunc) {
World_Sprite worldSprite = new World_Sprite(null, position, new Vector3(10, 10), Assets.i.s_White, Color.green, sortingOrderDefault);
worldSprite.AddButton(ClickFunc, null, null);
return worldSprite;
}
public static World_Sprite CreateDebugButton(Transform parent, Vector3 localPosition, System.Action ClickFunc) {
World_Sprite worldSprite = new World_Sprite(parent, localPosition, new Vector3(10, 10), Assets.i.s_White, Color.green, sortingOrderDefault);
worldSprite.AddButton(ClickFunc, null, null);
return worldSprite;
}
public static World_Sprite CreateDebugButton(Transform parent, Vector3 localPosition, string text, System.Action ClickFunc, int fontSize = 30, float paddingX = 5, float paddingY = 5) {
GameObject gameObject = new GameObject("DebugButton");
gameObject.transform.parent = parent;
gameObject.transform.localPosition = localPosition;
TextMesh textMesh = UtilsClass.CreateWorldText(text, gameObject.transform, Vector3.zero, fontSize, Color.white, TextAnchor.MiddleCenter, TextAlignment.Center, 20000);
Bounds rendererBounds = textMesh.GetComponent<MeshRenderer>().bounds;
Color color = UtilsClass.GetColorFromString("00BA00FF");
if (color.r >= 1f) color.r = .9f;
if (color.g >= 1f) color.g = .9f;
if (color.b >= 1f) color.b = .9f;
Color colorOver = color * 1.1f; // button over color lighter
World_Sprite worldSprite = new World_Sprite(gameObject.transform, Vector3.zero, rendererBounds.size + new Vector3(paddingX, paddingY), Assets.i.s_White, color, sortingOrderDefault);
worldSprite.AddButton(ClickFunc, () => worldSprite.SetColor(colorOver), () => worldSprite.SetColor(color));
return worldSprite;
}
public static World_Sprite Create(Transform parent, Vector3 localPosition, Vector3 localScale, Sprite sprite, Color color, int sortingOrderOffset) {
return new World_Sprite(parent, localPosition, localScale, sprite, color, sortingOrderOffset);
}
public static World_Sprite Create(Transform parent, Vector3 localPosition, Vector3 localScale, Sprite sprite = null, Color? color = null, int sortingOrderOffset = 0) {
if (sprite == null) {
sprite = Assets.i.s_White;
}
if (color == null) {
color = Color.white;
}
return new World_Sprite(parent, localPosition, localScale, sprite, (Color)color, sortingOrderOffset);
}
public static World_Sprite Create(Vector3 worldPosition, Sprite sprite) {
return new World_Sprite(null, worldPosition, new Vector3(1, 1, 1), sprite, Color.white, 0);
}
public static World_Sprite Create(Vector3 worldPosition, Vector3 localScale, Sprite sprite, Color color, int sortingOrderOffset) {
return new World_Sprite(null, worldPosition, localScale, sprite, color, sortingOrderOffset);
}
public static World_Sprite Create(Vector3 worldPosition, Vector3 localScale, Sprite sprite, Color color) {
return new World_Sprite(null, worldPosition, localScale, sprite, color, 0);
}
public static World_Sprite Create(Vector3 worldPosition, Vector3 localScale, Color color) {
return new World_Sprite(null, worldPosition, localScale, Assets.i.s_White, color, 0);
}
public static World_Sprite Create(Vector3 worldPosition, Vector3 localScale, Color color, int sortingOrderOffset) {
return new World_Sprite(null, worldPosition, localScale, Assets.i.s_White, color, sortingOrderOffset);
}
public static World_Sprite Create(Vector3 worldPosition, Vector3 localScale) {
return new World_Sprite(null, worldPosition, localScale, Assets.i.s_White, Color.white, 0);
}
public static World_Sprite Create(Vector3 worldPosition, Vector3 localScale, int sortingOrderOffset) {
return new World_Sprite(null, worldPosition, localScale, Assets.i.s_White, Color.white, sortingOrderOffset);
}
public static int GetSortingOrder(Vector3 position, int offset, int baseSortingOrder = sortingOrderDefault) {
return (int)(baseSortingOrder - position.y) + offset;
}
public World_Sprite(Transform parent, Vector3 localPosition, Vector3 localScale, Sprite sprite, Color color, int sortingOrderOffset) {
Vector3 position = localPosition;
if (parent != null) {
position = parent.TransformPoint(position);
}
int sortingOrder = GetSortingOrder(position, sortingOrderOffset);
gameObject = UtilsClass.CreateWorldSprite(parent, "Sprite", sprite, localPosition, localScale, sortingOrder, color);
transform = gameObject.transform;
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
}
public void SetActive(bool isActive) {
gameObject.SetActive(isActive);
}
public SpriteRenderer GetSpriteRenderer() {
return spriteRenderer;
}
public void SetName(string name) {
gameObject.name = name;
}
public void SetSortingOrderOffset(int sortingOrderOffset) {
SetSortingOrder(GetSortingOrder(gameObject.transform.position, sortingOrderOffset));
}
public void SetSortingOrder(int sortingOrder) {
gameObject.GetComponent<SpriteRenderer>().sortingOrder = sortingOrder;
}
public void SetSortingLayerName(string layerName) {
gameObject.GetComponent<SpriteRenderer>().sortingLayerName = layerName;
}
public int GetSortingOrder() {
return gameObject.GetComponent<SpriteRenderer>().sortingOrder;
}
public void SetParent(GameObject parentGameObject) {
SetParent(parentGameObject.transform);
}
public void SetParent(Transform parent) {
transform.parent = parent;
}
public void SetLocalScale(Vector3 localScale) {
transform.localScale = localScale;
}
public void SetPosition(Vector3 localPosition) {
transform.localPosition = localPosition;
}
public void SetPosition(Func<Vector3> getLocalPosition) {
FunctionUpdater.Create(() => {
transform.localPosition = getLocalPosition();
});
}
public Vector3 GetPosition() {
return transform.position;
}
public Vector3 GetLocalScale() {
return transform.localScale;
}
public float GetRotation() {
return transform.localEulerAngles.z;
}
public void SetRotation(float rotation) {
transform.localEulerAngles = new Vector3(0, 0, rotation);
}
public void SetColor(Color color) {
spriteRenderer.color = color;
}
public void SetSprite(Sprite sprite) {
spriteRenderer.sprite = sprite;
}
public void SetFlip(bool flipX, bool flipY) {
spriteRenderer.flipX = flipX;
spriteRenderer.flipY = flipY;
}
public void Show() {
gameObject.SetActive(true);
}
public void Hide() {
gameObject.SetActive(false);
}
public Button_Sprite AddButton(System.Action ClickFunc, System.Action MouseOverOnceFunc, System.Action MouseOutOnceFunc) {
gameObject.AddComponent<BoxCollider2D>();
Button_Sprite buttonSprite = gameObject.AddComponent<Button_Sprite>();
if (ClickFunc != null)
buttonSprite.ClickFunc = ClickFunc;
if (MouseOverOnceFunc != null)
buttonSprite.MouseOverOnceFunc = MouseOverOnceFunc;
if (MouseOutOnceFunc != null)
buttonSprite.MouseOutOnceFunc = MouseOutOnceFunc;
return buttonSprite;
}
public void DestroySelf() {
UnityEngine.Object.Destroy(gameObject);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 1460472c3006fb44daea959dda2c4d81
timeCreated: 1524300280
licenseType: Pro
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: