Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can I get project name?

Discussion in 'Unity Reflect' started by MAATpe, Sep 7, 2023.

  1. MAATpe

    MAATpe

    Joined:
    Apr 12, 2023
    Posts:
    11
    Hello everyone,

    I've added a button in AR mode which should retrieve the project name and then fetch the data I need from SQL server based on this project name. Currently, I have no idea how to retrieve the project name.

    Below is the code after clicking the button:

    Code (CSharp):
    1.      
    2. void OnSqlButtonClicked()
    3.         {
    4.             Text m_Text = GameObject.Find("Text").GetComponent<Text>();
    5.             try
    6.             {
    7.                 using (SqlConnection connection = new SqlConnection(connectionString))
    8.                 {
    9.                     connection.Open();
    10.  
    11.                     string query = "SELECT * FROM [AR].[dbo].[AR_Model] WHERE [ProjectName] = '" + ProjectName + "';";
    12.  
    13.                     using (SqlCommand command = new SqlCommand(query, connection))
    14.                     {
    15.                         using (SqlDataReader reader = command.ExecuteReader())
    16.                         {
    17.                             while (reader.Read())
    18.                             {
    19.                                 latitude = reader.GetString(reader.GetOrdinal("Latitude"));
    20.                                 longitude = reader.GetString(reader.GetOrdinal("Longitude"));
    21.  
    22.                                 m_GPSText.text += "SQL";
    23.                                 m_GPSText.text += "Latitude: " + latitude + "\n";
    24.                                 m_GPSText.text += "Longitude: " + longitude + "\n";
    25.                             }
    26.                         }
    27.                     }
    28.                 }
    29.             }
    30.             catch (Exception ex)
    31.             {
    32.                 m_Text.text += "Error: " + ex.Message;
    33.             }
    34.         }
    I don't really understand how Unity Reflect get the project name, so I've tried to use the onGUI function in ProjectDrawerUIE.cs, but the error said:
    Assets\Scripts\UI\Controllers\ARSideBarController.cs(415,51): error CS0246: The type or namespace name 'SerializedProperty' could not be found (are you missing a using directive or an assembly reference?)

    Here's the code:

    Code (CSharp):
    1. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    2.         {
    3.             if (SessionStateContext<UnityUser, LinkPermission>.current != null)
    4.             {
    5.                 using (var sessionRoomsSelector = UISelectorFactory.createSelector<ProjectRoom[]>(SessionStateContext<UnityUser, LinkPermission>.current, nameof(ISessionStateDataProvider<UnityUser, LinkPermission>.rooms)))
    6.                 {
    7.                     var realProject = property.serializedObject.targetObject as UIStateManager;
    8.                     if (realProject)
    9.                     {
    10.                         try
    11.                         {
    12.                             UnityProject unityProject = realProject.projectSettingStateData.activeProject;
    13.                             ProjectName = unityProject.Name;
    14.                         }
    15.                         catch (Exception ex)
    16.                         {
    17.                             errorMessage = ex.Message;
    18.                         }
    19.                     }
    20.                 }
    21.             }
    22.         }
     
    Last edited: Sep 7, 2023