Hi, I’m making a VR app for Quest 2 and 3, I have a WorldSpace canvas where a script raises points as if they were the points of a sonar, there is an axis drawn on a texture and points are drawn dynamically loaded from a CSV file in that canvas, giving play in the editor on the computer are displayed perfectly.
When I compile the app in Oculus according to an internal debug that I created the points are created, they load the image but I don’t see them in the canvas, and I don’t understand why, it’s as if I drew them somewhere else, but I emptied the scene, I left only the canvas and the camera, but the points are not visible anywhere.
I don’t understand why, if someone needs more data, see the script or something similar tell me and I’ll post it. Thank you.
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System.Collections.Generic;
using System.IO;
public class CSVPointRenderer_Der : MonoBehaviour
{
public RectTransform canvasRectTransform;
public GameObject pointPrefab;
public string csvFileName = "Campo_Der.csv";
public TextMeshProUGUI[] quadrantCounters;
public TextMeshProUGUI debugText;
private class PointData
{
public int Quadrant { get; set; }
public Vector2 Position { get; set; }
public bool IsValidated { get; set; }
}
private int[] totalPointsPerQuadrant = new int[4];
private int[] validPointsPerQuadrant = new int[4];
void Start()
{
if (canvasRectTransform == null || pointPrefab == null)
{
Debug.LogError("Canvas RectTransform or Point Prefab is not assigned.");
if (debugText != null) debugText.text = "Canvas RectTransform or Point Prefab is not assigned.";
return;
}
if (quadrantCounters.Length != 4)
{
Debug.LogError("Assign all 4 TextMeshProUGUI objects for quadrant counters.");
if (debugText != null) debugText.text = "Assign all 4 TextMeshProUGUI objects for quadrant counters.";
return;
}
string csvFilePath = Path.Combine(Application.persistentDataPath, csvFileName);
List<PointData> pointDataList = LoadCSVData(csvFilePath);
RenderPoints(pointDataList);
UpdateQuadrantCounters();
}
private List<PointData> LoadCSVData(string filePath)
{
List<PointData> pointDataList = new List<PointData>();
if (File.Exists(filePath))
{
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
string[] values = line.Split('|');
if (values.Length == 4)
{
int quadrant = int.Parse(values[0]);
float x = float.Parse(values[1].Replace(',', '.'));
float y = float.Parse(values[2].Replace(',', '.'));
bool isValidated = bool.Parse(values[3]);
pointDataList.Add(new PointData
{
Quadrant = quadrant,
Position = new Vector2(x, y),
IsValidated = isValidated
});
totalPointsPerQuadrant[quadrant]++;
if (isValidated)
{
validPointsPerQuadrant[quadrant]++;
}
}
else
{
Debug.LogError($"Invalid line format: {line}");
if (debugText != null) debugText.text += $"\nInvalid line format: {line}";
}
}
}
else
{
Debug.LogError("CSV file not found at: " + filePath);
if (debugText != null) debugText.text = "CSV file not found at: " + filePath;
}
return pointDataList;
}
private void RenderPoints(List<PointData> pointDataList)
{
foreach (PointData pointData in pointDataList)
{
CreatePoint(pointData);
}
}
private void CreatePoint(PointData pointData)
{
if (pointPrefab == null)
{
Debug.LogError("Point prefab is not assigned.");
if (debugText != null) debugText.text += "\nPoint prefab is not assigned.";
return;
}
GameObject point = Instantiate(pointPrefab, canvasRectTransform);
if (point == null)
{
Debug.LogError("Failed to instantiate point prefab.");
if (debugText != null) debugText.text += "\nFailed to instantiate point prefab.";
return;
}
RectTransform pointRectTransform = point.GetComponent<RectTransform>();
if (pointRectTransform == null)
{
Debug.LogError("Point prefab does not have a RectTransform component.");
if (debugText != null) debugText.text += "\nPoint prefab does not have a RectTransform component.";
return;
}
pointRectTransform.localPosition = GetPositionInQuadrant(pointData.Quadrant, pointData.Position);
Image pointImage = point.GetComponent<Image>();
if (pointImage != null)
{
pointImage.color = pointData.IsValidated ? Color.green : Color.red;
if (pointImage.sprite == null)
{
Debug.LogError("Point prefab does not have a sprite assigned to the Image component.");
if (debugText != null) debugText.text += "\nPoint prefab does not have a sprite assigned to the Image component.";
}
else
{
Debug.Log($"Point created in quadrant {pointData.Quadrant} at position {pointRectTransform.localPosition} with sprite {pointImage.sprite.name}");
if (debugText != null) debugText.text += $"\nPoint created in quadrant {pointData.Quadrant} at position {pointRectTransform.localPosition} with sprite {pointImage.sprite.name}";
}
}
else
{
Debug.LogError("Point prefab does not have an Image component.");
if (debugText != null) debugText.text += "\nPoint prefab does not have an Image component.";
}
}
private Vector2 GetPositionInQuadrant(int quadrant, Vector2 position)
{
Vector2 canvasCenter = canvasRectTransform.rect.center;
Vector2 adjustedPosition = position;
switch (quadrant)
{
case 0:
adjustedPosition.x = canvasCenter.x + Mathf.Abs(position.x);
adjustedPosition.y = canvasCenter.y + Mathf.Abs(position.y);
break;
case 1:
adjustedPosition.x = canvasCenter.x - Mathf.Abs(position.x);
adjustedPosition.y = canvasCenter.y + Mathf.Abs(position.y);
break;
case 2:
adjustedPosition.x = canvasCenter.x - Mathf.Abs(position.x);
adjustedPosition.y = canvasCenter.y - Mathf.Abs(position.y);
break;
case 3:
adjustedPosition.x = canvasCenter.x + Mathf.Abs(position.x);
adjustedPosition.y = canvasCenter.y - Mathf.Abs(position.y);
break;
default:
Debug.LogError("Invalid quadrant: " + quadrant);
if (debugText != null) debugText.text += "\nInvalid quadrant: " + quadrant;
break;
}
return adjustedPosition;
}
private void UpdateQuadrantCounters()
{
for (int i = 0; i < 4; i++)
{
quadrantCounters[i].text = $"{validPointsPerQuadrant[i]} / {totalPointsPerQuadrant[i]}";
}
}
}
