Skip to content

Commit

Permalink
479 label display errors (#480)
Browse files Browse the repository at this point in the history
* Rework labels activation logic

* Recreate LODGroup in DisplayObjectData.PlaceTexts()
  • Loading branch information
Cedrok authored Mar 15, 2024
1 parent dd1cf21 commit 9eb6ff5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 41 deletions.
47 changes: 33 additions & 14 deletions Assets/Prefabs/LabeledBox.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4254767957273672101}
serializedVersion: 2
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_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 8422964209624112029}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!33 &2756453064248392137
MeshFilter:
Expand All @@ -52,6 +53,7 @@ MeshRenderer:
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
Expand Down Expand Up @@ -90,9 +92,17 @@ BoxCollider:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4254767957273672101}
m_Material: {fileID: 0}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_LayerOverridePriority: 0
m_IsTrigger: 0
m_ProvidesContacts: 0
m_Enabled: 1
serializedVersion: 2
serializedVersion: 3
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!1 &6010235731161253863
Expand Down Expand Up @@ -122,13 +132,14 @@ Transform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6010235731161253863}
serializedVersion: 2
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_ConstrainProportionsScale: 0
m_Children:
- {fileID: 4176928236324298618}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &226685182499933010
MonoBehaviour:
Expand Down Expand Up @@ -161,22 +172,14 @@ LODGroup:
m_GameObject: {fileID: 6010235731161253863}
serializedVersion: 2
m_LocalReferencePoint: {x: 0, y: 1.1175, z: 0.001}
m_Size: 0.48
m_Size: 1
m_FadeMode: 0
m_AnimateCrossFading: 0
m_LastLODIsBillboard: 0
m_LODs:
- screenRelativeHeight: 0.10489535
fadeTransitionWidth: 0
renderers:
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
renderers: []
m_Enabled: 1
--- !u!54 &7641173002511634591
Rigidbody:
Expand All @@ -185,10 +188,21 @@ Rigidbody:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 6010235731161253863}
serializedVersion: 2
serializedVersion: 4
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}
m_IncludeLayers:
serializedVersion: 2
m_Bits: 0
m_ExcludeLayers:
serializedVersion: 2
m_Bits: 0
m_ImplicitCom: 1
m_ImplicitTensor: 1
m_UseGravity: 0
m_IsKinematic: 1
m_Interpolate: 0
Expand All @@ -206,3 +220,8 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 11599482bb3741f4b890d546eeba923f, type: 3}
m_Name:
m_EditorClassIdentifier:
isTemplate: 0
scatterPlotOfOneParent: 0
isHidden: 0
isHiddenInGroup: 0
isHighlighted: 0
8 changes: 2 additions & 6 deletions Assets/Prefabs/Rack.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ LODGroup:
m_GameObject: {fileID: 382823270839367371}
serializedVersion: 2
m_LocalReferencePoint: {x: 0, y: 1.1175, z: 0.001}
m_Size: 0.48
m_Size: 1
m_FadeMode: 0
m_AnimateCrossFading: 0
m_LastLODIsBillboard: 0
m_LODs:
- screenRelativeHeight: 0.10099162
fadeTransitionWidth: 0
renderers:
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
- renderer: {fileID: 0}
renderers: []
m_Enabled: 1
--- !u!54 &-3966503025729716914
Rigidbody:
Expand Down
57 changes: 36 additions & 21 deletions Assets/Scripts/DisplayObjectData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class DisplayObjectData : MonoBehaviour
private ELabelMode currentLabelMode;
private Vector3 boxSize;
private Item item;
private LODGroup lodGroup;

private void Start()
{
Expand Down Expand Up @@ -53,6 +54,7 @@ public void PlaceTexts(string _labelPos)
{
usedLabels.Clear();
item = GetComponent<Item>();
lodGroup = GetComponent<LODGroup>();
Transform shape = transform.GetChild(0);

if (item && item.attributes.ContainsKey("template") && !string.IsNullOrEmpty(item.attributes["template"]))
Expand Down Expand Up @@ -168,11 +170,17 @@ public void PlaceTexts(string _labelPos)
usedLabels.Add(labelBottom);
break;
}
List<Renderer> renderers = new();
foreach (TextMeshPro tmp in usedLabels)
{
tmp.gameObject.SetActive(true);
tmp.margin = new Vector4(tmp.rectTransform.sizeDelta.x, 0, tmp.rectTransform.sizeDelta.x, 0) / 20;
renderers.Add(tmp.GetComponent<Renderer>());
}
LOD withLabels = new(0.1f, renderers.ToArray());
LOD withoutLabels = new(0, new Renderer[0]);
lodGroup.SetLODs(new LOD[] { withLabels, withoutLabels });
lodGroup.RecalculateBounds();
}

///<summary>
Expand Down Expand Up @@ -224,22 +232,22 @@ private void WriteLabels(string _str)
floatingLabel.text = $"<b>{floatingLabel.text}</b>";
if (isItalic)
floatingLabel.text = $"<i>{floatingLabel.text}</i>";
floatingLabel.transform.GetChild(0).GetComponent<Renderer>().material.color = Utils.ParseHtmlColor("#" + backgroundColor);
floatingLabel.transform.GetChild(0).GetComponent<Renderer>().material.color = Utils.ParseHtmlColor($"#{backgroundColor}");
}
}

///<summary>
/// Display or hide labels.
/// Activate or deactivate labels depending on given <paramref name="_labelType"/>
///</summary>
///<param name="_value">The value to assign</param>
public void SwitchLabel(ELabelMode _value)
///<param name="_labelType">The ELabelMode to use</param>
public void SwitchLabel(ELabelMode _labelType)
{
switch (_value)
switch (_labelType)
{
case ELabelMode.Default:
GetComponent<LODGroup>().enabled = true;
foreach (TextMeshPro tmp in usedLabels)
tmp.GetComponent<MeshRenderer>().enabled = true;
tmp.gameObject.SetActive(true);
if (hasFloatingLabel)
floatingLabel.gameObject.SetActive(false);
break;
Expand All @@ -248,48 +256,55 @@ public void SwitchLabel(ELabelMode _value)
if (hasFloatingLabel)
{
foreach (TextMeshPro tmp in usedLabels)
tmp.GetComponent<MeshRenderer>().enabled = false;
tmp.gameObject.SetActive(false);
floatingLabel.gameObject.SetActive(true);
}
break;
case ELabelMode.Hidden:
GetComponent<LODGroup>().enabled = true;
foreach (TextMeshPro tmp in usedLabels)
tmp.GetComponent<MeshRenderer>().enabled = false;
tmp.gameObject.SetActive(false);
if (hasFloatingLabel)
floatingLabel.gameObject.SetActive(false);
break;
case ELabelMode.Forced:
GetComponent<LODGroup>().enabled = false;
foreach (TextMeshPro tmp in usedLabels)
tmp.GetComponent<MeshRenderer>().enabled = true;
tmp.gameObject.SetActive(true);
if (hasFloatingLabel)
floatingLabel.gameObject.SetActive(false);
break;
default: break;
}
currentLabelMode = _value;
currentLabelMode = _labelType;
}

/// <summary>
/// Toggle all labels MeshRenderer
/// </summary>
/// <param name="_value"></param>
public void ToggleLabel(bool _value)
{
if (currentLabelMode == ELabelMode.Default || currentLabelMode == ELabelMode.Forced || !hasFloatingLabel)
foreach (TextMeshPro tmp in usedLabels)
tmp.GetComponent<MeshRenderer>().enabled = _value;
else if (currentLabelMode == ELabelMode.FloatingOnTop)
floatingLabel.gameObject.SetActive(_value);
foreach (TextMeshPro tmp in usedLabels)
tmp.GetComponent<MeshRenderer>().enabled = _value;
if (hasFloatingLabel)
{
floatingLabel.GetComponent<MeshRenderer>().enabled = _value;
floatingLabel.transform.GetChild(0).GetComponent<MeshRenderer>().enabled = _value;
}
}

/// <summary>
/// Called when receiving a SwitchLavelEvent, call SwitchLabel with given <see cref="ELabelMode"/>.
/// </summary>
/// <param name="_e"></param>
private void OnSwitchLabelEvent(SwitchLabelEvent _e)
{
// Ignore slots and opened groups
if (GetComponent<Slot>() || (item is Group group && !group.isDisplayed))
// Ignore slots
if (GetComponent<Slot>())
return;

if ((item && item.referent == item && !GameManager.instance.GetSelected().Contains(gameObject)) ||
GameManager.instance.GetSelected().Contains(transform.parent.gameObject) ||
GameManager.instance.GetFocused().Contains(transform.parent.gameObject))
SwitchLabel(_e.value);
SwitchLabel(_e.value);
}

///<summary>
Expand Down
21 changes: 21 additions & 0 deletions ProjectSettings/LocalizationSettings.asset
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &1
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: aa32dd7bcb4d749abb23199c1f0f51af, type: 3}
m_Name:
m_EditorClassIdentifier:
m_StringTable:
m_TableReference:
m_TableCollectionName:
m_AssetTable:
m_TableReference:
m_TableCollectionName:
m_TrackingChanges: 0

0 comments on commit 9eb6ff5

Please sign in to comment.