Topic: cvs data for popups

I hate to ask a basic programming question here but I've been working on this for days. 

I want to input spreadsheet data (presumably with an imported cvs file) into the UI Bubble Popup script.  For the life of me I can't figure it out.  There's a lot of documentation out ther for applying cvs data to scriptable objects, but this script, using a serializable class (CData), is a little different.  Any help or hints would be appreciated.

I'd attach a script but I'm really not working with anything meaningfully different than the example UI Bubble Popup script. 
- -thanks

Re: cvs data for popups

Hello.

CData is a regular class that you can create with an inspector or runtime.
How to create this runtime:

CData newData = new CData
{
    longitude = myLongitude,
    latitude = myLatitude,
    address = "My Address",
    // etc
};

datas.Add(newData); // If you want to create runtime objects, it’s more convenient to use List<CData> datas.

If you still have problems with CData, show me an example of your CVS, and I will give you an example of how to use it.

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: cvs data for popups

Thanks for the help!  Yes, I forgot about the CData class.

Here's my csv.  Again thanks for the help, your asset is great, I just wish I was a better programmer!

Post's attachments

Attachment icon HolySites.csv 37.51 kb, 7 downloads since 2019-07-12 

Re: cvs data for popups

Example:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

public class UIBubblePopupFromCVS : MonoBehaviour
{
    /// <summary>
    /// Root canvas
    /// </summary>
    public Canvas canvas;

    /// <summary>
    /// Bubble popup
    /// </summary>
    public GameObject bubble;

    /// <summary>
    /// Title text
    /// </summary>
    public Text title;

    /// <summary>
    /// Address text
    /// </summary>
    public Text address;

    /// <summary>
    /// Photo RawImage
    /// </summary>
    public RawImage photo;

    public List<CData> datas;

    /// <summary>
    /// Reference to active marker
    /// </summary>
    private OnlineMapsMarker targetMarker;

    /// <summary>
    /// This method is called when downloading photo texture.
    /// </summary>
    /// <param name="texture2D">Photo texture</param>
    private void OnDownloadPhotoComplete(OnlineMapsWWW www)
    {
        Texture2D texture = new Texture2D(1, 1);
        www.LoadImageIntoTexture(texture);

        // Set place texture to bubble popup
        photo.texture = texture;
    }

    /// <summary>
    /// This method is called by clicking on the map
    /// </summary>
    private void OnMapClick()
    {
        // Remove active marker reference
        targetMarker = null;

        // Hide the popup
        bubble.SetActive(false);
    }

    /// <summary>
    /// This method is called by clicking on the marker
    /// </summary>
    /// <param name="marker">The marker on which clicked</param>
    private void OnMarkerClick(OnlineMapsMarkerBase marker)
    {
        // Set active marker reference
        targetMarker = marker as OnlineMapsMarker;

        // Get a result item from instance of the marker
        CData data = marker["data"] as CData;
        if (data == null) return;

        // Show the popup
        bubble.SetActive(true);

        // Set title and address
        title.text = data.title;
        //address.text = data.address;

        // Destroy the previous photo
        if (photo.texture != null)
        {
            OnlineMapsUtils.Destroy(photo.texture);
            photo.texture = null;
        }

        OnlineMapsWWW www = new OnlineMapsWWW(data.photo_url);
        www.OnComplete += OnDownloadPhotoComplete;

        // Initial update position
        UpdateBubblePosition();
    }

    /// <summary>
    /// Start is called on the frame when a script is enabled just before any of the Update methods are called the first time.
    /// </summary>
    private void Start()
    {
        // Subscribe to events of the map 
        OnlineMaps.instance.OnChangePosition += UpdateBubblePosition;
        OnlineMaps.instance.OnChangeZoom += UpdateBubblePosition;
        OnlineMapsControlBase.instance.OnMapClick += OnMapClick;

        if (OnlineMapsControlBaseDynamicMesh.instance != null)
        {
            OnlineMapsControlBaseDynamicMesh.instance.OnMeshUpdated += UpdateBubblePosition;
        }

        if (OnlineMapsCameraOrbit.instance != null)
        {
            OnlineMapsCameraOrbit.instance.OnCameraControl += UpdateBubblePosition;
        }

        if (datas == null) datas = new List<CData>();

        ReadCVS();

        foreach (CData data in datas)
        {
            OnlineMapsMarker marker = OnlineMapsMarkerManager.CreateItem(data.longitude, data.latitude);
            marker["data"] = data;
            marker.OnClick += OnMarkerClick;
        }


        // Initial hide popup
        bubble.SetActive(false);
    }

    private void ReadCVS()
    {
        string filename = "e:/HolySites.csv";
        if (!File.Exists(filename)) return;

        string[] lines = File.ReadAllLines(filename, Encoding.UTF8);

        for (int i = 1; i < lines.Length; i++)
        {
            string[] parts = lines[i].Split(',');
            CData data = new CData
            {
                longitude = double.Parse(parts[4], OnlineMapsUtils.numberFormat),
                latitude = double.Parse(parts[3], OnlineMapsUtils.numberFormat),
                title = parts[0],
                status = parts[1],
                tradition = parts[2],
                photo_url = parts[5],
                webpage = parts[6],
                description = parts[7]
            };

            datas.Add(data);
        }
    }

    /// <summary>
    /// Updates the popup position
    /// </summary>
    private void UpdateBubblePosition()
    {
        // If no marker is selected then exit.
        if (targetMarker == null) return;

        // Hide the popup if the marker is outside the map view
        if (!targetMarker.inMapView)
        {
            if (bubble.activeSelf) bubble.SetActive(false);
        }
        else if (!bubble.activeSelf) bubble.SetActive(true);

        // Convert the coordinates of the marker to the screen position.
        Vector2 screenPosition = OnlineMapsControlBase.instance.GetScreenPosition(targetMarker.position);

        // Add marker height
        screenPosition.y += targetMarker.height;

        // Get a local position inside the canvas.
        Vector2 point;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, screenPosition, null, out point);

        // Set local position of the popup
        (bubble.transform as RectTransform).localPosition = point;
    }

    [Serializable]
    public class CData
    {
        public string title;
        public string status;
        public string tradition;
        public string address;
        public string photo_url;
        public string webpage;
        public string description;
        public double longitude;
        public double latitude;
    }
}

P.S. Note that this script reads cvs from the file system.
If you want, you can modify it and read the file from Assets.
https://docs.unity3d.com/ScriptReference/TextAsset.html

Kind Regards,
Infinity Code Team.

Do not know the best way to thank the developer? Rate the asset in Asset Store!

Re: cvs data for popups

Rad, thank you.