Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Saving Ray coordinates to JSON

Discussion in 'EditorXR' started by KushMathur, Aug 6, 2019.

  1. KushMathur

    KushMathur

    Joined:
    Jul 3, 2019
    Posts:
    22
    Hello Community!

    I have been using Editor XR for one of my projects, and I created a new tool to get the coordinates where I am pointing the ray and save the vector coordinates into a JSON file(or .txt file). But it is not working, rather I am getting just empty curly braces in my file.

    What is the problem?
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.IO;
    4.  
    5. namespace UnityEditor.Experimental.EditorVR.Tools
    6. {
    7.     [MainMenuItem("Json Exporter", "primitive", "Saving data to Json")]
    8.     [SpatialMenuItem("Json Exporter", "primtive", "Saving data to Json")]
    9.     sealed class JsonSaver : MonoBehaviour, ITool, IConnectInterfaces, IInstantiateMenuUI,
    10.         IUsesSpatialHash, IUsesViewerScale, ISelectTool, IIsHoveringOverUI, IIsMainMenuVisible,
    11.         IRayVisibilitySettings, IRequestFeedback, IMenuIcon
    12.     {
    13.         //public JsonData getdata;
    14.  
    15.         [SerializeField]
    16.         Sprite m_Icon;
    17.  
    18.         [Serializable]
    19.  
    20.         public class VerticesData
    21.         {
    22.             public Vector3[] vertices = new Vector3[d];
    23.         }
    24.         public VerticesData verticesData;
    25.         string dataPath;
    26.         public static int d;
    27.  
    28.         public Sprite icon { get { return m_Icon; } }
    29.         public Transform rayOrigin { get; set; }
    30.  
    31.  
    32.         void Start()
    33.         {
    34.             dataPath = Path.Combine(Application.persistentDataPath, "CharacterData.txt");
    35.  
    36.             d = CreateTool.a;
    37.             verticesData.vertices = CreateTool.VerticesData.vertices;
    38.  
    39.         }
    40.  
    41.         void Update()
    42.         {
    43.             SaveCharacter(verticesData, dataPath);
    44.             Debug.Log(Application.persistentDataPath);
    45.         }
    46.  
    47.         static void SaveCharacter(VerticesData data, string path)
    48.         {
    49.             string jsonString = JsonUtility.ToJson(data);
    50.  
    51.             using (StreamWriter streamWriter = File.CreateText(path))
    52.             {
    53.                 streamWriter.Write(jsonString);
    54.             }
    55.         }
    56.         void OnDestroy()
    57.         {
    58.             //ObjectUtils.Destroy(m_ToolMenu);
    59.  
    60.             if (rayOrigin == null)
    61.                 return;
    62.  
    63.             this.RemoveRayVisibilitySettings(rayOrigin, this);
    64.             this.ClearFeedbackRequests();
    65.         }
    66.     }
    67. }
    Here, CreateTool is a different tool which is getting the coordinates of ray pointer. Also, I am simply defining the variable vertices in CreateTool script.

    Code (CSharp):
    1.  [Serializable]
    2.         public class VerticesData
    3.         {
    4.  
    5.             public static Vector3[] vertices = new Vector3[a];
    6.  
    7.         }
    8.  
    9. // and later in code putting value of vertices as
    10. sidesy = rayOrigin.position[1];
    11. VerticesData.vertices[a] =  rayOrigin.position + rayOrigin.forward * sidesy * this.GetViewerScale();;
    12.  
    Am I defining the code in wrong manner?

    Thanks to everyone!!
    Awaiting your comments. If I have described something in a wrong way or their is a confusion, please feel free to ask.
     
  2. amirebrahimi_unity

    amirebrahimi_unity

    Joined:
    Aug 12, 2015
    Posts:
    400
    Hi @KushMathur, were you able to figure this out? This is likely more of an issue with serialization than it is with EditorXR. For starters, you probably don't need a lot of interfaces that you are including. I think you'd only need ITool at the very least since you are copying data from CreateTool.
     
  3. KushMathur

    KushMathur

    Joined:
    Jul 3, 2019
    Posts:
    22
    I did created a temporary solution for it, I am putting a non visible sphere where we point the mouse:D with a tag, and later a new script will take the tags and will take the transform.position of these spheres. It is working, I do not know but, maybe it will be having performance issue later in the project.

    Thanks for reply though!