converting on-screen messages to uGUI

This commit is contained in:
in0finite 2020-06-06 18:09:26 +02:00
parent 78319b501b
commit 394348745e
7 changed files with 189 additions and 51 deletions

View file

@ -295,4 +295,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 1c72eda03771ae74aa128f3c7218283a, type: 3}
m_Name:
m_EditorClassIdentifier:
m_drawMessages: 1
messagePrefab: {fileID: 2694704631655002565, guid: 6d322e231f3455d48956d7437520a7c9,
type: 3}
messagesContainer: {fileID: 0}
messagePoolSize: 10

View file

@ -0,0 +1,81 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &2694704631655002565
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8122135491475348229}
- component: {fileID: 6596440728574097929}
- component: {fileID: 7210735960633946770}
m_Layer: 0
m_Name: OnScreenMessage
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8122135491475348229
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2694704631655002565}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 160, y: 30}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &6596440728574097929
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2694704631655002565}
m_CullTransparentMesh: 0
--- !u!114 &7210735960633946770
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2694704631655002565}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 708705254, guid: f70555f144d8491a825f0804e09c671c, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 0
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_TypeName: UnityEngine.UI.MaskableGraphic+CullStateChangedEvent, UnityEngine.UI,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 17
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 1
m_MaxSize: 40
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: New Text

View file

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 6d322e231f3455d48956d7437520a7c9
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -7055,6 +7055,41 @@ MonoBehaviour:
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text:
--- !u!1 &6707687495432811311
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8743615454504171472}
m_Layer: 0
m_Name: OnScreenMessagesContainer
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8743615454504171472
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6707687495432811311}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 7060217674643716524}
m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!1 &6708427117946774985
GameObject:
m_ObjectHideFlags: 0
@ -7630,6 +7665,7 @@ RectTransform:
- {fileID: 7544529063945659150}
- {fileID: 2222193836290576247}
- {fileID: 122172316996072528}
- {fileID: 8743615454504171472}
m_Father: {fileID: 5719983600610341389}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

View file

@ -126,14 +126,12 @@ namespace SanAndreasUnity.Behaviours
public void DisplayInflictedDamageMessage(float damageAmount)
{
OnScreenMessageManager.Instance.AddMessage(
new OnScreenMessage
{
velocity = Random.insideUnitCircle.normalized * PedManager.Instance.inflictedDamageMessageVelocityInScreenPerc,
color = PedManager.Instance.inflictedDamageMessageColor,
timeLeft = PedManager.Instance.inflictedDamageMessageLifetime,
text = damageAmount.ToString(),
});
var msg = OnScreenMessageManager.Instance.CreateMessage();
msg.velocity = Random.insideUnitCircle.normalized * PedManager.Instance.inflictedDamageMessageVelocityInScreenPerc;
msg.TextColor = PedManager.Instance.inflictedDamageMessageColor;
msg.timeLeft = PedManager.Instance.inflictedDamageMessageLifetime;
msg.Text = damageAmount.ToString();
}
}

View file

@ -1,21 +1,51 @@
using UnityEngine;
using UnityEngine.UI;
namespace SanAndreasUnity.Utilities
{
public class OnScreenMessage
public class OnScreenMessage : MonoBehaviour
{
public string text = "";
public Color color = Color.black;
public Color backgroundColor = new Color(0, 0, 0, 1); // transparent
public Text TextComponent { get; private set; }
public string Text { get => this.TextComponent.text; set => this.TextComponent.text = value; }
public Color TextColor { get => this.TextComponent.color; set => this.TextComponent.color = value; }
//public Color backgroundColor = new Color(0, 0, 0, 1); // transparent
/// in percentage of screen dimensions
public Vector2 screenPos = Vector2.one / 2f;
public Vector2 ScreenPos
{
get => Vector2.Scale(new Vector2(this.transform.localPosition.x, this.transform.localPosition.y), new Vector2(1.0f / Screen.width, 1.0f / Screen.height));
set
{
(this.transform as RectTransform).localPosition = new Vector3(value.x * Screen.width, value.y * Screen.height, 0f);
}
}
/// in percentage of screen dimensions
public Vector2 sizeOnScreen = new Vector2(80 / 1280f, 30 / 720f);
public Vector2 SizeOnScreen
{
get => Vector2.Scale((this.transform as RectTransform).sizeDelta, new Vector2(1.0f / Screen.width, 1.0f / Screen.height));
set
{
(this.transform as RectTransform).sizeDelta = new Vector2(value.x * Screen.width, value.y * Screen.height);
}
}
/// in percentage of screen dimensions
public Vector2 velocity = Vector2.zero;
/// how much time it is displayed
public float timeLeft = 2;
void Awake()
{
this.TextComponent = this.GetComponentInChildren<Text>();
}
}
}

View file

@ -7,11 +7,14 @@ namespace SanAndreasUnity.Utilities
public class OnScreenMessageManager : MonoBehaviour
{
public GameObject messagePrefab;
public RectTransform messagesContainer;
private List<OnScreenMessage> m_onScreenMessages = new List<OnScreenMessage>();
public IReadOnlyList<OnScreenMessage> Messages => m_onScreenMessages;
[SerializeField] private bool m_drawMessages = true;
public static bool DrawMessages { get { return Instance.m_drawMessages; } set { Instance.m_drawMessages = value; } }
public int messagePoolSize = 10;
public static OnScreenMessageManager Instance { get; private set; }
@ -25,52 +28,32 @@ namespace SanAndreasUnity.Utilities
void Update()
{
m_onScreenMessages.RemoveDeadObjects();
foreach (var msg in m_onScreenMessages)
{
msg.timeLeft -= Time.deltaTime;
msg.screenPos += msg.velocity * Time.deltaTime;
msg.ScreenPos += msg.velocity * Time.deltaTime;
if (msg.timeLeft <= 0)
{
Object.Destroy(msg);
}
}
m_onScreenMessages.RemoveAll(msg => msg.timeLeft <= 0);
}
void OnGUI()
public OnScreenMessage CreateMessage()
{
// draw messages
messagePrefab.GetComponentOrThrow<OnScreenMessage>();
if (!m_drawMessages)
return;
GameObject messageGo = Object.Instantiate(messagePrefab);
messageGo.transform.SetParent(messagesContainer, false);
var originalColor = GUI.color;
var originalBackgroundColor = GUI.backgroundColor;
OnScreenMessage message = messageGo.GetComponent<OnScreenMessage>();
Vector2 screenSize = new Vector2(Screen.width, Screen.height);
m_onScreenMessages.Add(message);
foreach (var msg in m_onScreenMessages)
{
GUI.color = msg.color;
GUI.backgroundColor = msg.backgroundColor;
Vector2 size = Utilities.GUIUtils.CalcScreenSizeForContent(new GUIContent(msg.text), GUI.skin.label);
GUI.Label(new Rect(Vector2.Scale(msg.screenPos, screenSize), size), msg.text);
}
GUI.color = originalColor;
GUI.backgroundColor = originalBackgroundColor;
}
public void AddMessage(OnScreenMessage msg)
{
m_onScreenMessages.Add(msg);
}
public void RemoveMessage(OnScreenMessage msg)
{
m_onScreenMessages.Remove(msg);
return message;
}
}