Search Unity

string.split not working on webplayer?

Discussion in 'Editor & General Support' started by DaphydTheBard, Jun 26, 2012.

  1. DaphydTheBard

    DaphydTheBard

    Joined:
    Mar 6, 2012
    Posts:
    101
    Hey Guys

    This is baffling me. I am writing some very basic code to get the full URL that the webplayer is running at (which works) and then split the string so can analyse the parameters passed to it.

    I get the URL back fine, and can display it on a label. However when I run this code in the webplayer, the string.split function is not doing anything.

    It works in the editor (by giving it a dummy url to split).

    Does anyone have any ideas why this might be?

    Thanks in advance, code below.


    Code (csharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class csCarousel
    6. : MonoBehaviour
    7. {
    8.     private string m_URL_Full;
    9.     private string m_URL_Base;
    10.     private string m_URL_Params;   
    11.  
    12.     private bool m_hasParams = false;
    13.  
    14.     void Start()
    15.     {
    16.         ExtractArgumentsFromURL();
    17.     }
    18.  
    19.     public void ReceiveURL(string url)
    20.     {        
    21.         m_URL_Full = url;
    22.     }
    23.  
    24.     public void OnGUI()
    25.     {
    26.         GUI.Label(new Rect(10,10,800,25), "FULL: " + m_URL_Full);
    27.         GUI.Label(new Rect(10,40,800,25), "BASE: " + m_URL_Base);
    28.     }
    29.    
    30.     // Extracts the arguments from the application URL.
    31.     private void ExtractArgumentsFromURL()
    32.     {
    33.         // Get full URL
    34.         Application.ExternalEval
    35.             ("var unity = unityObject.getObjectById(\"unityPlayer\");unity.SendMessage(\"" + name + "\", \"ReceiveURL\", document.URL);");
    36.        
    37.         //Dummy URL for running in editor to test
    38.         //m_URL_Full = "file:///C:/Users/Public/Documents/Unity%20Projects/ObituaryCarousel/Build/Web/WebPlayer/WebPlayer.html?id=5";
    39.            
    40.         // Did we get a URL?  (i.e. we are running in a webplayer)
    41.         if (m_URL_Full != null)
    42.         {      
    43.             string[] urlParts = m_URL_Full.Split(new string[] { "?" }, System.StringSplitOptions.None);
    44.    
    45.             m_URL_Base = urlParts[0];
    46.    
    47.             if (urlParts.GetUpperBound(0) == 1)
    48.             {
    49.                 m_URL_Params = urlParts[1];
    50.                 m_hasParams  = true;
    51.             }
    52.         }      
    53.     }
    54. }
     
    Last edited: Jun 26, 2012
  2. DaphydTheBard

    DaphydTheBard

    Joined:
    Mar 6, 2012
    Posts:
    101
    Ok I get more and more frustrated. I have simplified the code a little, and I still don't get any results. Substring doesn't work either. If anyone can shed some light on this, it would be appreciated. I am running the webplayer in chrome.

    Code (csharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class csCarousel
    8. : MonoBehaviour
    9. {
    10.     private string m_URL_Full;
    11.     private string m_URL_Base;
    12.     private string m_URL_Params;   
    13.  
    14.     private bool m_hasParams = false;
    15.  
    16.     private Dictionary<string, string> m_params;
    17.  
    18.     void Start()
    19.     {
    20.         ExtractArgumentsFromURL();
    21.        
    22.     }
    23.  
    24.     public void ReceiveURL(string url)
    25.     {        
    26.         m_URL_Full = url;
    27.     }
    28.  
    29.     public void OnGUI()
    30.     {
    31.         GUI.Label(new Rect(10,10,800,25), "FULL: " + m_URL_Full);
    32.         GUI.Label(new Rect(10,50,800,25), "BASE: " + m_URL_Base);
    33.         GUI.Label(new Rect(10,90,800,25), "PRMS: " + m_URL_Params);
    34.     }
    35.    
    36.     // Extracts the arguments from the application URL.
    37.     private void ExtractArgumentsFromURL()
    38.     {
    39.         // Get full URL
    40.         Application.ExternalEval
    41.             ("var unity = unityObject.getObjectById(\"unityPlayer\");unity.SendMessage(\"" + name + "\", \"ReceiveURL\", document.URL);");
    42.        
    43.         // Did we get a URL?  (i.e. we are running in a webplayer)
    44.         if (m_URL_Full != null)
    45.         {      
    46.             m_URL_Base   = m_URL_Full.Substring(0, m_URL_Full.IndexOf("?"));
    47.             m_URL_Params = m_URL_Full.Substring(m_URL_Full.IndexOf("?")+1);
    48.         }      
    49.     }
    50. }
    $temp.jpg
     
  3. DaphydTheBard

    DaphydTheBard

    Joined:
    Mar 6, 2012
    Posts:
    101
    SOLVED:

    Needed to put the code that splits up the URL in the callback function.

    Thanks for reading, anyway :)