Skip to content

Commit

Permalink
chore(Menu): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam1337 committed Sep 27, 2021
1 parent 6d9df47 commit b643ed4
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class Example_UGUI : MonoBehaviour
private void Start()
{
DN.Render = new DNUGUIRender(NotifyCanvas, NotifyPrefab);
//DM.Render = new DMUGUIRender(NotifyCanvas, NotifyPrefab);


DM.Add("Simple Notice", action => DN.Notify("Simple notification"), order: 0);

DM.Add("Show Long Notice", action => DN.Notify(_longContext, "Long Notice Example", 15f), order: 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ GameObject:
- component: {fileID: 1763411915928636253}
- component: {fileID: 1763411915928636240}
- component: {fileID: 1763411915928636243}
- component: {fileID: 5913086800711110863}
- component: {fileID: 724420846467639584}
m_Layer: 5
m_Name: extDebug Notifications UGUI
m_Name: extDebug Notifications UGUI.Text Item
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down Expand Up @@ -198,6 +199,18 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_HorizontalFit: 2
m_VerticalFit: 2
--- !u!225 &5913086800711110863
CanvasGroup:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1763411915928636255}
m_Enabled: 1
m_Alpha: 1
m_Interactable: 1
m_BlocksRaycasts: 1
m_IgnoreParentGroups: 0
--- !u!114 &724420846467639584
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -210,6 +223,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: abed5df4b94e4213afa8bc39d5cadf03, type: 3}
m_Name:
m_EditorClassIdentifier:
Transform: {fileID: 1763411915928636252}
Label_Text: {fileID: 1763411915754868505}
Label_TextMeshPro: {fileID: 0}
_rectTransform: {fileID: 1763411915928636252}
_canvasGroup: {fileID: 5913086800711110863}
_label_Text: {fileID: 1763411915754868505}
_label_TextMeshPro: {fileID: 0}
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ GameObject:
- component: {fileID: 1763411915928636253}
- component: {fileID: 1763411915928636240}
- component: {fileID: 1763411915928636243}
- component: {fileID: 6614829701641159971}
- component: {fileID: 724420846467639584}
m_Layer: 5
m_Name: extDebug Notifications UGUI
m_Name: extDebug Notifications UGUI.TextMeshPro Item
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
Expand Down Expand Up @@ -255,6 +256,18 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_HorizontalFit: 2
m_VerticalFit: 2
--- !u!225 &6614829701641159971
CanvasGroup:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1763411915928636255}
m_Enabled: 1
m_Alpha: 1
m_Interactable: 1
m_BlocksRaycasts: 1
m_IgnoreParentGroups: 0
--- !u!114 &724420846467639584
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -267,6 +280,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: abed5df4b94e4213afa8bc39d5cadf03, type: 3}
m_Name:
m_EditorClassIdentifier:
Transform: {fileID: 1763411915928636252}
Label_Text: {fileID: 0}
Label_TextMeshPro: {fileID: 6723097552036980231}
_rectTransform: {fileID: 1763411915928636252}
_canvasGroup: {fileID: 6614829701641159971}
_label_Text: {fileID: 0}
_label_TextMeshPro: {fileID: 6723097552036980231}
2 changes: 1 addition & 1 deletion Assets/extDebug/Scripts/Hooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public static class Hooks

static Hooks() => UnityEngine.Object.DontDestroyOnLoad(new GameObject("extDebug Hooks", typeof(HooksBehaviour)));

#endregion
#endregion
}
}
52 changes: 40 additions & 12 deletions Assets/extDebug/Scripts/Notifications/DNUGUIItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;

using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

using TextMeshPro = TMPro.TextMeshProUGUI;
Expand All @@ -13,28 +14,55 @@ public class DNUGUIItem : MonoBehaviour
{
#region Public Vars

public RectTransform Transform;
public string Text
{
get => _label_TextMeshPro != null ? _label_TextMeshPro.text : _label_Text.text;
set
{
if (_label_TextMeshPro != null)
_label_TextMeshPro.text = value;
else
_label_Text.text = value;
}
}

public Vector2 Position
{
get => _rectTransform.anchoredPosition;
set => _rectTransform.anchoredPosition = value;
}

public Vector2 Size => _rectTransform.sizeDelta;

public Text Label_Text;
public float Width => _label_TextMeshPro != null ? _label_TextMeshPro.GetPreferredValues().x : _label_Text.preferredWidth;

public TextMeshPro Label_TextMeshPro;
public float Alpha
{
get => _canvasGroup.alpha;
set => _canvasGroup.alpha = value;
}

[NonSerialized]
public Vector2 Velocity;

#endregion

#region Public Methods
#region Private Vars

public void SetText(string text)
{
if (Label_TextMeshPro != null)
Label_TextMeshPro.text = text;
else
Label_Text.text = text;
}
[FormerlySerializedAs("Transform")]
[SerializeField]
private RectTransform _rectTransform;

[SerializeField]
private CanvasGroup _canvasGroup;

[FormerlySerializedAs("Label_Text")]
[SerializeField]
private Text _label_Text;

public float GetWidth() => Label_TextMeshPro != null ? Label_TextMeshPro.GetPreferredValues().x : Label_Text.preferredWidth;
[FormerlySerializedAs("Label_TextMeshPro")]
[SerializeField]
private TextMeshPro _label_TextMeshPro;

#endregion
}
Expand Down
40 changes: 19 additions & 21 deletions Assets/extDebug/Scripts/Notifications/DNUGUIRender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,25 @@ public DNUGUIRender(Canvas noticeCanvas, DNUGUIItem noticePrefab)

void IDNRender.SetupNotice(DNNotice notice, float currentHeight)
{
var noticeInstance = Object.Instantiate(_noticePrefab, _noticeCanvas.transform);
noticeInstance.SetText(notice.Text);

var size = noticeInstance.GetWidth();
noticeInstance.Transform.anchoredPosition = new Vector2(size, -ItemsOffset.y - currentHeight - (noticeInstance.Transform.sizeDelta.y + 4));
notice.Data = noticeInstance;
var noticeData = Object.Instantiate(_noticePrefab, _noticeCanvas.transform);
noticeData.Text = notice.Text;
noticeData.Position = new Vector2(noticeData.Width, -ItemsOffset.y - currentHeight - (noticeData.Size.y + 4));
notice.Data = noticeData;
}

void IDNRender.RemoveNotice(DNNotice notice)
{
if (notice.Data is DNUGUIItem noticeInstance)
Object.Destroy(noticeInstance.gameObject);
if (notice.Data is DNUGUIItem noticeData)
Object.Destroy(noticeData.gameObject);
}

void IDNRender.Repaint(DNNotice notice, float timeLeft, ref float currentHeight)
{
if (!(notice.Data is DNUGUIItem noticeInstance))
if (!(notice.Data is DNUGUIItem noticeData))
return;

var size = noticeInstance.Transform.sizeDelta;
var position = noticeInstance.Transform.anchoredPosition;
var size = noticeData.Size;
var position = noticeData.Position;

var width = size.x;
var height = size.y + ItemSpace;
Expand All @@ -70,24 +68,24 @@ void IDNRender.Repaint(DNNotice notice, float timeLeft, ref float currentHeight)

// Calculate new positions
var speed = Time.unscaledDeltaTime * 15;
position.x += noticeInstance.Velocity.x * speed;
position.y += noticeInstance.Velocity.y * speed;
position.x += noticeData.Velocity.x * speed;
position.y += noticeData.Velocity.y * speed;

// Calculate speed
var distance = targetX - position.x;
noticeInstance.Velocity.x += distance * speed * 1;
if (Mathf.Abs(distance) < 2 && Mathf.Abs(noticeInstance.Velocity.x) < 0.1) noticeInstance.Velocity.x = 0;
noticeData.Velocity.x += distance * speed * 1;
if (Mathf.Abs(distance) < 2 && Mathf.Abs(noticeData.Velocity.x) < 0.1) noticeData.Velocity.x = 0;

distance = targetY - position.y;
noticeInstance.Velocity.y += distance * speed * 1;
if (Mathf.Abs(distance) < 2 && Mathf.Abs(noticeInstance.Velocity.y) < 0.1) noticeInstance.Velocity.y = 0;
noticeData.Velocity.y += distance * speed * 1;
if (Mathf.Abs(distance) < 2 && Mathf.Abs(noticeData.Velocity.y) < 0.1) noticeData.Velocity.y = 0;

var friction = 0.95f - Time.unscaledDeltaTime * 8;
noticeInstance.Velocity.x *= friction;
noticeInstance.Velocity.y *= friction;
noticeData.Velocity.x *= friction;
noticeData.Velocity.y *= friction;

noticeInstance.Transform.anchoredPosition = position;
noticeInstance.SetText(notice.Text);
noticeData.Position = position;
noticeData.Text = notice.Text;

currentHeight -= height;
}
Expand Down

0 comments on commit b643ed4

Please sign in to comment.