Unity WebGL - 从LMS / Server中上载的构建中访问StreamingAssets文件夹

时间:2016-08-27 12:16:56

标签: unity3d scorm unity-webgl

我无法从LMS服务器上传的WebGL构建中访问文件StreamingAssets文件夹,我将这些文件保存在StreamingAssets文件夹中以更新它以备将来使用。遵循统一手册中给出的说明,在编辑器中工作正常,但是直接构建运行并且在LMS中它不起作用。

http://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html

发表在论坛上,他们要求遵循手册,但它不起作用。请查看此处附带的脚本以获取更多详细信息。检查名为'userDetailsXmlPath'的协同程序函数,该函数在游戏开始时调用。

public string filePath = Application.streamingAssetsPath + "/UserDetails.xml";
public string result = "";

IEnumerator userDetailsXmlPath() 
{
    print (filePath);

    if (filePath.Contains ("://") || filePath.Contains (":///")) {
        WWW www = new WWW (filePath);
        yield return www;
        result = www.text;

        print (result);
        FetchUserDetails ();
    } else {
        result = File.ReadAllText (filePath);

        print (result);
        FetchUserDetails ();
    }
}

public void FetchUserDetails()
{
    XmlDocument userXml1 = new XmlDocument ();
//  userXml1.Load(Application.streamingAssetsPath + "/UserDetails.xml");
//  userXml1.Load(Application.dataPath + "js/UserDetails 1.xml");
//  userXml1.LoadXml(userXml.text);

    userXml1.LoadXml(result);


    XmlNodeList userList = userXml1.GetElementsByTagName ("user");

    foreach(XmlNode userValue in userList)
    {
        XmlNodeList userContent = userValue.ChildNodes;
        objUser = new Dictionary<string, string>();

        foreach(XmlNode value in userContent)
        {
            objUser.Add (value.Name, value.InnerText);
        }

        userFullDetails.Add (objUser);

        userCountInXml = userList.Count;

        userId = new string[userList.Count];
        questionSetOfUser = new string[userList.Count];
    }

    AssignUserXmlValuesToArray ();
}

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

使用WWW类https://docs.unity3d.com/ScriptReference/Application-streamingAssetsPath.html

访问StreamingAssets路径文件夹

public string filePath = Application.streamingAssetsPath + "/UserDetails.xml";
         public string result = "";
   
   void Awake () 
   { 
   filePath = Application.streamingAssetsPath + "/UserDetails.xml"; 
   }
   
   void Start () 
   { 
   StartCoroutine(userDetailsXmlPath() );
   }
   
         IEnumerator userDetailsXmlPath() 
         {
             print (filePath);
     
             if (filePath.Contains ("://") || filePath.Contains (":///")) {
                 WWW www = new WWW (filePath);
                 yield return www;
                 result = www.text;
     
                 print (result);
                 FetchUserDetails ();
             } else {
                 result = File.ReadAllText (filePath);
     
                 print (result);
                 FetchUserDetails ();
             }
         }
     
         public void FetchUserDetails()
         {
             XmlDocument userXml1 = new XmlDocument ();
     
             userXml1.LoadXml(result);
     
     
             XmlNodeList userList = userXml1.GetElementsByTagName ("user");
     
             foreach(XmlNode userValue in userList)
             {
                 XmlNodeList userContent = userValue.ChildNodes;
                 objUser = new Dictionary<string, string>();
     
                 foreach(XmlNode value in userContent)
                 {
                     objUser.Add (value.Name, value.InnerText);
                 }
     
                 userFullDetails.Add (objUser);
     
                 userCountInXml = userList.Count;
     
                 userId = new string[userList.Count];
                 questionSetOfUser = new string[userList.Count];
             }
     
             AssignUserXmlValuesToArray ();
         }